-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.ts
243 lines (193 loc) · 6.66 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
import {
Rule,
Tree,
chain,
externalSchematic,
SchematicsException,
} from '@angular-devkit/schematics';
import { InsertChange } from '@schematics/angular/utility/change';
import { normalize } from '@angular-devkit/core';
import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils';
import { Schema as ModuleOptions } from './schema';
import {
getExtensions,
getSourceFile,
copy,
ns,
web,
removeNsSchemaOptions,
} from '../utils';
import {
removeImport,
removeMetadataArrayValue,
} from '../ast-utils';
import { dasherize } from '@angular-devkit/core/src/utils/strings';
interface Extensions {
web: string;
ns: string;
};
let originalExtensions;
let extensions: Extensions;
export default function (options: ModuleOptions): Rule {
return chain([
validateOptions(options),
externalSchematic('@schematics/angular', 'module', removeNsSchemaOptions(options)),
(tree: Tree) => fillExtensions(tree, options),
(tree: Tree) =>
web(tree, options) ? renameWebModules()(tree) : tree,
(tree: Tree) =>
ns(tree, options) ? performNsModifications(options)(tree) : tree,
]);
};
const renameWebModules = () =>
(tree: Tree) => {
const files = getFilesBasenames();
const originalToWeb = files.map(f => ({
from: getOriginalFile(f),
to: getWebFile(f),
}));
renameFiles(originalToWeb)(tree);
};
const performNsModifications = (options: ModuleOptions) =>
(tree: Tree) => {
const files = getFilesBasenames();
if (web(tree, options)) {
const webToNs = files.map(f => ({
from: getWebFile(f),
to: getNsFile(f),
}));
copyFiles(webToNs)(tree);
} else {
const originalToNs = files.map(f => ({
from: getOriginalFile(f),
to: getNsFile(f),
}));
renameFiles(originalToNs)(tree);
}
const moduleFile = getModuleBasename(options);
const nsModule = getNsFile(moduleFile);
addSchema(nsModule)(tree);
if (options.commonModule) {
ensureCommonModule(nsModule)(tree);
}
if (options.routing) {
const routingModule = getRoutingBasename(options);
const nsRoutingModule = getNsFile(routingModule);
ensureRouting(nsRoutingModule)(tree);
}
};
const validateOptions = (options) =>
(tree: Tree) => {
if (!web(tree, options) && !ns(tree, options)) {
throw new SchematicsException('You need to specify project type!');
}
return tree;
};
const dest = ({ name, sourceDir, path, flat }: ModuleOptions) =>
`${sourceDir}/${path}/${flat ? '' : dasherize(name)}`;
const getModuleBasename = (options: ModuleOptions) =>
normalize(`${dest(options)}/${options.name}.module`) as string;
const getRoutingBasename = (options: ModuleOptions) =>
normalize(`${dest(options)}/${options.name}-routing.module`) as string;
const fillExtensions = (tree: Tree, options: ModuleOptions) => {
extensions = getExtensions(tree, options);
originalExtensions = {
[getModuleBasename(options)]: '.ts',
};
if (options.routing) {
Object.assign(originalExtensions, {
[getRoutingBasename(options)]: '.ts',
});
}
};
const getFilesBasenames = () => Object.keys(originalExtensions);
const getOriginalFile = (basename: string) => {
return `${basename}${originalExtensions[basename]}`;
}
const getWebFile = (basename: string) =>
`${basename}${extensions.web}${originalExtensions[basename]}`;
const getNsFile = (basename: string) =>
`${basename}${extensions.ns}${originalExtensions[basename]}`;
const copyFiles = (paths: ({ from: string, to: string})[]) =>
(tree: Tree) =>
paths.forEach(({ from, to }) => copy(tree, from, to));
const renameFiles = (paths: ({ from: string, to: string })[]) =>
(tree: Tree) =>
paths.forEach(({ from, to }) => tree.rename(from, to));
const ensureCommonModule = (modulePath: string) =>
(tree: Tree) => {
addNSCommonModule(tree, modulePath);
removeNGCommonModule(tree, modulePath);
return tree;
};
const ensureRouting = (routingModulePath: string) =>
(tree: Tree) => {
removeNGRouterModule(tree, routingModulePath);
addNSRouterModule(tree, routingModulePath);
return tree;
};
const removeNGRouterModule = (tree: Tree, routingModulePath: string) => {
const moduleToRemove = 'RouterModule';
removeImport(tree, routingModulePath, moduleToRemove);
removeMetadataArrayValue(tree, routingModulePath, 'imports', `${moduleToRemove}.forChild(routes)`);
removeMetadataArrayValue(tree, routingModulePath, 'exports', moduleToRemove);
};
const addNSRouterModule = (tree: Tree, routingModulePath: string) => {
let moduleSource = getSourceFile(tree, routingModulePath);
const moduleName = 'NativeScriptRouterModule';
const addedImport = addSymbolToNgModuleMetadata(
moduleSource, routingModulePath,
'imports', `${moduleName}.forChild(routes)`,
'nativescript-angular/router'
);
const importRecorder = tree.beginUpdate(routingModulePath);
addedImport.forEach((change: InsertChange) =>
importRecorder.insertRight(change.pos, change.toAdd)
);
tree.commitUpdate(importRecorder);
// refetch new content after the update
moduleSource = getSourceFile(tree, routingModulePath);
const addedExport = addSymbolToNgModuleMetadata(
moduleSource, routingModulePath,
'exports', moduleName
);
const exportRecorder = tree.beginUpdate(routingModulePath);
addedExport.forEach((change: InsertChange) =>
exportRecorder.insertRight(change.pos, change.toAdd)
);
tree.commitUpdate(exportRecorder);
return tree;
};
const addSchema = (modulePath: string) =>
(tree: Tree) => {
const moduleSource = getSourceFile(tree, modulePath);
const recorder = tree.beginUpdate(modulePath);
const metadataChange = addSymbolToNgModuleMetadata(
moduleSource, modulePath,
'schemas', 'NO_ERRORS_SCHEMA',
'@angular/core');
metadataChange.forEach((change: InsertChange) =>
recorder.insertRight(change.pos, change.toAdd)
);
tree.commitUpdate(recorder);
return tree;
};
const removeNGCommonModule = (tree: Tree, modulePath: string) => {
const moduleName = 'CommonModule';
removeImport(tree, modulePath, moduleName);
removeMetadataArrayValue(tree, modulePath, 'imports', moduleName);
return tree;
};
const addNSCommonModule = (tree: Tree, modulePath: string) => {
const moduleSource = getSourceFile(tree, modulePath);
const recorder = tree.beginUpdate(modulePath);
const metadataChange = addSymbolToNgModuleMetadata(
moduleSource, modulePath,
'imports', 'NativeScriptCommonModule',
'nativescript-angular/common');
metadataChange.forEach((change: InsertChange) =>
recorder.insertRight(change.pos, change.toAdd)
);
tree.commitUpdate(recorder);
return tree;
};