Skip to content

Commit 9886e27

Browse files
clydinmgechev
authored andcommitted
refactor(@schematics/angular): use new workspace helpers in schematic-options migration
1 parent 99d2859 commit 9886e27

File tree

1 file changed

+27
-64
lines changed

1 file changed

+27
-64
lines changed

packages/schematics/angular/migrations/update-9/schematic-options.ts

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,49 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
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';
1311

1412
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);
2818
}
2919

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);
3325
}
34-
35-
const projectSchematics = findSchematicsField(value);
36-
if (!projectSchematics) {
37-
continue;
38-
}
39-
40-
updateSchematicsField(projectSchematics, recorder);
4126
}
42-
43-
tree.commitUpdate(recorder);
44-
45-
return tree;
46-
};
27+
});
4728
}
4829

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)) {
6433
continue;
6534
}
6635

6736
if (!schematicName.startsWith('@schematics/angular:')) {
6837
continue;
6938
}
7039

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+
}
8245

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;
8851
}
8952
}
9053
}

0 commit comments

Comments
 (0)