-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
144 lines (122 loc) · 4.36 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
import {
Rule,
SchematicContext,
Tree,
chain,
schematic,
SchematicsException,
template,
mergeWith,
apply,
url,
move,
} from '@angular-devkit/schematics';
import { addProviderToModule } from '@schematics/angular/utility/ast-utils';
import { InsertChange } from '@schematics/angular/utility/change';
import { dasherize, classify } from '@angular-devkit/core/src/utils/strings';
import { dirname, Path } from '@angular-devkit/core';
import { addExtension } from '../utils';
import { getSourceFile } from '../ts-utils';
import { getNsConfigExtension } from '../generate/utils';
import { parseModuleInfo, ModuleInfo } from './module-info-utils';
import { Schema as MigrateComponentSchema } from '../migrate-component/schema';
import { Schema as CommonModuleSchema } from '../generate/common-module/schema';
import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema';
import { Schema as MigrateModuleSchema } from './schema';
let nsext: string;
let moduleInfo: ModuleInfo;
export default function(options: MigrateModuleSchema): Rule {
return chain([
(tree: Tree) => {
const nsconfigExtensions = getNsConfigExtension(tree);
nsext = options.nsext || nsconfigExtensions.ns;
if (!nsext.startsWith('.')) {
nsext = '.' + nsext;
}
},
(tree: Tree, context: SchematicContext) => {
moduleInfo = parseModuleInfo(options)(tree, context);
},
(tree) => {
const moduleDir = dirname(moduleInfo.modulePath as Path);
return addModuleFile(options.name, nsext, moduleDir)(tree);
},
(tree, context) => migrateComponents(moduleInfo, options)(tree, context),
migrateProviders(),
() => addCommonModuleFile(options, moduleInfo),
schematic<ConvertRelativeImportsSchema>('convert-relative-imports', options),
]);
}
const addCommonModuleFile = (options, modInfo) => {
const { name } = options;
const { modulePath } = modInfo;
const moduleDirectory = dirname(modulePath);
const commonModuleOptions = {
name,
path: moduleDirectory,
};
return schematic<CommonModuleSchema>('common-module', commonModuleOptions);
};
const addModuleFile =
(name: string, nsExtension: string, path: string) =>
(_tree: Tree) => {
const templateSource = apply(url('./_ns-files'), [
template({
name,
nsext: nsExtension,
dasherize,
classify,
}),
move(path),
]);
return mergeWith(templateSource);
};
const migrateComponents = (modInfo: ModuleInfo, options: MigrateModuleSchema) => {
const isComponent = (className: string) => className.endsWith('Component');
const components = modInfo.declarations.filter(({ name }) => isComponent(name));
return chain(
components.map((component) => {
const convertComponentOptions: MigrateComponentSchema = {
name: component.name,
modulePath: modInfo.modulePath,
nsext,
project: options.project,
style: options.style,
skipConvertRelativeImports: true,
};
return schematic<MigrateComponentSchema>('migrate-component', convertComponentOptions);
}),
);
};
const migrateProviders = () => (tree: Tree) => {
moduleInfo.providers.forEach((provider) => {
addProvider(provider.name, provider.importPath)(tree);
});
};
const addProvider = (providerClassName: string, providerPath: string) => (tree: Tree) => {
const nsModulePath = addExtension(moduleInfo.modulePath, nsext);
// check if the {N} version of the @NgModule exists
if (!tree.exists(nsModulePath)) {
throw new SchematicsException(
`Module file [${nsModulePath}] doesn't exist.` +
`Create it if you want the schematic to add ${moduleInfo.className} to its module providers,` +
`or if you just want to update the component without updating its module, ` +
`then rerun this command with --skip-module flag`,
);
}
// Get the changes required to update the @NgModule
const changes = addProviderToModule(
getSourceFile(tree, nsModulePath),
// nsModulePath, // <- this doesn't look like it is in use
'',
providerClassName,
providerPath,
// findRelativeImportPath(nsModulePath, providerPath)
);
// Save changes
const recorder = tree.beginUpdate(nsModulePath);
changes.forEach((change: InsertChange) =>
recorder.insertRight(change.pos, change.toAdd),
);
tree.commitUpdate(recorder);
};