Skip to content

Commit e62c965

Browse files
committed
fix(generate): Angular 6 schematics
1 parent c18625a commit e62c965

21 files changed

Lines changed: 69 additions & 1415 deletions

File tree

packages/@ionic/cli-utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
"uuid": "^3.2.1"
6262
},
6363
"devDependencies": {
64-
"@angular-devkit/core": "0.3.2",
65-
"@angular-devkit/schematics": "0.3.2",
64+
"@angular-devkit/core": "0.5.5",
65+
"@angular-devkit/schematics": "0.5.5",
6666
"@types/clean-css": "^3.4.30",
6767
"@types/debug": "0.0.30",
6868
"@types/diff": "^3.2.2",

packages/@ionic/cli-utils/src/lib/project/angular/generate.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,13 @@ ${chalk.cyan('[1]')}: ${chalk.bold('https://ionicframework.com/docs/cli/projects
308308
}
309309

310310
private async generateComponent(schematic: Schematic, name: string, options: { [key: string]: string | boolean; }) {
311+
let { type } = schematic;
312+
311313
if (schematic.collection !== ANGULAR_SCHEMATICS_PACKAGE) {
312-
options.collection = schematic.collection;
314+
type = `${schematic.collection}:${type}`;
313315
}
314316

315-
const ngArgs = unparseArgs({ _: ['generate', schematic.type, name], ...options }, {});
317+
const ngArgs = unparseArgs({ _: ['generate', type, name], ...options }, {});
316318
const shellOptions = { cwd: this.project.directory };
317319

318320
const p = await this.shell.spawn('ng', ngArgs, shellOptions);

packages/@ionic/cli-utils/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"../../../types/elementtree.d.ts",
1313
"../../../types/leek.d.ts",
1414
"../../../types/os-name.d.ts",
15+
"../../../types/rxjs-custom.d.ts",
1516
"../../../types/slice-ansi.d.ts",
1617
"../../../types/ssh-config.d.ts",
1718
"../../../types/string-width.d.ts",

packages/@ionic/schematics-angular/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"clean": "rimraf '*/index.{js,d.ts}'",
99
"lint": "tslint --config tslint.js --project tsconfig.json",
1010
"build": "npm run clean && tsc",
11-
"watch": "tsc -w --preserveWatchOutput",
11+
"watch": "tsc -w",
1212
"prepublishOnly": "npm run build"
1313
},
1414
"keywords": [
@@ -21,11 +21,12 @@
2121
],
2222
"schematics": "./collection.json",
2323
"dependencies": {
24-
"@angular-devkit/core": "0.3.2",
25-
"@angular-devkit/schematics": "0.3.2",
24+
"@angular-devkit/core": "0.5.5",
25+
"@angular-devkit/schematics": "0.5.5",
26+
"@schematics/angular": "0.5.5",
2627
"lodash": "^4.17.5",
2728
"tslib": "^1.9.0",
28-
"typescript": "~2.8.1"
29+
"typescript": ">=2.6.2 <2.8"
2930
},
3031
"devDependencies": {
3132
"@types/lodash": "^4.14.104",

packages/@ionic/schematics-angular/page/files/__path__/__name@kebabCase@if-flat__/__name@kebabCase__.page.__styleext__ renamed to packages/@ionic/schematics-angular/page/files/__name@kebabCase@if-flat__/__name@kebabCase__.page.__styleext__

File renamed without changes.

packages/@ionic/schematics-angular/page/files/__path__/__name@kebabCase@if-flat__/__name@kebabCase__.page.html renamed to packages/@ionic/schematics-angular/page/files/__name@kebabCase@if-flat__/__name@kebabCase__.page.html

File renamed without changes.

packages/@ionic/schematics-angular/page/files/__path__/__name@kebabCase@if-flat__/__name@kebabCase__.page.spec.ts renamed to packages/@ionic/schematics-angular/page/files/__name@kebabCase@if-flat__/__name@kebabCase__.page.spec.ts

File renamed without changes.

packages/@ionic/schematics-angular/page/files/__path__/__name@kebabCase@if-flat__/__name@kebabCase__.page.ts renamed to packages/@ionic/schematics-angular/page/files/__name@kebabCase@if-flat__/__name@kebabCase__.page.ts

File renamed without changes.

packages/@ionic/schematics-angular/page/index.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ import {
1515
url,
1616
} from '@angular-devkit/schematics';
1717

18-
import { addDeclarationToModule, addEntryComponentsToModule } from '../utils/angular/ast-utils';
19-
import { InsertChange } from '../utils/angular/change';
20-
import { buildRelativePath, findModuleFromOptions } from '../utils/angular/find-module';
18+
import { addDeclarationToModule, addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils';
19+
import { Change, InsertChange } from '@schematics/angular/utility/change';
20+
import { getWorkspace } from '@schematics/angular/utility/config';
21+
import { buildRelativePath, findModuleFromOptions } from '@schematics/angular/utility/find-module';
22+
import { parseName } from '@schematics/angular/utility/parse-name';
23+
import { validateHtmlSelector, validateName } from '@schematics/angular/utility/validation';
2124

2225
import { Schema as PageOptions } from './schema';
2326

24-
function addPageToNgModule(utils: 'Declaration'|'EntryComponents', options: PageOptions): Rule {
27+
function addEntryComponentsToModule(source: any, modulePath: string, classifiedName: string, importPath: string): Change[] {
28+
return addSymbolToNgModuleMetadata(source, modulePath, 'entryComponents', classifiedName, importPath);
29+
}
30+
31+
function addPageToNgModule(options: PageOptions): Rule {
2532
const { module } = options;
2633

2734
if (!module) {
@@ -39,30 +46,25 @@ function addPageToNgModule(utils: 'Declaration'|'EntryComponents', options: Page
3946
const source = ts.createSourceFile(module, sourceText, ts.ScriptTarget.Latest, true);
4047

4148
const pagePath = (
42-
`/${options.sourceDir}/${options.path}/` +
49+
`/${options.path}/` +
4350
(options.flat ? '' : `${kebabCase(options.name)}/`) +
4451
`${kebabCase(options.name)}.page`
4552
);
4653

4754
const relativePath = buildRelativePath(module, pagePath);
4855
const classifiedName = `${upperFirst(camelCase(options.name))}Page`;
4956

50-
let addNgModuleMethod: Function;
51-
if (utils === 'Declaration') {
52-
addNgModuleMethod = addDeclarationToModule;
53-
} else if (utils === 'EntryComponents') {
54-
addNgModuleMethod = addEntryComponentsToModule;
55-
} else {
56-
throw new SchematicsException('add module method is not found.');
57-
}
57+
const Changes = [
58+
...addDeclarationToModule(source, module, classifiedName, relativePath),
59+
...addEntryComponentsToModule(source, module, classifiedName, relativePath),
60+
];
5861

59-
const Changes = addNgModuleMethod(source, module, classifiedName, relativePath);
6062
const Recorder = host.beginUpdate(module);
6163

6264
for (const change of Changes) {
63-
if (change instanceof InsertChange) {
64-
Recorder.insertLeft(change.pos, change.toAdd);
65-
}
65+
if (change instanceof InsertChange) {
66+
Recorder.insertLeft(change.pos, change.toAdd);
67+
}
6668
}
6769

6870
host.commitUpdate(Recorder);
@@ -82,20 +84,29 @@ function buildSelector(options: PageOptions) {
8284
}
8385

8486
export default function (options: PageOptions): Rule {
85-
const { sourceDir } = options;
87+
return (host, context) => {
88+
const workspace = getWorkspace(host);
8689

87-
if (!sourceDir) {
88-
throw new SchematicsException('sourceDir option is required.');
89-
}
90+
if (!options.project) {
91+
options.project = Object.keys(workspace.projects)[0];
92+
}
9093

91-
if (!options.path) {
92-
throw new SchematicsException('path option is required.');
93-
}
94+
const project = workspace.projects[options.project];
95+
96+
if (options.path === undefined) {
97+
options.path = `/${project.root}/src/app`;
98+
}
9499

95-
return (host, context) => {
96100
options.module = findModuleFromOptions(host, options);
101+
102+
const parsedPath = parseName(options.path, options.name);
103+
options.name = parsedPath.name;
104+
options.path = parsedPath.path;
97105
options.selector = options.selector ? options.selector : buildSelector(options);
98106

107+
validateName(options.name);
108+
validateHtmlSelector(options.selector);
109+
99110
const templateSource = apply(url('./files'), [
100111
options.spec ? noop() : filter(p => !p.endsWith('.spec.ts')),
101112
template({
@@ -105,13 +116,12 @@ export default function (options: PageOptions): Rule {
105116
'if-flat': (s: string) => options.flat ? '' : s,
106117
...options,
107118
}),
108-
move(sourceDir),
119+
move(parsedPath.path),
109120
]);
110121

111122
return chain([
112123
branchAndMerge(chain([
113-
addPageToNgModule('Declaration', options),
114-
addPageToNgModule('EntryComponents', options),
124+
addPageToNgModule(options),
115125
mergeWith(templateSource),
116126
])),
117127
])(host, context);

packages/@ionic/schematics-angular/page/schema.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export interface Schema {
22
path?: string;
3-
sourceDir?: string;
4-
appRoot?: string;
3+
project?: string;
54
name: string;
65
prefix?: string;
76
styleext?: string;

0 commit comments

Comments
 (0)