Skip to content

Commit cca2863

Browse files
committed
feat: prep for v11, drops fetching v6 / v7, adds cache
1 parent d3acafc commit cca2863

File tree

8 files changed

+108
-11
lines changed

8 files changed

+108
-11
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/.reuse/
66
.nyc_output/
77
coverage/
8+
content/cli/cache.json

cli/lib/build.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const semver = require('semver')
55
const pacote = require('pacote')
66
const extractRelease = require('./extract')
77
const log = require('./log')
8+
const {CacheVersionSha} = require('./cache')
89

910
const DOCS_PATH = 'cli'
1011

@@ -113,10 +114,14 @@ const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, conte
113114
}
114115
})
115116

117+
const cache = await CacheVersionSha.load()
118+
116119
const updates = await Promise.all(
117-
releases.map(r => extractRelease(r, {contentPath, baseNav: navData, prerelease})),
120+
releases.map(r => extractRelease(r, {cache, contentPath, baseNav: navData, prerelease})),
118121
).then(r => r.filter(Boolean))
119122

123+
await cache.save()
124+
120125
await updateNav(updates, {nav: navDoc, path: navPath})
121126
}
122127

cli/lib/cache.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const {join} = require('path')
2+
const fs = require('fs/promises')
3+
4+
/** cache npm cli version shas to NOT pull down changes we already have */
5+
class CacheVersionSha {
6+
constructor(cache) {
7+
this.cache = cache
8+
}
9+
10+
static path = join(__dirname, '../../content/cli/cache.json')
11+
12+
static async load() {
13+
return new CacheVersionSha(JSON.parse(await fs.readFile(this.path, 'utf-8')))
14+
}
15+
16+
async save() {
17+
await fs.writeFile(CacheVersionSha.path, JSON.stringify(this.cache, null, 2))
18+
return this
19+
}
20+
21+
set(id, sha) {
22+
this.cache[id] = sha
23+
return this
24+
}
25+
26+
same(id, value) {
27+
return this.cache[id] === value
28+
}
29+
}
30+
31+
module.exports = {
32+
CacheVersionSha,
33+
}

cli/lib/extract.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,16 @@ const writeChangelog = async ({release, nav, cwd, srcPath, contentPath}) => {
138138
})
139139
}
140140

141-
const unpackRelease = async (release, {contentPath, baseNav, prerelease = false}) => {
141+
const unpackRelease = async (release, {cache, contentPath, baseNav, prerelease = false}) => {
142+
if (cache) {
143+
const sha = await gh.getCurrentSha(release.branch)
144+
if (cache.same(release.id, sha)) {
145+
log.info(`Skipping ${release.id} due to cache`)
146+
return
147+
}
148+
cache.set(release.id, sha)
149+
}
150+
142151
if (release.prerelease && !prerelease) {
143152
log.info(`Skipping ${release.id} due to prerelease ${release.version}`)
144153
return

cli/lib/gh.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
const {posix, sep} = require('node:path')
2+
const {execSync} = require('node:child_process')
23

34
if (!process.env.GITHUB_TOKEN) {
4-
throw new Error('GITHUB_TOKEN env var is required to build CLI docs')
5+
try {
6+
// this allows people to run this locally
7+
process.env.GITHUB_TOKEN = execSync('gh auth token', {encoding: 'utf8'}).trim()
8+
} catch (err) {
9+
throw new Error('GITHUB_TOKEN env var is required to build CLI docs')
10+
}
511
}
612

713
let octokit
814
const owner = 'npm'
915
const repo = 'cli'
1016
const opts = {owner, repo}
1117

18+
const getCurrentSha = async branch => {
19+
if (!octokit) {
20+
const {Octokit} = await import('@octokit/rest')
21+
octokit = new Octokit({auth: process.env.GITHUB_TOKEN})
22+
}
23+
const {data} = await octokit.repos.getBranch({
24+
...opts,
25+
branch,
26+
})
27+
return data.commit.sha
28+
}
29+
1230
const getFile = async ({sha, ref, path}) => {
1331
if (!octokit) {
1432
const {Octokit} = await import('@octokit/rest')
@@ -51,5 +69,6 @@ const pathExists = async (ref, path) => {
5169
module.exports = {
5270
getFile,
5371
pathExists,
72+
getCurrentSha,
5473
nwo: `${owner}/${repo}`,
5574
}

cli/releases.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
[
2-
{
3-
"id": "v6",
4-
"branch": "release/v6"
5-
},
6-
{
7-
"id": "v7",
8-
"branch": "release/v7"
9-
},
102
{
113
"id": "v8",
124
"branch": "release/v8"
@@ -17,6 +9,10 @@
179
},
1810
{
1911
"id": "v10",
12+
"branch": "release/v10"
13+
},
14+
{
15+
"id": "v11",
2016
"branch": "latest"
2117
}
2218
]

cli/test/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const mockBuild = async (t, {releases = getReleases(), packument = {}, testdir:
6666
return yaml.stringify(children).replace(new RegExp(`/cli/${id}/`, 'g'), '/')
6767
}
6868

69+
let shaCounter = 0
6970
const build = t.mockRequire('../lib/build', {
7071
pacote: {
7172
...pacote,
@@ -82,7 +83,34 @@ const mockBuild = async (t, {releases = getReleases(), packument = {}, testdir:
8283
},
8384
},
8485
'@prettier/sync': {format: s => s},
86+
'../lib/cache.js': {
87+
CacheVersionSha: class CacheVersionSha {
88+
constructor() {
89+
this.cache = {}
90+
}
91+
92+
static async load() {
93+
return new CacheVersionSha()
94+
}
95+
96+
async save() {
97+
return this
98+
}
99+
100+
set() {
101+
return this
102+
}
103+
104+
same() {
105+
return false
106+
}
107+
},
108+
},
85109
'../lib/gh.js': {
110+
getCurrentSha: async () => {
111+
shaCounter = shaCounter + 1
112+
return 'abc' + shaCounter
113+
},
86114
getFile: async ({ref}) => navSection(ref),
87115
pathExists: async (ref, p) => {
88116
if (ref.includes('v6') && p.includes('docs/lib/content')) {

content/cli/cache.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"v6": "abc1",
3+
"v7": "abc2",
4+
"v8": "abc3",
5+
"v9": "abc4"
6+
}

0 commit comments

Comments
 (0)