/
update-config-files.ts
122 lines (108 loc) · 3.48 KB
/
update-config-files.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
import type { Tree } from '@nrwl/devkit';
import {
addProjectConfiguration,
getProjects,
offsetFromRoot,
readProjectConfiguration,
removeProjectConfiguration,
updateJson,
} from '@nrwl/devkit';
import { replaceAppNameWithPath } from '@nrwl/workspace/src/utils/cli-config-utils';
import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners';
import type { NormalizedSchema } from './normalized-schema';
export function updateConfigFiles(host: Tree, options: NormalizedSchema) {
updateTsConfigOptions(host, options);
updateAppAndE2EProjectConfigurations(host, options);
}
function updateTsConfigOptions(host: Tree, options: NormalizedSchema) {
// tsconfig.app.json
updateJson(host, `${options.appProjectRoot}/tsconfig.app.json`, (json) => ({
...json,
extends: './tsconfig.json',
compilerOptions: {
...json.compilerOptions,
outDir: `${offsetFromRoot(options.appProjectRoot)}dist/out-tsc`,
},
exclude: [
...new Set([
...(json.exclude || []),
'jest.config.ts',
'**/*.test.ts',
'**/*.spec.ts',
]),
],
}));
// tsconfig.json
updateJson(host, `${options.appProjectRoot}/tsconfig.json`, (json) => ({
...json,
compilerOptions: { ...json.compilerOptions, target: 'es2022' },
}));
}
function updateAppAndE2EProjectConfigurations(
host: Tree,
options: NormalizedSchema
) {
// workspace.json
let project = readProjectConfiguration(host, options.name);
if (options.ngCliSchematicAppRoot !== options.appProjectRoot) {
project = replaceAppNameWithPath(
project,
options.ngCliSchematicAppRoot,
options.appProjectRoot
);
// project already has the right root, but the above function, makes it incorrect.
// This corrects it.
project.root = options.appProjectRoot;
}
delete project.targets.test;
// Ensure the outputs property comes after the executor for
// better readability.
const { executor, ...rest } = project.targets.build;
project.targets.build = {
executor,
outputs: ['{options.outputPath}'],
...rest,
};
if (project.generators) {
delete project.generators;
}
if (options.port) {
project.targets.serve = {
...project.targets.serve,
options: {
...project.targets.serve.options,
port: options.port,
},
};
}
project.tags = options.parsedTags;
/**
* The "$schema" property on our configuration files is only added when the
* project configuration is added and not when updating it. It's done this
* way to avoid re-adding "$schema" when updating a project configuration
* and that property was intentionally removed by the devs.
*
* Since the project gets created by the Angular application schematic,
* the "$schema" property is not added, so we remove the project and add
* it back to workaround that.
*/
removeProjectConfiguration(host, options.name);
addProjectConfiguration(
host,
options.name,
project,
options.standaloneConfig
);
if (options.unitTestRunner === UnitTestRunner.None) {
host.delete(`${options.appProjectRoot}/src/app/app.component.spec.ts`);
host.delete(`${options.appProjectRoot}/tsconfig.spec.json`);
}
if (options.e2eTestRunner === E2eTestRunner.None) {
const projects = getProjects(host);
if (projects.has(options.e2eProjectName)) {
removeProjectConfiguration(host, options.e2eProjectName);
}
}
// delete some default test configs
host.delete(`${options.appProjectRoot}/karma.conf.js`);
}