-
Notifications
You must be signed in to change notification settings - Fork 52
/
xplat.ts
363 lines (341 loc) · 9.66 KB
/
xplat.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
import {
Tree,
noop,
branchAndMerge,
mergeWith,
apply,
url,
template,
move,
SchematicContext,
Rule,
SchematicsException
} from '@angular-devkit/schematics';
import { createSourceFile, ScriptTarget } from 'typescript';
import {
getDefaultTemplateOptions,
getPrefix,
updateJsonFile,
getJsonFromFile,
stringUtils,
PlatformTypes,
supportedSandboxPlatforms,
supportedPlatforms
} from './general';
import { updateJsonInTree } from '@nrwl/workspace';
import { insert, addGlobal } from './ast';
import { platformAppPrefixError, generatorError, optionsMissingError } from './errors';
export interface IXplatSchema {
/**
* npm scope - auto detected from nx.json but can specify your own name
*/
npmScope?: string;
/**
* The prefix to apply to generated selectors.
*/
prefix?: string;
/**
* Only if not present yet
*/
onlyIfNone?: boolean;
/**
* Skip formatting
*/
skipFormat?: boolean;
}
export function addPlatformFiles(
tree: Tree,
options: IXplatSchema,
platform: string
) {
if (tree.exists(`xplat/${platform}/core/index.ts`)) {
return noop();
}
return branchAndMerge(
mergeWith(
apply(url(`./_files`), [
template({
...(options as any),
...getDefaultTemplateOptions()
}),
move(`xplat/${platform}`)
])
)
);
}
export function addLibFiles(
tree: Tree,
options: IXplatSchema
) {
if (
tree.exists(`libs/core/base/base-component.ts`) ||
tree.exists(`libs/features/index.ts`)
) {
return noop();
}
return branchAndMerge(
mergeWith(
apply(url(`./_lib_files`), [
template({
...(options as any),
...getDefaultTemplateOptions()
}),
move('libs')
])
)
);
}
export function updateTestingConfig(tree: Tree, context: SchematicContext) {
const angularConfigPath = `angular.json`;
const nxConfigPath = `nx.json`;
const angularJson = getJsonFromFile(tree, angularConfigPath);
const nxJson = getJsonFromFile(tree, nxConfigPath);
const prefix = getPrefix();
// console.log('prefix:', prefix);
// update libs and xplat config
if (angularJson && angularJson.projects) {
angularJson.projects['libs'] = {
root: 'libs',
sourceRoot: 'libs',
projectType: 'library',
prefix: prefix,
architect: {
test: {
builder: '@angular-devkit/build-angular:karma',
options: {
main: 'testing/test.libs.ts',
tsConfig: 'testing/tsconfig.libs.spec.json',
karmaConfig: 'testing/karma.conf.js'
}
},
lint: {
builder: '@angular-devkit/build-angular:tslint',
options: {
tsConfig: [
'testing/tsconfig.libs.json',
'testing/tsconfig.libs.spec.json'
],
exclude: ['**/node_modules/**']
}
}
}
};
angularJson.projects['xplat'] = {
root: 'xplat',
sourceRoot: 'xplat',
projectType: 'library',
prefix: prefix,
architect: {
test: {
builder: '@angular-devkit/build-angular:karma',
options: {
main: 'testing/test.xplat.ts',
tsConfig: 'testing/tsconfig.xplat.spec.json',
karmaConfig: 'testing/karma.conf.js'
}
},
lint: {
builder: '@angular-devkit/build-angular:tslint',
options: {
tsConfig: [
'testing/tsconfig.xplat.json',
'testing/tsconfig.xplat.spec.json'
],
exclude: ['**/node_modules/**']
}
}
}
};
}
if (nxJson && nxJson.projects) {
nxJson.projects['libs'] = {
tags: []
};
nxJson.projects['xplat'] = {
tags: []
};
}
tree = updateJsonFile(tree, angularConfigPath, angularJson);
tree = updateJsonFile(tree, nxConfigPath, nxJson);
return tree;
}
export function updateLint(host: Tree, context: SchematicContext) {
const prefix = getPrefix();
return updateJsonInTree('tslint.json', json => {
json.rules = json.rules || {};
// remove forin rule as collides with LogService
delete json.rules['forin'];
// adjust console rules to work with LogService
json.rules['no-console'] = [true, 'debug', 'time', 'timeEnd', 'trace'];
json.rules['directive-selector'] = [true, 'attribute', prefix, 'camelCase'];
json.rules['component-selector'] = [true, 'element', prefix, 'kebab-case'];
return json;
})(host, context);
}
export namespace FeatureHelpers {
export interface Schema {
name: string;
/**
* Target apps
*/
projects?: string;
/**
* Target platforms
*/
platforms?: string;
/**
* Only generate for specified projects and ignore shared code
*/
onlyProject?: boolean;
/**
* Only generate the module and ignore default component creation
*/
onlyModule?: boolean;
/**
* Configure routing
*/
routing?: boolean;
/**
* Create base component for maximum code sharing
*/
createBase?: boolean;
/**
* Add link to route for sandbox
*/
adjustSandbox?: boolean;
/**
* Skip formatting
*/
skipFormat?: boolean;
}
export function prepare(options: Schema): { featureName: string; projectNames: Array<string>; platforms: Array<PlatformTypes>} {
if (!options.name) {
throw new SchematicsException(
`You did not specify the name of the feature you'd like to generate. For example: ng g feature my-feature`
);
}
const featureName = options.name.toLowerCase();
let projects = options.projects;
let projectNames: Array<string>;
let platforms = [];
if (options.adjustSandbox) {
// when adjusting sandbox for the feature, turn dependent options on
// for convenience also setup some default fallbacks to avoid requiring so many options
// sandbox flags are meant to be quick and convenient
options.onlyProject = true;
options.routing = true;
if (!projects) {
if (!options.platforms) {
// default to {N} sandbox
projects = 'nativescript-sandbox';
} else {
platforms = options.platforms.split(',');
const projectSandboxNames = [];
// default to project with sandbox name
for (const p of platforms) {
if (supportedSandboxPlatforms.includes(p)) {
projectSandboxNames.push(`${p}-sandbox`);
} else {
throw new SchematicsException(
`The --adjustSandbox flag supports the following at the moment: ${supportedSandboxPlatforms}`
);
}
}
projects = projectSandboxNames.join(',');
}
}
}
if (options.routing && !options.onlyProject) {
throw new SchematicsException(
`When generating a feature with the --routing option, please also specify --onlyProject. Support for shared code routing is under development.`
);
}
if (projects) {
// building feature in shared code and in projects
projectNames = projects.split(',');
for (const name of projectNames) {
const platPrefix = name.split('-')[0];
if (
supportedPlatforms.includes(platPrefix) &&
!platforms.includes(platPrefix)
) {
// if project name is prefixed with supported platform and not already added
platforms.push(platPrefix);
}
}
} else if (options.platforms) {
// building feature in shared code only
platforms = options.platforms.split(',');
}
if (platforms.length === 0) {
let error = projects ? platformAppPrefixError() : generatorError('feature');
throw new SchematicsException(optionsMissingError(error));
}
return { featureName, projectNames, platforms };
}
export function addFiles(
options: Schema,
target: string = '',
projectName: string = '',
extra: string = ''
) {
let moveTo: string;
if (target) {
moveTo = getMoveTo(options, target, projectName);
} else {
target = 'lib';
moveTo = `libs/features/${options.name.toLowerCase()}`;
}
return branchAndMerge(
mergeWith(
apply(url(`./${extra}_files`), [
template(getTemplateOptions(options)),
move(moveTo)
])
)
);
};
export function adjustBarrelIndex(options: Schema, indexFilePath: string): Rule {
return (host: Tree) => {
const indexSource = host.read(indexFilePath)!.toString('utf-8');
const indexSourceFile = createSourceFile(
indexFilePath,
indexSource,
ScriptTarget.Latest,
true
);
insert(host, indexFilePath, [
...addGlobal(
indexSourceFile,
indexFilePath,
`export * from './${options.name.toLowerCase()}';`,
true
)
]);
return host;
};
}
export function getTemplateOptions(options: Schema) {
const nameParts = options.name.split('-');
let endingDashName = nameParts[0];
if (nameParts.length > 1) {
endingDashName = stringUtils.capitalize(nameParts[nameParts.length - 1]);
}
return {
...(options as any),
...getDefaultTemplateOptions(),
name: options.name.toLowerCase(),
endingDashName
};
}
export function getMoveTo(options: Schema, platform: string, projectName?: string) {
const featureName = options.name.toLowerCase();
let moveTo = `xplat/${platform}/features/${featureName}`;
if (projectName) {
let appDir = platform === 'web' ? '/app' : '';
moveTo = `apps/${projectName}/src${appDir}/features/${featureName}`;
// console.log('moveTo:', moveTo);
}
return moveTo;
}
}