forked from nrwl/nx
/
init.ts
executable file
·150 lines (138 loc) · 4.34 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
import { cypressInitGenerator } from '@nrwl/cypress';
import type { GeneratorCallback, Tree } from '@nrwl/devkit';
import {
addDependenciesToPackageJson,
formatFiles,
readWorkspaceConfiguration,
updateJson,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { jestInitGenerator } from '@nrwl/jest';
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,
options: Schema
): Promise<GeneratorCallback> {
setDefaults(host, options);
addPostInstall(host);
const depsTask = updateDependencies(host);
const unitTestTask = addUnitTestRunner(host, options);
const e2eTask = addE2ETestRunner(host, options);
if (!options.skipFormat) {
await formatFiles(host);
}
return runTasksInSerial(depsTask, unitTestTask, e2eTask);
}
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'] = {
style: options.style,
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/compiler-cli': angularVersion,
'@angular/language-service': angularVersion,
'@angular-devkit/build-angular': angularDevkitVersion,
}
);
}
function addUnitTestRunner(
host: Tree,
options: Pick<Schema, 'unitTestRunner'>
): GeneratorCallback {
switch (options.unitTestRunner) {
case UnitTestRunner.Karma:
return karmaGenerator(host);
case UnitTestRunner.Jest:
addDependenciesToPackageJson(
host,
{},
{
'jest-preset-angular': jestPresetAngularVersion,
}
);
return jestInitGenerator(host, {});
default:
return () => {};
}
}
function addE2ETestRunner(
host: Tree,
options: Pick<Schema, 'e2eTestRunner'>
): GeneratorCallback {
switch (options.e2eTestRunner) {
case E2eTestRunner.Protractor:
return 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);
default:
return () => {};
}
}
export default angularInitGenerator;