-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsave-load.ts
63 lines (52 loc) · 1.77 KB
/
save-load.ts
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
56
57
58
59
60
61
62
63
import path from 'path'
import fs from 'fs'
import { version } from './package.json'
import { SubmittedChoice, SavedEntries, SavedEntry } from './interfaces'
const answersFilePath = path.resolve(__dirname, '.cached-answers.json')
export const save = (rushRootDir: string, projectsToRun: Array<SubmittedChoice>): void => {
let fileContents
try {
fileContents = JSON.parse(fs.readFileSync(answersFilePath).toString())
} catch (e) {
if (e.code === 'ENOENT') {
// no cached answers, that's ok.
fileContents = {}
} else {
// other unknown error, throw it
throw e
}
}
// json-ify
const projectsToRunByNameJsonFriendly: Array<SubmittedChoice> = []
projectsToRun.forEach((project: SubmittedChoice) => {
const jsonFriendly: SubmittedChoice = { ...project }
jsonFriendly.project = undefined
projectsToRunByNameJsonFriendly.push(jsonFriendly)
})
// add to existing results, if any
fileContents[rushRootDir] = {};
fileContents[rushRootDir].packages = projectsToRunByNameJsonFriendly
fileContents[rushRootDir].cliVersion = version
fs.writeFileSync(answersFilePath, JSON.stringify(fileContents, undefined, 4))
}
export const load = (rushRootDir: string): SavedEntry => {
let cachedAnswers: SavedEntries = {}
try {
cachedAnswers = JSON.parse(fs.readFileSync(answersFilePath).toString())
if (cachedAnswers[rushRootDir] && cachedAnswers[rushRootDir].cliVersion !== version) {
// can't use this data because it's from a different version
cachedAnswers = {}
}
} catch (e) {
if (e.code === 'ENOENT') {
// no cached answers, that's ok.
} else {
// other unknown error, throw it
throw e
}
}
return cachedAnswers[rushRootDir] || {
packages: [],
cliVersion: version,
}
}