-
Notifications
You must be signed in to change notification settings - Fork 378
/
project-utils.ts
123 lines (112 loc) · 3.33 KB
/
project-utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as inquirer from 'inquirer';
import { Answers, Question } from 'inquirer';
import { Input } from '../../commands';
import { getValueOrDefault } from '../compiler/helpers/get-value-or-default';
import { Configuration, ProjectConfiguration } from '../configuration';
import { generateSelect } from '../questions/questions';
export function shouldAskForProject(
schematic: string,
configurationProjects: { [key: string]: ProjectConfiguration },
appName: string,
) {
return (
['app', 'sub-app', 'library', 'lib'].includes(schematic) === false &&
configurationProjects &&
Object.entries(configurationProjects).length !== 0 &&
!appName
);
}
export function shouldGenerateSpec(
configuration: Required<Configuration>,
schematic: string,
appName: string,
specValue: boolean,
specPassedAsInput?: boolean,
) {
if (specPassedAsInput === true || specPassedAsInput === undefined) {
// CLI parameters has the highest priority
return specValue;
}
let specConfiguration = getValueOrDefault(
configuration,
'generateOptions.spec',
appName || '',
);
if (typeof specConfiguration === 'boolean') {
return specConfiguration;
}
if (
typeof specConfiguration === 'object' &&
specConfiguration[schematic] !== undefined
) {
return specConfiguration[schematic];
}
if (typeof specConfiguration === 'object' && appName) {
// The appName has a generateOption spec, but not for the schematic trying to generate
// Check if the global generateOptions has a spec to use instead
specConfiguration = getValueOrDefault(
configuration,
'generateOptions.spec',
'',
);
if (typeof specConfiguration === 'boolean') {
return specConfiguration;
}
if (
typeof specConfiguration === 'object' &&
specConfiguration[schematic] !== undefined
) {
return specConfiguration[schematic];
}
}
return specValue;
}
export function shouldGenerateFlat(
configuration: Required<Configuration>,
appName: string,
flatValue: boolean,
): boolean {
// CLI parameters have the highest priority
if (flatValue === true) {
return flatValue;
}
const flatConfiguration = getValueOrDefault(
configuration,
'generateOptions.flat',
appName || '',
);
if (typeof flatConfiguration === 'boolean') {
return flatConfiguration;
}
return flatValue;
}
export async function askForProjectName(
promptQuestion: string,
projects: string[],
): Promise<Answers> {
const questions: Question[] = [
generateSelect('appName')(promptQuestion)(projects),
];
const prompt = inquirer.createPromptModule();
return prompt(questions);
}
export function moveDefaultProjectToStart(
configuration: Configuration,
defaultProjectName: string,
defaultLabel: string,
) {
let projects: string[] =
configuration.projects != null ? Object.keys(configuration.projects) : [];
if (configuration.sourceRoot !== 'src') {
projects = projects.filter(
(p) => p !== defaultProjectName.replace(defaultLabel, ''),
);
}
projects.unshift(defaultProjectName);
return projects;
}
export function hasValidOptionFlag(queriedOptionName: string, options: Input[], queriedValue: string|number|boolean = true): boolean {
return options.some(
(option: Input) => option.name === queriedOptionName && option.value === queriedValue
);
}