Skip to content

Commit

Permalink
fix: move cache outside of tests perview to not touch files
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed Dec 6, 2024
1 parent cca2863 commit 543a108
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 53 deletions.
27 changes: 15 additions & 12 deletions cli/bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {resolve, relative, join} = require('path')
const {spawnSync} = require('child_process')
const build = require('../lib/build.js')
const {nwo} = require('../lib/gh')
const {CacheVersionSha} = require('../lib/cache.js')

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

build({
releases: require('../releases.json'),
loglevel: process.argv.includes('--debug') || process.env.CI ? 'verbose' : 'info',
prerelease: false,
useCurrent: checkOnly,
contentPath,
navPath,
})
.then(() => {
;(async () => {
try {
await build({
cache: await CacheVersionSha.load(join(ROOT, 'cache.json')),
releases: require('../releases.json'),
loglevel: process.argv.includes('--debug') || process.env.CI ? 'verbose' : 'info',
prerelease: false,
useCurrent: checkOnly,
contentPath,
navPath,
})
if (checkOnly) {
checkContent()
}
return console.log('DONE')
})
.catch(e => {
} catch (e) {
console.error(e)
process.exit(1)
})
}
})()
7 changes: 2 additions & 5 deletions cli/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -56,7 +55,7 @@ const getCurrentVersions = nav => {
}
}

const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, contentPath, prerelease}) => {
const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, contentPath, prerelease, cache}) => {
/* istanbul ignore next */
if (loglevel) {
log.on(loglevel)
Expand Down Expand Up @@ -114,13 +113,11 @@ const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, conte
}
})

const cache = await CacheVersionSha.load()

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

await cache.save()
await cache?.save()

await updateNav(updates, {nav: navDoc, path: navPath})
}
Expand Down
12 changes: 5 additions & 7 deletions cli/lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
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) {
constructor(cache, path) {
this.cache = cache
this.path = path
}

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

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

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

Expand Down
23 changes: 0 additions & 23 deletions cli/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,6 @@ 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
Expand Down
6 changes: 0 additions & 6 deletions content/cli/cache.json

This file was deleted.

0 comments on commit 543a108

Please sign in to comment.