@@ -6,6 +6,7 @@ var Cli = module.exports,
66 IonicStore = require ( './ionic/store' ) . IonicStore ,
77 IonicConfig = new IonicStore ( 'ionic.config' ) ,
88 Info = IonicAppLib . info ,
9+ IonicProject = IonicAppLib . project ,
910 Ionitron = require ( './ionic/ionitron' ) ,
1011 optimist = require ( 'optimist' ) ,
1112 path = require ( 'path' ) ,
@@ -94,105 +95,92 @@ Cli.run = function run(processArgv) {
9495 if ( ! taskSetting ) {
9596 return Cli . printAvailableTasks ( ) ;
9697 }
97-
9898 logging . logger . debug ( 'Task setting:' , taskSetting ) ;
99+ var booleanOptions = Cli . getBooleanOptionsForTask ( taskSetting ) ;
100+ argv = optimist ( processArgv . slice ( 2 ) ) . boolean ( booleanOptions ) . argv ;
101+ var taskModule = Cli . lookupTask ( taskSetting . module ) ;
102+ var taskInstance = new taskModule ( ) ;
99103
100- // assume false for tasks that can be run outside of Ionic projects
101- // start, help, docs
102- var isIonicV2 = false ;
103-
104- // Addressing issue #471
105- // Changes the cwd to the project root
106- // (whereever it finds ionic.project, errors if it doesn't find it) for tasks that require it.
107- // The cliTasks are in lib/tasks/cliTasks.js - with an attribute to disable changing pwd
108- // for commands like start, help, docs, and v2
104+ //this should be tracked by each task
109105 if ( ! taskSetting . disableChangePwd ) {
110- try {
111- logging . logger . debug ( 'Looking up ionic root, cwd:' , process . env . PWD ) ;
112- var root = Utils . cdIonicRoot ( ) ;
113- isIonicV2 = Utils . isIonicV2 ( root ) ;
114- isIonicV2 && logging . logger . debug ( 'Ionic 2 project.' )
115- logging . logger . debug ( 'ionic root now set pwd:' , process . env . PWD ) ;
116- } catch ( error ) {
117- //Hard fail here breaks plugin installation for v1 start task
118- logging . logger . debug ( error ) ;
106+ var root = Utils . cdIonicRoot ( ) ;
107+ var project = IonicProject . load ( root ) ;
108+ argv . v2 = project && project . get ( 'v2' ) ;
109+ argv . v2 && logging . logger . debug ( 'Ionic 2 project.' ) ;
110+
111+ if ( fs . existsSync ( 'node_modules' ) ) {
112+ //run with gulp hooks
113+ return runWithGulp ( argv , taskInstance ) ;
114+ } else if ( argv . v2 ) {
115+ console . warn ( 'WARN: No node_modules directory found, do you need to run npm install?' ) ;
119116 }
120117 }
118+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
121119
122- var booleanOptions = Cli . getBooleanOptionsForTask ( taskSetting ) ;
120+ } catch ( ex ) {
121+ logging . logger . debug ( 'Cli.Run - Error' , ex ) ;
122+ return Utils . fail ( ex ) ;
123+ }
124+ } ;
123125
124- argv = optimist ( processArgv . slice ( 2 ) ) . boolean ( booleanOptions ) . argv ;
126+ function runWithGulp ( argv , taskInstance ) {
127+ var cmdName = argv . _ [ 0 ] ;
128+ var beforeHook = cmdName + ':before' ;
129+ var afterHook = cmdName + ':after' ;
125130
126- var taskModule = Cli . lookupTask ( taskSetting . module ) ;
127- var taskInstance = new taskModule ( ) ;
131+ if ( loadGulpfile ( ) ) {
132+ logging . logger . debug ( 'Gulpfile found' ) ;
128133
129- if ( isIonicV2 ) {
130- // probably a better way to do this, but to avoid changing run() for every
131- // task for now
132- argv . v2 = true ;
133- }
134-
135- console . log ( ) ;
136- var oldConfig = fs . existsSync ( 'ionic.config.js' ) ;
137- if ( oldConfig ) {
138- console . warn ( 'WARN: ionic.config.js has been deprecated. Please use ionic.json instead.\n' ) ;
134+ try {
135+ var gulp = require ( path . resolve ( process . cwd ( ) + '/node_modules/gulp' ) ) ;
136+ } catch ( e ) {
137+ // Empty gulpfile (or one that doesn't require gulp?), and no gulp
138+ console . error ( '\nGulpfile detected, but gulp is not installed' ) ;
139+ console . error ( 'Do you need to run `npm install`?\n' ) ;
140+ process . exit ( 1 ) ;
139141 }
140-
141- var cmdName = argv . _ [ 0 ] ;
142- var gulpFound = loadGulp ( ) ;
143- if ( gulpFound ) {
144- logging . logger . debug ( 'Gulpfile found' ) ;
145- var beforeHook = cmdName + ':before' ;
146- var afterHook = cmdName + ':after' ;
147- try {
148- var gulp = require ( path . resolve ( process . cwd ( ) + '/node_modules/gulp' ) ) ;
149- } catch ( e ) {
150- // Empty gulpfile (or one that doesn't require gulp?), and no gulp
151- console . error ( '\nGulpfile detected, but gulp is not installed' ) ;
152- console . error ( 'Do you need to run `npm install`?\n' ) ;
153- process . exit ( 1 ) ;
154- }
155- logEvents ( gulp ) ;
156-
157- return Q . nfcall ( gulp . start . bind ( gulp ) , beforeHook ) . then (
158- function ( ) {
159- // Only some commands return promises
160- return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
161- } ,
162- function ( e ) { //task error, let gulp handle it
163- if ( / s e r v e | b u i l d | r u n | e m u l a t e | u p l o a d / . test ( cmdName ) && argv . v2 ) {
164- var taskName = ( cmdName === 'serve' ) ? 'watch' : 'build' ;
165- console . warn ( 'WARN: No \'' + beforeHook + '\' gulp task found!' ) ;
166- if ( oldConfig && gulp . tasks [ taskName ] ) {
167- console . info ( 'Your gulpfile contains a \'' + taskName + '\' task already! Add:\n' )
168- console . log ( ' gulp.task(\'' + cmdName + ':before\', [\'' + taskName + '\']);\n' ) ;
169- console . info ( 'to your gulpfile to have Ionic CLI run \'' + taskName + '\' before ' + cmdName + '.' ) ;
170- } else {
171- console . warn ( 'If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n' ) ;
172- }
173- console . log ( ) ;
142+ logEvents ( gulp ) ;
143+
144+ return Q . nfcall ( gulp . start . bind ( gulp ) , beforeHook ) . then (
145+ function ( ) {
146+ // Only some commands return promises, so wrap it
147+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
148+ } ,
149+ function ( e ) { //task error, let gulp handle it
150+
151+ // Warn if it's a v2 project and there isn't a build hook
152+ if ( / s e r v e | b u i l d | r u n | e m u l a t e | u p l o a d / . test ( cmdName ) && argv . v2 ) {
153+ var taskName = ( cmdName === 'serve' ) ? 'watch' : 'build' ;
154+ console . warn ( 'WARN: No \'' + beforeHook + '\' gulp task found!' ) ;
155+
156+ //The task exists, but there are no hooks for it
157+ //They have the old config file, so we're safe to yell at them
158+ //With new config, may intentionally not have hooks if running their own build
159+ if ( gulp . tasks [ taskName ] && fs . existsSync ( 'ionic.config.js' ) ) {
160+ console . info ( 'Your gulpfile contains a \'' + taskName + '\' task already! Add:\n' )
161+ console . log ( ' gulp.task(\'' + cmdName + ':before\', [\'' + taskName + '\']);\n' ) ;
162+ console . info ( 'to your gulpfile to have Ionic CLI run \'' + taskName + '\' before ' + cmdName + '.\n' ) ;
163+ } else {
164+ console . warn ( 'If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n' ) ;
174165 }
175- // Only some commands return promises
176- return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
177166 }
178- ) . then ( function ( ) {
179- return Q . nfcall ( gulp . start . bind ( gulp ) , argv . _ [ 0 ] + ':after' ) ;
180- } ) ;
181- } else {
182- if ( / s e r v e | b u i l d | r u n | e m u l a t e | u p l o a d / . test ( cmdName ) && argv . v2 ) {
183- console . warn ( 'WARN: No gulpfile found!' ) ;
184- console . warn ( 'If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n' ) ;
167+ // Only some commands return promises, so wrap it
168+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
185169 }
186- return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
187- }
170+ ) . then ( function ( ) {
171+ return Q . nfcall ( gulp . start . bind ( gulp ) , argv . _ [ 0 ] + ':after' ) ;
172+ } ) ;
173+ }
188174
189- } catch ( ex ) {
190- logging . logger . debug ( 'Cli.Run - Error' , ex ) ;
191- return Utils . fail ( ex ) ;
175+ if ( / s e r v e | b u i l d | r u n | e m u l a t e | u p l o a d / . test ( cmdName ) && argv . v2 ) {
176+ console . warn ( 'WARN: No gulpfile found!' ) ;
177+ console . warn ( 'If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n' ) ;
192178 }
193- } ;
179+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
180+ }
194181
195- function loadGulp ( ) {
182+ function loadGulpfile ( ) {
183+ //TODO add babel support?
196184 var names = [ 'gulpfile.js' , 'Gulpfile.js' ] ;
197185 for ( var i = 0 , ii = names . length ; i < ii ; i ++ ) {
198186 try {
0 commit comments