@@ -25,8 +25,9 @@ import {
25
25
import { addDeclarationToNgModule } from '../utility/add-declaration-to-ng-module' ;
26
26
import { findModuleFromOptions } from '../utility/find-module' ;
27
27
import { parseName } from '../utility/parse-name' ;
28
+ import { createProjectSchematic } from '../utility/project' ;
28
29
import { validateClassName , validateHtmlSelector } from '../utility/validation' ;
29
- import { buildDefaultPath , getWorkspace } from '../utility/workspace' ;
30
+ import { buildDefaultPath } from '../utility/workspace' ;
30
31
import { Schema as ComponentOptions , Style } from './schema' ;
31
32
32
33
function buildSelector ( options : ComponentOptions , projectPrefix : string ) {
@@ -40,62 +41,52 @@ function buildSelector(options: ComponentOptions, projectPrefix: string) {
40
41
return selector ;
41
42
}
42
43
43
- export default function ( options : ComponentOptions ) : Rule {
44
- return async ( host : Tree ) => {
45
- const workspace = await getWorkspace ( host ) ;
46
- const project = workspace . projects . get ( options . project ) ;
47
-
48
- if ( ! project ) {
49
- throw new SchematicsException ( `Project "${ options . project } " does not exist.` ) ;
50
- }
51
-
52
- if ( options . path === undefined ) {
53
- options . path = buildDefaultPath ( project ) ;
54
- }
44
+ export default createProjectSchematic < ComponentOptions > ( ( options , { project, tree } ) => {
45
+ if ( options . path === undefined ) {
46
+ options . path = buildDefaultPath ( project ) ;
47
+ }
55
48
56
- options . module = findModuleFromOptions ( host , options ) ;
57
- // Schematic templates require a defined type value
58
- options . type ??= '' ;
49
+ options . module = findModuleFromOptions ( tree , options ) ;
50
+ // Schematic templates require a defined type value
51
+ options . type ??= '' ;
59
52
60
- const parsedPath = parseName ( options . path , options . name ) ;
61
- options . name = parsedPath . name ;
62
- options . path = parsedPath . path ;
63
- options . selector =
64
- options . selector || buildSelector ( options , ( project && project . prefix ) || '' ) ;
53
+ const parsedPath = parseName ( options . path , options . name ) ;
54
+ options . name = parsedPath . name ;
55
+ options . path = parsedPath . path ;
56
+ options . selector = options . selector || buildSelector ( options , ( project && project . prefix ) || '' ) ;
65
57
66
- validateHtmlSelector ( options . selector ) ;
67
- validateClassName ( strings . classify ( options . name ) ) ;
58
+ validateHtmlSelector ( options . selector ) ;
59
+ validateClassName ( strings . classify ( options . name ) ) ;
68
60
69
- const skipStyleFile = options . inlineStyle || options . style === Style . None ;
70
- const templateSource = apply ( url ( './files' ) , [
71
- options . skipTests ? filter ( ( path ) => ! path . endsWith ( '.spec.ts.template' ) ) : noop ( ) ,
72
- skipStyleFile ? filter ( ( path ) => ! path . endsWith ( '.__style__.template' ) ) : noop ( ) ,
73
- options . inlineTemplate ? filter ( ( path ) => ! path . endsWith ( '.html.template' ) ) : noop ( ) ,
74
- applyTemplates ( {
75
- ...strings ,
76
- 'if-flat' : ( s : string ) => ( options . flat ? '' : s ) ,
77
- 'ngext' : options . ngHtml ? '.ng' : '' ,
78
- ...options ,
79
- } ) ,
80
- ! options . type
81
- ? forEach ( ( ( file ) => {
82
- return file . path . includes ( '..' )
83
- ? {
84
- content : file . content ,
85
- path : file . path . replace ( '..' , '.' ) ,
86
- }
87
- : file ;
88
- } ) as FileOperator )
89
- : noop ( ) ,
90
- move ( parsedPath . path ) ,
91
- ] ) ;
61
+ const skipStyleFile = options . inlineStyle || options . style === Style . None ;
62
+ const templateSource = apply ( url ( './files' ) , [
63
+ options . skipTests ? filter ( ( path ) => ! path . endsWith ( '.spec.ts.template' ) ) : noop ( ) ,
64
+ skipStyleFile ? filter ( ( path ) => ! path . endsWith ( '.__style__.template' ) ) : noop ( ) ,
65
+ options . inlineTemplate ? filter ( ( path ) => ! path . endsWith ( '.html.template' ) ) : noop ( ) ,
66
+ applyTemplates ( {
67
+ ...strings ,
68
+ 'if-flat' : ( s : string ) => ( options . flat ? '' : s ) ,
69
+ 'ngext' : options . ngHtml ? '.ng' : '' ,
70
+ ...options ,
71
+ } ) ,
72
+ ! options . type
73
+ ? forEach ( ( ( file ) => {
74
+ return file . path . includes ( '..' )
75
+ ? {
76
+ content : file . content ,
77
+ path : file . path . replace ( '..' , '.' ) ,
78
+ }
79
+ : file ;
80
+ } ) as FileOperator )
81
+ : noop ( ) ,
82
+ move ( parsedPath . path ) ,
83
+ ] ) ;
92
84
93
- return chain ( [
94
- addDeclarationToNgModule ( {
95
- type : 'component' ,
96
- ...options ,
97
- } ) ,
98
- mergeWith ( templateSource ) ,
99
- ] ) ;
100
- } ;
101
- }
85
+ return chain ( [
86
+ addDeclarationToNgModule ( {
87
+ type : 'component' ,
88
+ ...options ,
89
+ } ) ,
90
+ mergeWith ( templateSource ) ,
91
+ ] ) ;
92
+ } ) ;
0 commit comments