-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.ts
385 lines (306 loc) · 10.9 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { join, dirname } from 'path';
import {
Rule,
SchematicContext,
Tree,
chain,
SchematicsException,
apply,
url,
template,
mergeWith,
schematic,
noop,
move,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { dasherize } from '@angular-devkit/core/src/utils/strings';
import { Schema as MigrationOptions } from './schema';
import { getJsonFile, getFileContents, getPackageJson, overwritePackageJson } from '../utils';
import { getAngularProjectSettings, AngularProjectSettings } from '../angular-project-parser';
import { Extensions } from '../generate/utils';
import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema';
import { getCompilerOptions } from '../ts-utils';
import { getMappedImportsRuleConfig } from '../mapped-imports-rule-utils';
let extensions: Extensions;
let projectSettings: AngularProjectSettings;
export default function(options: MigrationOptions): Rule {
extensions = {
ns: (options.nsExtension.length > 0) ? '.' + options.nsExtension : '',
web: (options.webExtension.length > 0) ? '.' + options.webExtension : '',
};
return chain([
validateOptions(options),
getProjectSettings(options.project),
addNativeScriptSchematics,
addNsFiles(options),
options.sample ?
addSampleFiles() :
noop(),
addAppResources(),
mergeGitIgnore,
addRunScriptsToPackageJson,
// addNativeScriptProjectId,
modifyWebTsconfig,
modifyTsLintConfig,
options.skipAutoGeneratedComponent ?
noop() :
addSampleComponent(options.nsExtension, options.webExtension, options.project),
addDependencies(),
schematic<ConvertRelativeImportsSchema>('convert-relative-imports', options),
options.skipInstall ?
noop() :
(_tree: Tree, context: SchematicContext) => {
context.addTask(new NodePackageInstallTask());
},
]);
}
/**
* Make sure that nsExtension != webExtension
*/
const validateOptions = (options: MigrationOptions) => () => {
if (options.nsExtension === options.webExtension) {
throw new SchematicsException(
`nsExtension "${options.nsExtension}" and webExtension "${options.webExtension}" should have different values`,
);
}
};
const getProjectSettings = (projectName: string) => async (tree: Tree, context: SchematicContext) => {
context.logger.info('Reading Project Settings');
projectSettings = await getAngularProjectSettings(tree, projectName);
context.logger.info(`Project settings:
${JSON.stringify(projectSettings, null, 2)}`);
};
const addNativeScriptSchematics = (tree: Tree, context: SchematicContext) => {
context.logger.info('Adding @nativescript/schematics to angular.json');
const angularJson: any = getJsonFile(tree, 'angular.json');
const defaultCollection = '@nativescript/schematics';
if (angularJson.cli && angularJson.cli.defaultCollection !== defaultCollection) {
context.logger.warn(`Changing default schematics collection
${JSON.stringify(angularJson.cli, null, 2)}
to:
${JSON.stringify(angularJson.cli, null, 2)}`);
}
angularJson.cli = { defaultCollection };
tree.overwrite('angular.json', JSON.stringify(angularJson, null, 2));
};
const addNsFiles = (options: MigrationOptions) => (_tree: Tree, context: SchematicContext) => {
context.logger.info('Adding {N} files');
const templateOptions = {
sample: options.sample,
skipAutoGeneratedComponent: options.skipAutoGeneratedComponent,
theme: true,
dasherize,
nsext: extensions.ns,
webext: extensions.web,
sourceDir: projectSettings.sourceRoot,
prefix: projectSettings.prefix,
main: projectSettings.mainName,
entryModuleClassName: projectSettings.entryModuleClassName,
entryModuleName: projectSettings.entryModuleName,
entryModuleImportPath: projectSettings.entryModuleImportPath,
entryComponentClassName: projectSettings.entryComponentClassName,
entryComponentName: projectSettings.entryComponentName,
entryComponentImportPath: projectSettings.entryComponentImportPath,
indexAppRootTag: projectSettings.indexAppRootTag,
};
const templateSource = apply(url('./_ns-files'), [
template(templateOptions),
]);
return mergeWith(templateSource);
};
const addSampleFiles = () => (_tree: Tree, context: SchematicContext) => {
context.logger.info('Adding sample files');
const templateOptions = {
nsext: extensions.ns,
webext: extensions.web,
sourceDir: projectSettings.sourceRoot,
indexAppRootTag: projectSettings.indexAppRootTag,
prefix: projectSettings.prefix,
};
const path = join(projectSettings.sourceRoot, 'app');
const templateSource = apply(url('./_sample-files'), [
template(templateOptions),
move(path),
]);
return mergeWith(templateSource);
};
const addSampleComponent = (nsExtension: string, webExtension: string, project: string) =>
(_tree, context: SchematicContext) => {
context.logger.info('Adding Sample Shared Component');
return schematic('component', {
nsExtension,
webExtension,
web: true,
nativescript: true,
name: 'auto-generated',
module: 'app',
prefix: projectSettings.prefix,
spec: false,
project,
});
};
const addAppResources = () => (_tree: Tree, context: SchematicContext) => {
context.logger.info('Adding App_Resources');
return schematic('app-resources', {
path: '',
});
};
/**
* Adds NativeScript specific ignores to .gitignore
*/
const mergeGitIgnore = (tree: Tree, context: SchematicContext) => {
context.logger.info('Adding NativeScript specific exclusions to .gitignore');
// Read existing .gitignore file
const GITIGNORE = '.gitignore';
if (!tree.exists(GITIGNORE)) {
tree.create(GITIGNORE, '');
}
const gitignore = getFileContents(tree, `/${GITIGNORE}`).split('\n');
// Prepare {N} ignore items
const nsGitignoreItems = [
'node_modules/',
'platforms/',
'hooks/',
`${projectSettings.sourceRoot}/**/*.js`,
].filter((line) => !gitignore.includes(line));
const nsGitignoreContent =
`# NativeScript` +
nsGitignoreItems.join('\n') +
'\n';
// Update .gitignore
const recorder = tree.beginUpdate(GITIGNORE);
recorder.insertLeft(0, nsGitignoreContent);
tree.commitUpdate(recorder);
};
const addRunScriptsToPackageJson = (tree: Tree, context: SchematicContext) => {
context.logger.info('Adding NativeScript run scripts to package.json');
const packageJson = getPackageJson(tree);
const scriptsToAdd = {
android: 'ns run android --no-hmr',
ios: 'ns run ios --no-hmr',
mobile: 'ns run',
// preview: 'ns preview',
// ngcc: 'ngcc --properties es2015 module main --first-only',
// postinstall: 'npm run ngcc',
};
packageJson.scripts = {...scriptsToAdd, ...packageJson.scripts};
overwritePackageJson(tree, packageJson);
};
const addNativeScriptProjectId = (tree: Tree, context: SchematicContext) => {
context.logger.info('Adding NativeScript Project ID to package.json');
const packageJson: any = getJsonFile(tree, 'package.json');
packageJson.nativescript = packageJson.nativescript || {};
packageJson.nativescript = {
id: 'org.nativescript.ngsample', ...packageJson.nativescript};
tree.overwrite('package.json', JSON.stringify(packageJson, null, 2));
};
const modifyTsLintConfig = (tree: Tree, context: SchematicContext) => {
context.logger.info('Modifying tslint.json');
const tsLintConfigPath = 'tslint.json';
let tsLintConfig;
try {
tsLintConfig = getJsonFile(tree, tsLintConfigPath);
} catch (e) {
context.logger.warn('Failed to update tslint.json.');
context.logger.debug(e.message);
return;
}
tsLintConfig.extends = tsLintConfig.extends || [];
if (typeof tsLintConfig.extends === 'string') {
tsLintConfig.extends = [
tsLintConfig.extends,
];
}
tsLintConfig.extends.push('@nativescript/tslint-rules');
tsLintConfig.rules = tsLintConfig.rules || {};
const ruleConfig = getRuleConfig(tree);
if (!ruleConfig) {
context.logger.warn('Failed to update tslint.json.');
context.logger.debug('Failed to construct tslint rule configuration.');
return;
}
const { name, options } = ruleConfig;
tsLintConfig.rules[name] = options;
tree.overwrite(tsLintConfigPath, JSON.stringify(tsLintConfig, null, 2));
};
const getRuleConfig = (tree: Tree) => {
const tsConfigPath = projectSettings.tsConfig || 'tsconfig.json';
const compilerOptions = getCompilerOptions(tree, tsConfigPath);
if (!compilerOptions) {
return;
}
const ruleConfig = getMappedImportsRuleConfig(compilerOptions);
return ruleConfig;
};
/**
* Add web-specific path mappings and files
*/
const modifyWebTsconfig = (tree: Tree, context: SchematicContext) => {
context.logger.info('Modifying web tsconfig');
const tsConfigPath = projectSettings.tsConfig;
const tsConfig: any = getJsonFile(tree, tsConfigPath);
const srcDir = projectSettings.sourceRoot;
// add list of entry "files"
const defaultFiles = [
`${srcDir}/main.ts`,
`${srcDir}/polyfills.ts`,
];
tsConfig.files = tsConfig.files || [];
tsConfig.files.push(...defaultFiles);
// remove "include" property
// because it overrides "files"
delete tsConfig.include;
// paths
const webPaths = {
'@src/*': [
`${srcDir}/*.web`,
`${srcDir}/*`],
};
tsConfig.compilerOptions = tsConfig.compilerOptions || {};
tsConfig.compilerOptions.paths = {
...tsConfig.compilerOptions.paths,
...webPaths,
};
tree.overwrite(tsConfigPath, JSON.stringify(tsConfig, null, 2));
if (!tsConfig.extends) {
return;
}
const baseTsConfigPath = join(dirname(tsConfigPath), tsConfig.extends);
const baseTsConfig: any = getJsonFile(tree, baseTsConfigPath);
const basePaths = {
'@src/*': [
`${srcDir}/*.android.ts`,
`${srcDir}/*.ios.ts`,
`${srcDir}/*.tns.ts`,
`${srcDir}/*.web.ts`,
`${srcDir}/*`],
};
baseTsConfig.compilerOptions = baseTsConfig.compilerOptions || {};
baseTsConfig.compilerOptions.paths = {
...baseTsConfig.compilerOptions.paths,
...basePaths,
};
tree.overwrite(baseTsConfigPath, JSON.stringify(baseTsConfig, null, 2));
};
const addDependencies = () => (tree: Tree, context: SchematicContext) => {
context.logger.info('Adding npm dependencies');
const packageJson = getPackageJson(tree);
// add {N} 7 main key
(<any>packageJson).main = 'main.tns.js';
// @UPGRADE: Update all versions whenever {N} version updates
const depsToAdd = {
'@nativescript/angular': '~10.1.0',
'@nativescript/core': '~7.0.0',
'@nativescript/theme': '~2.5.0',
'reflect-metadata': '~0.1.12',
tslib: '1.10.0',
};
packageJson.dependencies = {...depsToAdd, ...packageJson.dependencies};
const devDepsToAdd = {
'@nativescript/webpack': '~3.0.0',
'@nativescript/tslint-rules': '~0.0.5',
};
packageJson.devDependencies = {...devDepsToAdd, ...packageJson.devDependencies};
overwritePackageJson(tree, packageJson);
};