-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchoice-generation.ts
70 lines (62 loc) · 2.04 KB
/
choice-generation.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
64
65
66
67
68
69
70
import { Choice, Project, Package, CreatedChoicesAndScriptNames, SavedEntry } from './interfaces'
export const createChoices = (
projects: Array<Project>,
scriptFilterFn: (_: string) => boolean = () => true
): CreatedChoicesAndScriptNames => {
const tempSet = new Set<string>([])
if (!scriptFilterFn) {
scriptFilterFn = () => {
return true
}
}
const choices = projects
// some projects may not have a single script that is allowed to run, so filter them out
.filter(
(project: Project) =>
project.packageJson &&
project.packageJson.scripts &&
Object.keys(project.packageJson.scripts).some((scriptName) => scriptFilterFn(scriptName))
)
.reduce((total: Array<Choice>, project: Project) => {
// keep track of the scripts that were found
if (project.packageJson.scripts) {
Object.keys(project.packageJson.scripts).forEach((s) => tempSet.add(s))
}
const availableScripts = Object.keys(project.packageJson.scripts || [])
.sort()
.filter(scriptFilterFn)
// insert a project
total.push({
name: project.packageName,
category: project.reviewCategory,
scriptExecutable: 'npm',
scriptCommand: ['run'],
availableScripts
})
return total
}, [])
return {
choices,
allScriptNames: Array.from(tempSet)
}
}
export const applySelectedScriptsOnChoicesFromCache = (
choices: Array<Choice>,
savedProjectScripts: SavedEntry,
scriptFilterFn: (param: string) => boolean
): void => {
// set the initial values, if possible
savedProjectScripts.packages.forEach((savedProjectScript: Package) => {
const foundChoice = choices.find(
(unusedChoice: Choice) => unusedChoice.name === savedProjectScript.packageName
)
if (foundChoice) {
if (!scriptFilterFn || scriptFilterFn(savedProjectScript.script)) {
if (!Array.isArray(foundChoice.initial)) {
foundChoice.initial = []
}
foundChoice.initial.push(savedProjectScript.script)
}
}
})
}