Skip to content

Commit

Permalink
feat: prep for v11, drops fetching v6 / v7, adds cache
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed Dec 6, 2024
1 parent d3acafc commit cca2863
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 11 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.reuse/
.nyc_output/
coverage/
content/cli/cache.json
7 changes: 6 additions & 1 deletion cli/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const semver = require('semver')
const pacote = require('pacote')
const extractRelease = require('./extract')
const log = require('./log')
const {CacheVersionSha} = require('./cache')

const DOCS_PATH = 'cli'

Expand Down Expand Up @@ -113,10 +114,14 @@ const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, conte
}
})

const cache = await CacheVersionSha.load()

const updates = await Promise.all(
releases.map(r => extractRelease(r, {contentPath, baseNav: navData, prerelease})),
releases.map(r => extractRelease(r, {cache, contentPath, baseNav: navData, prerelease})),
).then(r => r.filter(Boolean))

await cache.save()

await updateNav(updates, {nav: navDoc, path: navPath})
}

Expand Down
33 changes: 33 additions & 0 deletions cli/lib/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const {join} = require('path')
const fs = require('fs/promises')

/** cache npm cli version shas to NOT pull down changes we already have */
class CacheVersionSha {
constructor(cache) {
this.cache = cache
}

static path = join(__dirname, '../../content/cli/cache.json')

static async load() {
return new CacheVersionSha(JSON.parse(await fs.readFile(this.path, 'utf-8')))
}

async save() {
await fs.writeFile(CacheVersionSha.path, JSON.stringify(this.cache, null, 2))
return this
}

set(id, sha) {
this.cache[id] = sha
return this
}

same(id, value) {
return this.cache[id] === value
}
}

module.exports = {
CacheVersionSha,
}
11 changes: 10 additions & 1 deletion cli/lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,16 @@ const writeChangelog = async ({release, nav, cwd, srcPath, contentPath}) => {
})
}

const unpackRelease = async (release, {contentPath, baseNav, prerelease = false}) => {
const unpackRelease = async (release, {cache, contentPath, baseNav, prerelease = false}) => {
if (cache) {
const sha = await gh.getCurrentSha(release.branch)
if (cache.same(release.id, sha)) {
log.info(`Skipping ${release.id} due to cache`)
return
}
cache.set(release.id, sha)
}

if (release.prerelease && !prerelease) {
log.info(`Skipping ${release.id} due to prerelease ${release.version}`)
return
Expand Down
21 changes: 20 additions & 1 deletion cli/lib/gh.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
const {posix, sep} = require('node:path')
const {execSync} = require('node:child_process')

if (!process.env.GITHUB_TOKEN) {
throw new Error('GITHUB_TOKEN env var is required to build CLI docs')
try {
// this allows people to run this locally
process.env.GITHUB_TOKEN = execSync('gh auth token', {encoding: 'utf8'}).trim()
} catch (err) {
throw new Error('GITHUB_TOKEN env var is required to build CLI docs')
}
}

let octokit
const owner = 'npm'
const repo = 'cli'
const opts = {owner, repo}

const getCurrentSha = async branch => {
if (!octokit) {
const {Octokit} = await import('@octokit/rest')
octokit = new Octokit({auth: process.env.GITHUB_TOKEN})
}
const {data} = await octokit.repos.getBranch({
...opts,
branch,
})
return data.commit.sha
}

const getFile = async ({sha, ref, path}) => {
if (!octokit) {
const {Octokit} = await import('@octokit/rest')
Expand Down Expand Up @@ -51,5 +69,6 @@ const pathExists = async (ref, path) => {
module.exports = {
getFile,
pathExists,
getCurrentSha,
nwo: `${owner}/${repo}`,
}
12 changes: 4 additions & 8 deletions cli/releases.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
[
{
"id": "v6",
"branch": "release/v6"
},
{
"id": "v7",
"branch": "release/v7"
},
{
"id": "v8",
"branch": "release/v8"
Expand All @@ -17,6 +9,10 @@
},
{
"id": "v10",
"branch": "release/v10"
},
{
"id": "v11",
"branch": "latest"
}
]
28 changes: 28 additions & 0 deletions cli/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const mockBuild = async (t, {releases = getReleases(), packument = {}, testdir:
return yaml.stringify(children).replace(new RegExp(`/cli/${id}/`, 'g'), '/')
}

let shaCounter = 0
const build = t.mockRequire('../lib/build', {
pacote: {
...pacote,
Expand All @@ -82,7 +83,34 @@ const mockBuild = async (t, {releases = getReleases(), packument = {}, testdir:
},
},
'@prettier/sync': {format: s => s},
'../lib/cache.js': {
CacheVersionSha: class CacheVersionSha {
constructor() {
this.cache = {}
}

static async load() {
return new CacheVersionSha()
}

async save() {
return this
}

set() {
return this
}

same() {
return false
}
},
},
'../lib/gh.js': {
getCurrentSha: async () => {
shaCounter = shaCounter + 1
return 'abc' + shaCounter
},
getFile: async ({ref}) => navSection(ref),
pathExists: async (ref, p) => {
if (ref.includes('v6') && p.includes('docs/lib/content')) {
Expand Down
6 changes: 6 additions & 0 deletions content/cli/cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"v6": "abc1",
"v7": "abc2",
"v8": "abc3",
"v9": "abc4"
}

0 comments on commit cca2863

Please sign in to comment.