@@ -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
2225import { 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
8486export 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 ) ;
0 commit comments