|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -import { JsonAstObject } from '@angular-devkit/core'; |
9 | | -import { Rule, UpdateRecorder } from '@angular-devkit/schematics'; |
10 | | -import { getWorkspacePath } from '../../utility/config'; |
11 | | -import { findPropertyInAstObject } from '../../utility/json-utils'; |
12 | | -import { getWorkspace } from './utils'; |
| 8 | +import { json } from '@angular-devkit/core'; |
| 9 | +import { Rule } from '@angular-devkit/schematics'; |
| 10 | +import { updateWorkspace } from '../../utility/workspace'; |
13 | 11 |
|
14 | 12 | export default function(): Rule { |
15 | | - return tree => { |
16 | | - const workspacePath = getWorkspacePath(tree); |
17 | | - const workspace = getWorkspace(tree); |
18 | | - const recorder = tree.beginUpdate(workspacePath); |
19 | | - |
20 | | - const rootSchematics = findSchematicsField(workspace); |
21 | | - if (rootSchematics) { |
22 | | - updateSchematicsField(rootSchematics, recorder); |
23 | | - } |
24 | | - |
25 | | - const projects = findPropertyInAstObject(workspace, 'projects'); |
26 | | - if (!projects || projects.kind !== 'object' || !projects.properties) { |
27 | | - return; |
| 13 | + return updateWorkspace(workspace => { |
| 14 | + // Update root level schematics options if present |
| 15 | + const rootSchematics = workspace.extensions.schematics; |
| 16 | + if (rootSchematics && json.isJsonObject(rootSchematics)) { |
| 17 | + updateSchematicsField(rootSchematics); |
28 | 18 | } |
29 | 19 |
|
30 | | - for (const { value } of projects.properties) { |
31 | | - if (value.kind !== 'object') { |
32 | | - continue; |
| 20 | + // Update project level schematics options if present |
| 21 | + for (const [, project] of workspace.projects) { |
| 22 | + const projectSchematics = project.extensions.schematics; |
| 23 | + if (projectSchematics && json.isJsonObject(projectSchematics)) { |
| 24 | + updateSchematicsField(projectSchematics); |
33 | 25 | } |
34 | | - |
35 | | - const projectSchematics = findSchematicsField(value); |
36 | | - if (!projectSchematics) { |
37 | | - continue; |
38 | | - } |
39 | | - |
40 | | - updateSchematicsField(projectSchematics, recorder); |
41 | 26 | } |
42 | | - |
43 | | - tree.commitUpdate(recorder); |
44 | | - |
45 | | - return tree; |
46 | | - }; |
| 27 | + }); |
47 | 28 | } |
48 | 29 |
|
49 | | -function findSchematicsField(value: JsonAstObject): JsonAstObject | null { |
50 | | - const schematics = findPropertyInAstObject(value, 'schematics'); |
51 | | - if (schematics && schematics.kind == 'object') { |
52 | | - return schematics; |
53 | | - } |
54 | | - |
55 | | - return null; |
56 | | -} |
57 | | - |
58 | | -function updateSchematicsField(schematics: JsonAstObject, recorder: UpdateRecorder): void { |
59 | | - for (const { |
60 | | - key: { value: schematicName }, |
61 | | - value: schematicValue, |
62 | | - } of schematics.properties) { |
63 | | - if (schematicValue.kind !== 'object') { |
| 30 | +function updateSchematicsField(schematics: json.JsonObject): void { |
| 31 | + for (const [schematicName, schematicOptions] of Object.entries(schematics)) { |
| 32 | + if (!json.isJsonObject(schematicOptions)) { |
64 | 33 | continue; |
65 | 34 | } |
66 | 35 |
|
67 | 36 | if (!schematicName.startsWith('@schematics/angular:')) { |
68 | 37 | continue; |
69 | 38 | } |
70 | 39 |
|
71 | | - for (const { key: optionKey, value: optionValue } of schematicValue.properties) { |
72 | | - if (optionKey.value === 'styleext') { |
73 | | - // Rename `styleext` to `style |
74 | | - const offset = optionKey.start.offset + 1; |
75 | | - recorder.remove(offset, optionKey.value.length); |
76 | | - recorder.insertLeft(offset, 'style'); |
77 | | - } else if (optionKey.value === 'spec') { |
78 | | - // Rename `spec` to `skipTests` |
79 | | - const offset = optionKey.start.offset + 1; |
80 | | - recorder.remove(offset, optionKey.value.length); |
81 | | - recorder.insertLeft(offset, 'skipTests'); |
| 40 | + // Replace `styleext` with `style` |
| 41 | + if (schematicOptions.styleext !== undefined) { |
| 42 | + schematicOptions.style = schematicOptions.styleext; |
| 43 | + delete schematicOptions.styleext; |
| 44 | + } |
82 | 45 |
|
83 | | - // invert value |
84 | | - const { start, end } = optionValue; |
85 | | - recorder.remove(start.offset, end.offset - start.offset); |
86 | | - recorder.insertLeft(start.offset, `${!optionValue.value}`); |
87 | | - } |
| 46 | + // Replace `spec` with `skipTests` |
| 47 | + if (schematicOptions.spec !== undefined) { |
| 48 | + // skipTests value is inverted |
| 49 | + schematicOptions.skipTests = !schematicOptions.spec; |
| 50 | + delete schematicOptions.spec; |
88 | 51 | } |
89 | 52 | } |
90 | 53 | } |
0 commit comments