/
generate.action.ts
128 lines (115 loc) · 4.09 KB
/
generate.action.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
124
125
126
127
128
import * as chalk from 'chalk';
import { Answers } from 'inquirer';
import { Input } from '../commands';
import { getValueOrDefault } from '../lib/compiler/helpers/get-value-or-default';
import {
AbstractCollection,
Collection,
CollectionFactory,
SchematicOption,
} from '../lib/schematics';
import { MESSAGES } from '../lib/ui';
import { loadConfiguration } from '../lib/utils/load-configuration';
import {
askForProjectName,
moveDefaultProjectToStart,
shouldAskForProject,
shouldGenerateSpec,
} from '../lib/utils/project-utils';
import { AbstractAction } from './abstract.action';
export class GenerateAction extends AbstractAction {
public async handle(inputs: Input[], options: Input[]) {
await generateFiles(inputs.concat(options));
}
}
const generateFiles = async (inputs: Input[]) => {
const configuration = await loadConfiguration();
const collectionOption = inputs.find(
(option) => option.name === 'collection',
)!.value as string;
const schematic = inputs.find((option) => option.name === 'schematic')!
.value as string;
const appName = inputs.find((option) => option.name === 'project')!
.value as string;
const spec = inputs.find((option) => option.name === 'spec');
const collection: AbstractCollection = CollectionFactory.create(
collectionOption || configuration.collection || Collection.NESTJS,
);
const schematicOptions: SchematicOption[] = mapSchematicOptions(inputs);
schematicOptions.push(
new SchematicOption('language', configuration.language),
);
const configurationProjects = configuration.projects;
let sourceRoot = appName
? getValueOrDefault(configuration, 'sourceRoot', appName)
: configuration.sourceRoot;
const specValue = spec!.value as boolean;
const specOptions = spec!.options as any;
let generateSpec = shouldGenerateSpec(
configuration,
schematic,
appName,
specValue,
specOptions.passedAsInput,
);
// If you only add a `lib` we actually don't have monorepo: true BUT we do have "projects"
// Ensure we don't run for new app/libs schematics
if (shouldAskForProject(schematic, configurationProjects, appName)) {
const defaultLabel = ' [ Default ]';
let defaultProjectName: string = configuration.sourceRoot + defaultLabel;
for (const property in configurationProjects) {
if (
configurationProjects[property].sourceRoot === configuration.sourceRoot
) {
defaultProjectName = property + defaultLabel;
break;
}
}
const projects = moveDefaultProjectToStart(
configuration,
defaultProjectName,
defaultLabel,
);
const answers: Answers = await askForProjectName(
MESSAGES.PROJECT_SELECTION_QUESTION,
projects,
);
const project: string = answers.appName.replace(defaultLabel, '');
if (project !== configuration.sourceRoot) {
sourceRoot = configurationProjects[project].sourceRoot;
}
if (answers.appName !== defaultProjectName) {
// Only overwrite if the appName is not the default- as it has already been loaded above
generateSpec = shouldGenerateSpec(
configuration,
schematic,
answers.appName,
specValue,
specOptions.passedAsInput,
);
}
}
schematicOptions.push(new SchematicOption('sourceRoot', sourceRoot));
schematicOptions.push(new SchematicOption('spec', generateSpec));
try {
const schematicInput = inputs.find((input) => input.name === 'schematic');
if (!schematicInput) {
throw new Error('Unable to find a schematic for this configuration');
}
await collection.execute(schematicInput.value as string, schematicOptions);
} catch (error) {
if (error && error.message) {
console.error(chalk.red(error.message));
}
}
};
const mapSchematicOptions = (inputs: Input[]): SchematicOption[] => {
const excludedInputNames = ['schematic', 'spec'];
const options: SchematicOption[] = [];
inputs.forEach((input) => {
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
options.push(new SchematicOption(input.name, input.value));
}
});
return options;
};