/
update-project.ts
189 lines (167 loc) · 5.47 KB
/
update-project.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
189
import {
generateFiles,
getWorkspaceLayout,
joinPathFragments,
offsetFromRoot,
readProjectConfiguration,
Tree,
updateJson,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { replaceAppNameWithPath } from '@nrwl/workspace';
import { getRootTsConfigPathInTree } from '@nrwl/workspace/src/utilities/typescript';
import * as path from 'path';
import { NormalizedSchema } from './normalized-schema';
import { updateNgPackage } from './update-ng-package';
export async function updateProject(host: Tree, options: NormalizedSchema) {
createFiles(host, options);
updateProjectTsConfig(host, options);
fixProjectWorkspaceConfig(host, options);
updateNgPackage(host, options);
updateFiles(host, options);
}
function updateFiles(host: Tree, options: NormalizedSchema) {
const libRoot = `${options.projectRoot}/src/lib/`;
const serviceSpecPath = path.join(libRoot, `${options.name}.service.spec.ts`);
const componentSpecPath = path.join(
libRoot,
`${options.name}.component.spec.ts`
);
host.delete(path.join(libRoot, `${options.name}.service.ts`));
if (host.exists(serviceSpecPath)) {
host.delete(serviceSpecPath);
}
host.delete(path.join(libRoot, `${options.name}.component.ts`));
if (host.exists(componentSpecPath)) {
host.delete(path.join(libRoot, `${options.name}.component.spec.ts`));
}
if (!options.publishable && !options.buildable) {
host.delete(path.join(options.projectRoot, 'ng-package.json'));
host.delete(path.join(options.projectRoot, 'package.json'));
host.delete(path.join(options.projectRoot, 'tsconfig.lib.prod.json'));
host.delete(path.join(options.projectRoot, '.browserslistrc'));
}
host.delete(path.join(options.projectRoot, 'karma.conf.js'));
host.delete(path.join(options.projectRoot, 'src/test.ts'));
host.delete(path.join(options.projectRoot, 'tsconfig.spec.json'));
if (options.name !== options.fileName) {
host.delete(path.join(libRoot, `${options.name}.module.ts`));
}
if (!options.skipModule) {
host.write(
path.join(libRoot, `${options.fileName}.module.ts`),
`
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
]
})
export class ${options.moduleName} { }
`
);
if (options.unitTestRunner !== 'none' && options.addModuleSpec) {
host.write(
path.join(libRoot, `${options.fileName}.module.spec.ts`),
`
import { async, TestBed } from '@angular/core/testing';
import { ${options.moduleName} } from './${options.fileName}.module';
describe('${options.moduleName}', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ ${options.moduleName} ]
})
.compileComponents();
}));
// TODO: Add real tests here.
//
// NB: This particular test does not do anything useful.
// It does NOT check for correct instantiation of the module.
it('should have a module definition', () => {
expect(${options.moduleName}).toBeDefined();
});
});
`
);
}
} else {
host.delete(joinPathFragments(libRoot, `${options.fileName}.module.ts`));
}
host.write(
`${options.projectRoot}/src/index.ts`,
options.skipModule
? ``
: `
export * from './lib/${options.fileName}.module';
`
);
}
function createFiles(host: Tree, options: NormalizedSchema) {
generateFiles(
host,
path.join(__dirname, '../files/lib'),
options.projectRoot,
{
...options,
rootTsConfigPath: `${offsetFromRoot(
options.projectRoot
)}${getRootTsConfigPathInTree(host)}`,
tpl: '',
}
);
}
function fixProjectWorkspaceConfig(host: Tree, options: NormalizedSchema) {
const project = readProjectConfiguration(host, options.name);
project.tags = options.parsedTags;
const fixedProject = replaceAppNameWithPath(
project,
options.name,
options.projectRoot
);
if (!options.publishable && !options.buildable) {
delete fixedProject.targets.build;
} else {
// Set the right builder for the type of library.
// Ensure the outputs property comes after the builder for
// better readability.
const { executor, ...rest } = fixedProject.targets.build;
fixedProject.targets.build = {
executor: options.publishable
? '@nrwl/angular:package'
: '@nrwl/angular:ng-packagr-lite',
outputs: [
joinPathFragments(
'dist',
getWorkspaceLayout(host).libsDir,
options.projectDirectory
),
],
...rest,
};
}
delete fixedProject.targets.test;
updateProjectConfiguration(host, options.name, fixedProject);
}
function updateProjectTsConfig(host: Tree, options: NormalizedSchema) {
if (!host.exists(`${options.projectRoot}/tsconfig.lib.json`)) {
host.write(`${options.projectRoot}/tsconfig.lib.json`, '{}');
}
updateJson(host, `${options.projectRoot}/tsconfig.lib.json`, (json) => {
if (options.unitTestRunner === 'jest') {
json.exclude = ['src/test-setup.ts', '**/*.spec.ts'];
} else if (options.unitTestRunner === 'none') {
json.exclude = [];
} else {
json.exclude = json.exclude || [];
}
return {
...json,
extends: `./tsconfig.json`,
compilerOptions: {
...json.compilerOptions,
outDir: `${offsetFromRoot(options.projectRoot)}dist/out-tsc`,
},
};
});
}