Skip to content

Commit 47b26a2

Browse files
committed
feat: adds caching, drops 6 / 7 generation
1 parent f8716d6 commit 47b26a2

File tree

7 files changed

+67
-11
lines changed

7 files changed

+67
-11
lines changed

cli/bin/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const checkContent = () => {
2727
build({
2828
releases: require('../releases.json'),
2929
loglevel: process.argv.includes('--debug') || process.env.CI ? 'verbose' : 'info',
30-
prerelease: true,
30+
prerelease: false,
3131
useCurrent: checkOnly,
3232
contentPath,
3333
navPath,

cli/lib/build.js

Lines changed: 7 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,15 @@ 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+
updates.forEach(r => cache.set(r.id, r.sha))
124+
await cache.save()
125+
120126
await updateNav(updates, {nav: navDoc, path: navPath})
121127
}
122128

cli/lib/cache.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
get(id) {
22+
return this.cache[id]
23+
}
24+
25+
set(id, sha) {
26+
this.cache[id] = sha
27+
return this
28+
}
29+
30+
same(id, value) {
31+
return this.cache[id] === value
32+
}
33+
}
34+
35+
module.exports = {
36+
CacheVersionSha,
37+
}

cli/lib/extract.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ 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+
const sha = await gh.getCurrentSha(release.branch)
143+
if (cache.same(release.id, sha)) {
144+
log.info(`Skipping ${release.id} due to cache`)
145+
return
146+
}
147+
142148
if (release.prerelease && !prerelease) {
143149
log.info(`Skipping ${release.id} due to prerelease ${release.version}`)
144150
return
@@ -219,6 +225,7 @@ const unpackRelease = async (release, {contentPath, baseNav, prerelease = false}
219225
return {
220226
...release,
221227
nav: nav.children,
228+
sha,
222229
}
223230
}
224231

cli/lib/gh.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ const owner = 'npm'
1515
const repo = 'cli'
1616
const opts = {owner, repo}
1717

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+
1830
const getFile = async ({sha, ref, path}) => {
1931
if (!octokit) {
2032
const {Octokit} = await import('@octokit/rest')
@@ -57,5 +69,6 @@ const pathExists = async (ref, path) => {
5769
module.exports = {
5870
getFile,
5971
pathExists,
72+
getCurrentSha,
6073
nwo: `${owner}/${repo}`,
6174
}

cli/releases.json

Lines changed: 0 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"

content/cli/cache.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)