Skip to content

Commit 100b1d1

Browse files
committed
refactor(utils): Cleaned up code in Grunt util
1 parent 9a0fe82 commit 100b1d1

1 file changed

Lines changed: 66 additions & 58 deletions

File tree

lib/util/grunt.js

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ var Promise = require( 'bluebird' )
1414
* @private
1515
*/
1616

17-
function gruntCLI ( ) {
18-
this.tasks = [ ];
17+
function gruntCLI() {
18+
this.tasks = [];
1919

20-
this.option = function ( key ) {
20+
this.option = function( key ) {
2121
return process.env[ key ];
2222
}
2323

24-
this.registerTask = function ( cmd ) {
24+
this.registerTask = function( cmd ) {
2525
this.tasks.push( cmd );
2626
}
2727

28-
this.loadNpmTasks = function ( ) { }
28+
this.loadNpmTasks = function() { }
2929
}
3030

3131
/**
@@ -38,11 +38,11 @@ function gruntCLI ( ) {
3838
* @api public
3939
*/
4040

41-
var gruntUtility = exports.gruntUtility = function ( filePath ) {
42-
var cli = new gruntCLI( )
41+
var gruntUtility = exports.gruntUtility = function( filePath ) {
42+
var cli = new gruntCLI()
4343
, file = require( filePath )( cli );
4444

45-
if (Array.isArray( file ) && typeof file[ file.length-1 ] === "function") {
45+
if ( Array.isArray( file ) && typeof file[ file.length-1 ] === "function" ) {
4646
file[ file.length-1 ].call( file, cli );
4747
}
4848

@@ -58,25 +58,25 @@ var gruntUtility = exports.gruntUtility = function ( filePath ) {
5858
* @api public
5959
*/
6060

61-
var readTasks = exports.readTasks = function ( pathSrc, silence ) {
62-
var def = Promise.defer( );
61+
var readTasks = exports.readTasks = function( pathSrc, silence ) {
62+
var def = Promise.defer();
6363

64-
if (typeof silence === "undefined") {
64+
if ( typeof silence === "undefined" ) {
6565
silence = true;
6666
}
6767

6868
findGruntFile( pathSrc )
69-
.then( function ( filePath ) {
69+
.then( function( filePath ) {
7070
var tasks = gruntUtility( filePath );
7171
def.resolve( [ tasks, pathSrc ] );
72-
} )
72+
})
7373
.catch( function ( err ) {
74-
if (err.match( /^Gruntfile within/ ) !== null && silence === true) {
74+
if ( err.match( /^Gruntfile within/ ) !== null && silence === true ) {
7575
return def.resolve( [ [ ], pathSrc ] );
7676
}
7777

7878
def.reject( err );
79-
} );
79+
});
8080

8181
return def.promise;
8282
}
@@ -89,21 +89,21 @@ var readTasks = exports.readTasks = function ( pathSrc, silence ) {
8989
* @api public
9090
*/
9191

92-
var findGruntFile = exports.findGruntFile = function ( pathSrc ) {
93-
var def = Promise.defer( );
92+
var findGruntFile = exports.findGruntFile = function( pathSrc ) {
93+
var def = Promise.defer();
9494

95-
async.detect( [
95+
async.detect([
9696
path.join( pathSrc, 'Gruntfile.js' ),
9797
path.join( pathSrc, 'gruntfile.js' ),
9898
path.join( pathSrc, 'Grunt.js' ),
9999
path.join( pathSrc, 'grunt.js' )
100-
], fs.exists, function ( filePath ) {
101-
if (typeof filePath === "undefined") {
100+
], fs.exists, function( filePath ) {
101+
if ( typeof filePath === "undefined" ) {
102102
return def.reject( 'Gruntfile within ' + pathSrc + ' could not be found.' );
103103
}
104104

105105
def.resolve( filePath );
106-
} );
106+
});
107107

108108
return def.promise;
109109
}
@@ -117,7 +117,7 @@ var findGruntFile = exports.findGruntFile = function ( pathSrc ) {
117117
* @api private
118118
*/
119119

120-
function runTask ( projectFolder, cmd ) {
120+
function runTask( projectFolder, cmd ) {
121121
var def = Promise.defer( )
122122
, proc = spawn( !isWin ? 'grunt' : 'grunt.cmd', [ cmd ], { cwd: projectFolder, stdio: 'inherit' } );
123123

@@ -147,30 +147,30 @@ function runTask ( projectFolder, cmd ) {
147147
* @api private
148148
*/
149149

150-
function runDBMigrations ( projectFolder ) {
151-
var def = Promise.defer( )
150+
function runDBMigrations( projectFolder ) {
151+
var def = Promise.defer()
152152
, env = process.env;
153153

154154
lib.utils.warn( 'Running database migrations...' );
155155

156156
// check for NODE_ENV json config file if it doesn't exist then revert to local
157-
if (!fs.existsSync( path.join( projectFolder, 'config', env.NOD_ENV + '.json') ) ) {
157+
if ( !fs.existsSync( path.join( projectFolder, 'config', env.NOD_ENV + '.json') ) ) {
158158
env.NODE_ENV = 'local';
159159
}
160160

161-
exec( 'NODE_ENV=' + env.NODE_ENV + ' grunt db', { env: env, cwd: projectFolder }, function ( err, stdout, stderr ) {
161+
exec( 'NODE_ENV=' + env.NODE_ENV + ' grunt db', { env: env, cwd: projectFolder }, function( err, stdout, stderr ) {
162162
if (!!err) {
163163
return def.reject( err );
164164
}
165165

166-
if (!!stderr && stderr !== "") {
166+
if ( !!stderr && stderr !== "" ) {
167167
return def.reject( stderr );
168168
}
169169

170170
lib.utils.progress();
171171

172-
def.resolve( );
173-
} );
172+
def.resolve();
173+
});
174174

175175
return def.promise;
176176
}
@@ -184,7 +184,7 @@ function runDBMigrations ( projectFolder ) {
184184
* @api public
185185
*/
186186

187-
exports.runTasks = function ( projectFolder, modulePath ) {
187+
exports.runTasks = function( projectFolder, modulePath ) {
188188
var def = Promise.defer( );
189189

190190
readTasks( modulePath )
@@ -195,44 +195,52 @@ exports.runTasks = function ( projectFolder, modulePath ) {
195195
}
196196

197197
Promise.all( tasks )
198-
.then( function ( ) {
199-
if (tasks.indexOf( 'readme') > -1) {
198+
.then( function() {
199+
if ( tasks.indexOf( 'readme' ) > -1 ) {
200200
lib.utils.expandProgress( 1 );
201201
return runTask( projectFolder, 'readme' );
202202
}
203203
return true;
204-
} )
205-
.then( function ( ) {
206-
if (tasks.indexOf( 'prompt:clever' ) > -1) {
207-
lib.utils.expandProgress( 1 );
208-
209-
return new Promise(function( resolve, reject ) {
210-
lib.utils.stopBar(function() {
211-
runTask( projectFolder, 'prompt:clever' )
212-
.then(function() {
213-
resolve();
214-
})
215-
.catch(function( err ) {
216-
reject( err );
204+
})
205+
.then( function() {
206+
return new Promise( function( resolve, reject ) {
207+
async.forEach(
208+
tasks,
209+
function( task, callback ) {
210+
if ( /^prompt:clever*/.test( task ) ) {
211+
lib.utils.expandProgress( 1 );
212+
lib.utils.stopBar( function() {
213+
runTask( projectFolder, task )
214+
.then( function() {
215+
callback( null );
216+
})
217+
.catch( callback );
217218
});
218-
});
219-
});
220-
}
221-
return true;
222-
} )
223-
.then( function ( ) {
219+
} else {
220+
callback( null );
221+
}
222+
},
223+
function( err ) {
224+
!err
225+
? resolve()
226+
: reject( err );
227+
}
228+
);
229+
});
230+
})
231+
.then( function() {
224232
if (tasks.indexOf( 'db' ) > -1) {
225233
lib.utils.expandProgress( 1 );
226234
return runDBMigrations( projectFolder );
227235
}
228236
return true;
229-
} )
230-
.then( function ( ) {
231-
def.resolve( );
232-
}, function ( err ) {
237+
})
238+
.then( function() {
239+
def.resolve();
240+
}, function( err ) {
233241
def.reject( err );
234-
} );
235-
} );
242+
});
243+
});
236244

237245
return def.promise;
238-
}
246+
}

0 commit comments

Comments
 (0)