1
- import { CliConfig } from '../../models/config' ;
2
- import { getAppFromConfig } from '../../utilities/app-utils' ;
3
- import { dynamicPathParser } from '../../utilities/dynamic-path-parser' ;
1
+ import * as chalk from 'chalk' ;
2
+ import * as path from 'path' ;
3
+ import { oneLine } from 'common-tags' ;
4
+ import { NodeHost } from '../../lib/ast-tools' ;
5
+ import { CliConfig } from '../../models/config' ;
6
+ import { getAppFromConfig } from '../../utilities/app-utils' ;
7
+ import { resolveModulePath } from '../../utilities/resolve-module-file' ;
8
+ import { dynamicPathParser } from '../../utilities/dynamic-path-parser' ;
4
9
5
- const path = require ( 'path' ) ;
6
- const Blueprint = require ( '../../ember-cli/lib/models/blueprint' ) ;
10
+ const stringUtils = require ( 'ember-cli-string-utils' ) ;
11
+ const Blueprint = require ( '../../ember-cli/lib/models/blueprint' ) ;
12
+ const astUtils = require ( '../../utilities/ast-utils' ) ;
7
13
const getFiles = Blueprint . prototype . files ;
8
14
9
15
export default Blueprint . extend ( {
@@ -32,9 +38,22 @@ export default Blueprint.extend({
32
38
type : String ,
33
39
aliases : [ 'a' ] ,
34
40
description : 'Specifies app name to use.'
41
+ } ,
42
+ {
43
+ name : 'module' ,
44
+ type : String , aliases : [ 'm' ] ,
45
+ description : 'Specifies where the module should be imported.'
35
46
}
36
47
] ,
37
48
49
+ beforeInstall : function ( options : any ) {
50
+ if ( options . module ) {
51
+ const appConfig = getAppFromConfig ( this . options . app ) ;
52
+ this . pathToModule =
53
+ resolveModulePath ( options . module , this . project , this . project . root , appConfig ) ;
54
+ }
55
+ } ,
56
+
38
57
normalizeEntityName : function ( entityName : string ) {
39
58
this . entityName = entityName ;
40
59
const appConfig = getAppFromConfig ( this . options . app ) ;
@@ -59,7 +78,7 @@ export default Blueprint.extend({
59
78
} ;
60
79
} ,
61
80
62
- files : function ( ) {
81
+ files : function ( ) {
63
82
let fileList = getFiles . call ( this ) as Array < string > ;
64
83
65
84
if ( ! this . options || ! this . options . spec ) {
@@ -84,5 +103,36 @@ export default Blueprint.extend({
84
103
return this . generatePath ;
85
104
}
86
105
} ;
106
+ } ,
107
+
108
+ afterInstall ( options : any ) {
109
+ const returns : Array < any > = [ ] ;
110
+
111
+ if ( ! this . pathToModule ) {
112
+ const warningMessage = oneLine `
113
+ Module is generated but not provided,
114
+ it must be provided to be used
115
+ ` ;
116
+ this . _writeStatusToUI ( chalk . yellow , 'WARNING' , warningMessage ) ;
117
+ } else {
118
+ let className = stringUtils . classify ( `${ options . entity . name } Module` ) ;
119
+ let fileName = stringUtils . dasherize ( `${ options . entity . name } .module` ) ;
120
+ if ( options . routing ) {
121
+ className = stringUtils . classify ( `${ options . entity . name } RoutingModule` ) ;
122
+ fileName = stringUtils . dasherize ( `${ options . entity . name } -routing.module` ) ;
123
+ }
124
+ const fullGeneratePath = path . join ( this . project . root , this . generatePath ) ;
125
+ const moduleDir = path . parse ( this . pathToModule ) . dir ;
126
+ const relativeDir = path . relative ( moduleDir , fullGeneratePath ) ;
127
+ const importPath = relativeDir ? `./${ relativeDir } /${ fileName } ` : `./${ fileName } ` ;
128
+ returns . push (
129
+ astUtils . addImportToModule ( this . pathToModule , className , importPath )
130
+ . then ( ( change : any ) => change . apply ( NodeHost ) ) ) ;
131
+ this . _writeStatusToUI ( chalk . yellow ,
132
+ 'update' ,
133
+ path . relative ( this . project . root , this . pathToModule ) ) ;
134
+ }
135
+
136
+ return Promise . all ( returns ) ;
87
137
}
88
138
} ) ;
0 commit comments