Skip to content

Commit 7c9a245

Browse files
authored
feat(): update schematics to support ng10 (#251)
1 parent 48d566f commit 7c9a245

File tree

2 files changed

+79
-37
lines changed

2 files changed

+79
-37
lines changed

schematics/component/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { strings } from '@angular-devkit/core';
2+
import { ProjectDefinition } from '@angular-devkit/core/src/workspace';
23
import { Rule, SchematicsException, Tree, apply, branchAndMerge, chain, filter, mergeWith, move, noop, template, url } from '@angular-devkit/schematics';
34
import { buildRelativePath } from '@schematics/angular/utility/find-module';
45
import { parseName } from '@schematics/angular/utility/parse-name';
5-
import { buildDefaultPath, getProject } from '@schematics/angular/utility/project';
66
import { validateHtmlSelector, validateName } from '@schematics/angular/utility/validation';
7+
import { buildDefaultPath, getWorkspace } from '@schematics/angular/utility/workspace';
78
import * as ts from 'typescript';
89

910
import { buildSelector } from '../util';
@@ -123,21 +124,22 @@ function addImportToImports(host: Tree, options: ComponentOptions): void {
123124
}
124125

125126
export default function(options: ComponentOptions): Rule {
126-
return (host, context) => {
127+
return async (host: Tree) => {
127128
if (!options.project) {
128129
throw new SchematicsException('Option (project) is required.');
129130
}
130131

131-
const project = getProject(host, options.project);
132+
const workspace = await getWorkspace(host);
133+
const project = workspace.projects.get(options.project);
132134

133135
if (options.path === undefined) {
134-
options.path = buildDefaultPath(project);
136+
options.path = buildDefaultPath(project as ProjectDefinition);
135137
}
136138

137139
const parsedPath = parseName(options.path, options.name);
138140
options.name = parsedPath.name;
139141
options.path = parsedPath.path;
140-
options.selector = options.selector ? options.selector : buildSelector(options, project.prefix);
142+
options.selector = options.selector ? options.selector : buildSelector(options, project?.prefix ?? 'app');
141143

142144
validateName(options.name);
143145
validateHtmlSelector(options.selector);
@@ -158,6 +160,6 @@ export default function(options: ComponentOptions): Rule {
158160
addImportToNgModule(options),
159161
mergeWith(templateSource),
160162
])),
161-
])(host, context);
163+
]);
162164
};
163165
}

schematics/page/index.ts

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Path, join, normalize, strings } from '@angular-devkit/core';
2+
import { ProjectDefinition } from '@angular-devkit/core/src/workspace';
23
import { DirEntry, Rule, SchematicsException, Tree, apply, branchAndMerge, chain, filter, mergeWith, move, noop, template, url } from '@angular-devkit/schematics';
34
import { ModuleOptions, buildRelativePath } from '@schematics/angular/utility/find-module';
45
import { parseName } from '@schematics/angular/utility/parse-name';
5-
import { buildDefaultPath, getProject } from '@schematics/angular/utility/project';
66
import { validateHtmlSelector, validateName } from '@schematics/angular/utility/validation';
7+
import { buildDefaultPath, getWorkspace } from '@schematics/angular/utility/workspace';
78
import * as ts from 'typescript';
89

910
import { buildSelector } from '../util';
@@ -12,19 +13,22 @@ import { Change, InsertChange } from '../util/change';
1213

1314
import { Schema as PageOptions } from './schema';
1415

15-
function findRoutingModuleFromOptions(host: Tree, options: ModuleOptions): Path | undefined {
16+
function findRoutingModuleFromOptions(
17+
host: Tree,
18+
options: ModuleOptions
19+
): Path | undefined {
1620
if (options.hasOwnProperty('skipImport') && options.skipImport) {
1721
return undefined;
1822
}
1923

2024
if (!options.module) {
21-
const pathToCheck = (options.path || '')
22-
+ (options.flat ? '' : '/' + strings.dasherize(options.name));
25+
const pathToCheck =
26+
(options.path || '') +
27+
(options.flat ? '' : '/' + strings.dasherize(options.name));
2328

2429
return normalize(findRoutingModule(host, pathToCheck));
2530
} else {
26-
const modulePath = normalize(
27-
'/' + (options.path) + '/' + options.module);
31+
const modulePath = normalize('/' + options.path + '/' + options.module);
2832
const moduleBaseName = normalize(modulePath).split('/').pop();
2933

3034
if (host.exists(modulePath)) {
@@ -52,15 +56,19 @@ function findRoutingModule(host: Tree, generateDir: string): Path {
5256
if (matches.length === 1) {
5357
return join(dir.path, matches[0]);
5458
} else if (matches.length > 1) {
55-
throw new Error('More than one module matches. Use skip-import option to skip importing '
56-
+ 'the component into the closest module.');
59+
throw new Error(
60+
'More than one module matches. Use skip-import option to skip importing ' +
61+
'the component into the closest module.'
62+
);
5763
}
5864

5965
dir = dir.parent;
6066
}
6167

62-
throw new Error('Could not find an NgModule. Use the skip-import '
63-
+ 'option to skip importing in NgModule.');
68+
throw new Error(
69+
'Could not find an NgModule. Use the skip-import ' +
70+
'option to skip importing in NgModule.'
71+
);
6472
}
6573

6674
function addRouteToNgModule(options: PageOptions): Rule {
@@ -78,19 +86,31 @@ function addRouteToNgModule(options: PageOptions): Rule {
7886
}
7987

8088
const sourceText = text.toString('utf8');
81-
const source = ts.createSourceFile(module, sourceText, ts.ScriptTarget.Latest, true);
89+
const source = ts.createSourceFile(
90+
module,
91+
sourceText,
92+
ts.ScriptTarget.Latest,
93+
true
94+
);
8295

83-
const pagePath = (
96+
const pagePath =
8497
`/${options.path}/` +
8598
(options.flat ? '' : `${strings.dasherize(options.name)}/`) +
86-
`${strings.dasherize(options.name)}.module`
87-
);
99+
`${strings.dasherize(options.name)}.module`;
88100

89101
const relativePath = buildRelativePath(module, pagePath);
90102

91-
const routePath = strings.dasherize(options.routePath ? options.routePath : options.name);
103+
const routePath = strings.dasherize(
104+
options.routePath ? options.routePath : options.name
105+
);
92106
const ngModuleName = `${strings.classify(options.name)}PageModule`;
93-
const changes = addRouteToRoutesArray(source, module, routePath, relativePath, ngModuleName);
107+
const changes = addRouteToRoutesArray(
108+
source,
109+
module,
110+
routePath,
111+
relativePath,
112+
ngModuleName
113+
);
94114
const recorder = host.beginUpdate(module);
95115

96116
for (const change of changes) {
@@ -105,14 +125,24 @@ function addRouteToNgModule(options: PageOptions): Rule {
105125
};
106126
}
107127

108-
function addRouteToRoutesArray(source: ts.SourceFile, ngModulePath: string, routePath: string, routeLoadChildren: string, ngModuleName: string): Change[] {
128+
function addRouteToRoutesArray(
129+
source: ts.SourceFile,
130+
ngModulePath: string,
131+
routePath: string,
132+
routeLoadChildren: string,
133+
ngModuleName: string
134+
): Change[] {
109135
const keywords = findNodes(source, ts.SyntaxKind.VariableStatement);
110136

111137
for (const keyword of keywords) {
112138
if (ts.isVariableStatement(keyword)) {
113-
const [ declaration ] = keyword.declarationList.declarations;
139+
const [declaration] = keyword.declarationList.declarations;
114140

115-
if (ts.isVariableDeclaration(declaration) && declaration.initializer && declaration.name.getText() === 'routes') {
141+
if (
142+
ts.isVariableDeclaration(declaration) &&
143+
declaration.initializer &&
144+
declaration.name.getText() === 'routes'
145+
) {
116146
const node = declaration.initializer.getChildAt(1);
117147
const lastRouteNode = node.getLastToken();
118148

@@ -126,10 +156,20 @@ function addRouteToRoutesArray(source: ts.SourceFile, ngModulePath: string, rout
126156
if (lastRouteNode.kind === ts.SyntaxKind.CommaToken) {
127157
trailingCommaFound = true;
128158
} else {
129-
changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd(), ','));
159+
changes.push(
160+
new InsertChange(ngModulePath, lastRouteNode.getEnd(), ',')
161+
);
130162
}
131163

132-
changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd() + 1, ` {\n path: '${routePath}',\n loadChildren: () => import('${routeLoadChildren}').then( m => m.${ngModuleName})\n }${trailingCommaFound ? ',' : ''}\n`));
164+
changes.push(
165+
new InsertChange(
166+
ngModulePath,
167+
lastRouteNode.getEnd() + 1,
168+
` {\n path: '${routePath}',\n loadChildren: () => import('${routeLoadChildren}').then( m => m.${ngModuleName})\n }${
169+
trailingCommaFound ? ',' : ''
170+
}\n`
171+
)
172+
);
133173

134174
return changes;
135175
}
@@ -140,23 +180,24 @@ function addRouteToRoutesArray(source: ts.SourceFile, ngModulePath: string, rout
140180
}
141181

142182
export default function(options: PageOptions): Rule {
143-
return (host, context) => {
183+
return async (host: Tree) => {
144184
if (!options.project) {
145185
throw new SchematicsException('Option (project) is required.');
146186
}
147187

148-
const project = getProject(host, options.project);
188+
const workspace = await getWorkspace(host);
189+
const project = workspace.projects.get(options.project);
149190

150191
if (options.path === undefined) {
151-
options.path = buildDefaultPath(project);
192+
options.path = buildDefaultPath(project as ProjectDefinition);
152193
}
153194

154195
options.module = findRoutingModuleFromOptions(host, options);
155196

156197
const parsedPath = parseName(options.path, options.name);
157198
options.name = parsedPath.name;
158199
options.path = parsedPath.path;
159-
options.selector = options.selector ? options.selector : buildSelector(options, project.prefix);
200+
options.selector = options.selector ? options.selector : buildSelector(options, project?.prefix ?? 'app');
160201

161202
validateName(options.name);
162203
validateHtmlSelector(options.selector);
@@ -165,17 +206,16 @@ export default function(options: PageOptions): Rule {
165206
options.spec ? noop() : filter(p => !p.endsWith('.spec.ts')),
166207
template({
167208
...strings,
168-
'if-flat': (s: string) => options.flat ? '' : s,
209+
'if-flat': (s: string) => (options.flat ? '' : s),
169210
...options,
170211
}),
171212
move(parsedPath.path),
172213
]);
173214

174215
return chain([
175-
branchAndMerge(chain([
176-
addRouteToNgModule(options),
177-
mergeWith(templateSource),
178-
])),
179-
])(host, context);
216+
branchAndMerge(
217+
chain([addRouteToNgModule(options), mergeWith(templateSource)])
218+
),
219+
]);
180220
};
181221
}

0 commit comments

Comments
 (0)