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' ;
49
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' ) ;
713const getFiles = Blueprint . prototype . files ;
814
915export default Blueprint . extend ( {
@@ -32,9 +38,22 @@ export default Blueprint.extend({
3238 type : String ,
3339 aliases : [ 'a' ] ,
3440 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.'
3546 }
3647 ] ,
3748
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+
3857 normalizeEntityName : function ( entityName : string ) {
3958 this . entityName = entityName ;
4059 const appConfig = getAppFromConfig ( this . options . app ) ;
@@ -59,7 +78,7 @@ export default Blueprint.extend({
5978 } ;
6079 } ,
6180
62- files : function ( ) {
81+ files : function ( ) {
6382 let fileList = getFiles . call ( this ) as Array < string > ;
6483
6584 if ( ! this . options || ! this . options . spec ) {
@@ -84,5 +103,36 @@ export default Blueprint.extend({
84103 return this . generatePath ;
85104 }
86105 } ;
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 ) ;
87137 }
88138} ) ;
0 commit comments