-
Notifications
You must be signed in to change notification settings - Fork 22
/
update-tags.mjs
55 lines (47 loc) · 1.47 KB
/
update-tags.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { readFile, writeFile } from 'fs/promises'
import { glob as baseGlob } from 'glob'
import * as prettier from 'prettier'
// FIXME: replace with a JSON import when Import Attributes become part of
// EcmaScript. See https://github.com/tc39/proposal-import-attributes
const { version } = JSON.parse(
await readFile('package.json', { encoding: 'utf8' }),
)
const tagPath = `v${version}`
async function glob(path) {
return await baseGlob(path, {
ignore: ['node_modules/**'],
posix: true,
})
}
async function fileUpdates(fileSet, key, oldValue, newValue) {
await Promise.all(
fileSet.map(async (fileItem) => {
let file = JSON.parse(
await readFile(fileItem, {
encoding: 'utf-8',
}),
)
file[key] = file[key].replace(
`https://gcn.nasa.gov/schema/${oldValue}/`,
`https://gcn.nasa.gov/schema/${newValue}/`,
)
await writeFile(
fileItem,
await prettier.format(JSON.stringify(file), {
parser: 'json',
}),
)
}),
)
}
async function loadAndUpdateFiles(oldValue, newValue) {
const schemaFilenames = await glob('**/*.schema.json')
await fileUpdates(schemaFilenames, '$id', oldValue, newValue)
const exampleFilenames = await glob('**/*.example.json')
await fileUpdates(exampleFilenames, '$schema', oldValue, newValue)
}
if (process.argv.includes('--reset')) {
await loadAndUpdateFiles(tagPath, 'main')
} else {
await loadAndUpdateFiles('main', tagPath)
}