-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
init.ts
executable file
·188 lines (171 loc) · 5.67 KB
/
init.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { cypressInitGenerator } from '@nrwl/cypress';
import { GeneratorCallback, logger, Tree } from '@nrwl/devkit';
import {
addDependenciesToPackageJson,
formatFiles,
readWorkspaceConfiguration,
updateJson,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { jestInitGenerator } from '@nrwl/jest';
import { Linter } from '@nrwl/linter';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import { setDefaultCollection } from '@nrwl/workspace/src/utilities/set-default-collection';
import { E2eTestRunner, UnitTestRunner } from '../../utils/test-runners';
import {
angularVersion,
angularDevkitVersion,
jestPresetAngularVersion,
rxjsVersion,
} from '../../utils/versions';
import { karmaGenerator } from '../karma/karma';
import { Schema } from './schema';
export async function angularInitGenerator(
host: Tree,
rawOptions: Schema
): Promise<GeneratorCallback> {
const options = normalizeOptions(rawOptions);
setDefaults(host, options);
if (!options.skipPostInstall) {
addPostInstall(host);
}
const depsTask = !options.skipPackageJson
? updateDependencies(host)
: () => {};
const unitTestTask = addUnitTestRunner(host, options);
const e2eTask = addE2ETestRunner(host, options);
addGitIgnoreEntry(host, '.angular');
if (!options.skipFormat) {
await formatFiles(host);
}
return runTasksInSerial(depsTask, unitTestTask, e2eTask);
}
function normalizeOptions(options: Schema): Required<Schema> {
return {
e2eTestRunner: options.e2eTestRunner ?? E2eTestRunner.Cypress,
linter: options.linter ?? Linter.EsLint,
skipFormat: options.skipFormat ?? false,
skipInstall: options.skipInstall ?? false,
skipPostInstall: options.skipPostInstall ?? false,
skipPackageJson: options.skipPackageJson ?? false,
style: options.style ?? 'css',
unitTestRunner: options.unitTestRunner ?? UnitTestRunner.Jest,
};
}
function setDefaults(host: Tree, options: Schema) {
const workspace = readWorkspaceConfiguration(host);
workspace.generators = workspace.generators || {};
workspace.generators['@nrwl/angular:application'] = {
style: options.style,
linter: options.linter,
unitTestRunner: options.unitTestRunner,
e2eTestRunner: options.e2eTestRunner,
...(workspace.generators['@nrwl/angular:application'] || {}),
};
workspace.generators['@nrwl/angular:library'] = {
linter: options.linter,
unitTestRunner: options.unitTestRunner,
...(workspace.generators['@nrwl/angular:library'] || {}),
};
workspace.generators['@nrwl/angular:component'] = {
style: options.style,
...(workspace.generators['@nrwl/angular:component'] || {}),
};
updateWorkspaceConfiguration(host, workspace);
setDefaultCollection(host, '@nrwl/angular');
}
function addPostInstall(host: Tree) {
updateJson(host, 'package.json', (pkgJson) => {
pkgJson.scripts = pkgJson.scripts ?? {};
const command = 'ngcc --properties es2015 browser module main';
if (!pkgJson.scripts.postinstall) {
pkgJson.scripts.postinstall = command;
} else if (!pkgJson.scripts.postinstall.includes('ngcc')) {
pkgJson.scripts.postinstall = `${pkgJson.scripts.postinstall} && ${command}`;
}
return pkgJson;
});
}
function updateDependencies(host: Tree): GeneratorCallback {
return addDependenciesToPackageJson(
host,
{
'@angular/animations': angularVersion,
'@angular/common': angularVersion,
'@angular/compiler': angularVersion,
'@angular/core': angularVersion,
'@angular/forms': angularVersion,
'@angular/platform-browser': angularVersion,
'@angular/platform-browser-dynamic': angularVersion,
'@angular/router': angularVersion,
rxjs: rxjsVersion,
tslib: '^2.0.0',
'zone.js': '~0.11.4',
},
{
'@angular/cli': angularDevkitVersion,
'@angular/compiler-cli': angularVersion,
'@angular/language-service': angularVersion,
'@angular-devkit/build-angular': angularDevkitVersion,
}
);
}
function addUnitTestRunner(host: Tree, options: Schema): GeneratorCallback {
switch (options.unitTestRunner) {
case UnitTestRunner.Karma:
return karmaGenerator(host, { skipPackageJson: options.skipPackageJson });
case UnitTestRunner.Jest:
if (!options.skipPackageJson) {
addDependenciesToPackageJson(
host,
{},
{
'jest-preset-angular': jestPresetAngularVersion,
}
);
}
return jestInitGenerator(host, {
skipPackageJson: options.skipPackageJson,
});
default:
return () => {};
}
}
function addE2ETestRunner(host: Tree, options: Schema): GeneratorCallback {
switch (options.e2eTestRunner) {
case E2eTestRunner.Protractor:
return !options.skipPackageJson
? addDependenciesToPackageJson(
host,
{},
{
protractor: '~7.0.0',
'jasmine-core': '~3.6.0',
'jasmine-spec-reporter': '~5.0.0',
'ts-node': '~9.1.1',
'@types/jasmine': '~3.6.0',
'@types/jasminewd2': '~2.0.3',
}
)
: () => {};
case E2eTestRunner.Cypress:
return cypressInitGenerator(host, {
skipPackageJson: options.skipPackageJson,
});
default:
return () => {};
}
}
function addGitIgnoreEntry(host: Tree, entry: string) {
if (host.exists('.gitignore')) {
let content = host.read('.gitignore', 'utf-8');
if (/^\.angular$/gm.test(content)) {
return;
}
content = `${content}\n${entry}\n`;
host.write('.gitignore', content);
} else {
logger.warn(`Couldn't find .gitignore file to update`);
}
}
export default angularInitGenerator;