Skip to content

Commit 543a108

Browse files
committed
fix: move cache outside of tests perview to not touch files
1 parent cca2863 commit 543a108

File tree

5 files changed

+22
-53
lines changed

5 files changed

+22
-53
lines changed

cli/bin/build.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {resolve, relative, join} = require('path')
22
const {spawnSync} = require('child_process')
33
const build = require('../lib/build.js')
44
const {nwo} = require('../lib/gh')
5+
const {CacheVersionSha} = require('../lib/cache.js')
56

67
// check only build with the current versions instead of checking the registry
78
// and also fails if any changes are detected. this is used in CI to make sure
@@ -24,21 +25,23 @@ const checkContent = () => {
2425
}
2526
}
2627

27-
build({
28-
releases: require('../releases.json'),
29-
loglevel: process.argv.includes('--debug') || process.env.CI ? 'verbose' : 'info',
30-
prerelease: false,
31-
useCurrent: checkOnly,
32-
contentPath,
33-
navPath,
34-
})
35-
.then(() => {
28+
;(async () => {
29+
try {
30+
await build({
31+
cache: await CacheVersionSha.load(join(ROOT, 'cache.json')),
32+
releases: require('../releases.json'),
33+
loglevel: process.argv.includes('--debug') || process.env.CI ? 'verbose' : 'info',
34+
prerelease: false,
35+
useCurrent: checkOnly,
36+
contentPath,
37+
navPath,
38+
})
3639
if (checkOnly) {
3740
checkContent()
3841
}
3942
return console.log('DONE')
40-
})
41-
.catch(e => {
43+
} catch (e) {
4244
console.error(e)
4345
process.exit(1)
44-
})
46+
}
47+
})()

cli/lib/build.js

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

109
const DOCS_PATH = 'cli'
1110

@@ -56,7 +55,7 @@ const getCurrentVersions = nav => {
5655
}
5756
}
5857

59-
const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, contentPath, prerelease}) => {
58+
const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, contentPath, prerelease, cache}) => {
6059
/* istanbul ignore next */
6160
if (loglevel) {
6261
log.on(loglevel)
@@ -114,13 +113,11 @@ const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, conte
114113
}
115114
})
116115

117-
const cache = await CacheVersionSha.load()
118-
119116
const updates = await Promise.all(
120117
releases.map(r => extractRelease(r, {cache, contentPath, baseNav: navData, prerelease})),
121118
).then(r => r.filter(Boolean))
122119

123-
await cache.save()
120+
await cache?.save()
124121

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

cli/lib/cache.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
const {join} = require('path')
21
const fs = require('fs/promises')
32

43
/** cache npm cli version shas to NOT pull down changes we already have */
54
class CacheVersionSha {
6-
constructor(cache) {
5+
constructor(cache, path) {
76
this.cache = cache
7+
this.path = path
88
}
99

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')))
10+
static async load(path) {
11+
return new CacheVersionSha(JSON.parse(await fs.readFile(path, 'utf-8')), path)
1412
}
1513

1614
async save() {
17-
await fs.writeFile(CacheVersionSha.path, JSON.stringify(this.cache, null, 2))
15+
await fs.writeFile(this.path, JSON.stringify(this.cache, null, 2))
1816
return this
1917
}
2018

cli/test/index.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,6 @@ const mockBuild = async (t, {releases = getReleases(), packument = {}, testdir:
8383
},
8484
},
8585
'@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-
},
10986
'../lib/gh.js': {
11087
getCurrentSha: async () => {
11188
shaCounter = shaCounter + 1

content/cli/cache.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)