grunt-multi
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "grunt-multi", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Run Grunt task with multi-configuration.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -115,3 +115,20 @@ # grunt-multi | ||
} | ||
} | ||
}, | ||
// Also you can use function to direct modify the config, this is useful if you want to get more flexible to modify the configuration. | ||
// params: | ||
// 1、vars: a single instant of the vars you defined | ||
// 2、rawConfig: the raw configuration. | ||
constant_func: { | ||
options: { | ||
vars: { | ||
page_list: [ 'a', 'b', 'c' ], | ||
out_target: 'mod2.js' | ||
}, | ||
config: { | ||
targetPage: function( vars, rawConfig ){ return vars.page_list; }, | ||
outTarget: function( vars, rawConfig ){ return vars.out_target; } | ||
}, | ||
tasks: [ 'copy' ] | ||
} | ||
}, | ||
} | ||
@@ -129,2 +146,12 @@ ``` | ||
### How to decide if its a multi-single thread. | ||
In some cases maybe you want to tell if the current thread is a child spawned by `grunt-multi`, just use the `multi-single` option to distinguish: | ||
``` | ||
if( grunt.option( 'multi-single' ) ){ | ||
console.log( 'Child' ); | ||
} | ||
``` | ||
Enjoy! |
@@ -9,5 +9,3 @@ /* | ||
grunt.registerTask( 'multi-single', 'The single task for multi', function(){ | ||
console.log( '\n' ); | ||
grunt.log.ok( 'A single thread begin:' ); | ||
console.log( '\n' ); | ||
@@ -17,3 +15,3 @@ // Get the raw config and try to update. | ||
// Get the special config | ||
var singleInfo = JSON.parse( decodeURIComponent( grunt.option( 'multi-single-info' ) ) ); | ||
var singleInfo = JSON.parse(process.env._grunt_multi_info); | ||
var singleCfg = singleInfo.config; | ||
@@ -26,3 +24,3 @@ var singleTasks = singleInfo.tasks; | ||
grunt.util._.each( singleCfg, function( value, key ){ | ||
console.log( '\033[1;33m' + key + ':', value + '\033[0m' ); | ||
console.log( '\033[1;33m' + key + ':', JSON.stringify(value) + '\033[0m' ); | ||
}); | ||
@@ -53,4 +51,7 @@ console.log( '\033[1;32m\n--------------------------------\033[0m\n' ); | ||
var vars = options.vars || {}; | ||
var ifContinued = options.continued; | ||
// Stringify the config, to simplify the template process. | ||
var configStr = JSON.stringify( options.config ); | ||
var configFunc = {}; | ||
var configNotFunc = {}; | ||
var configStr = ''; | ||
var tasks = options.tasks || []; | ||
@@ -74,2 +75,17 @@ // All the var lists go here | ||
/** | ||
* Separate the option.config | ||
*/ | ||
grunt.util._.each( options.config, function( cfg, key ){ | ||
if( grunt.util._.isFunction( cfg ) ){ | ||
configFunc[ key ] = cfg; | ||
} | ||
else { | ||
configNotFunc[ key ] = cfg; | ||
} | ||
}); | ||
configStr = JSON.stringify( configNotFunc ); | ||
/** | ||
* If multi-vars specified, it will override the configuration. | ||
@@ -145,3 +161,7 @@ * example: --multi-vars a=1,2,3:b:jack,bill,rose | ||
var config = grunt.template.process( configStr, { data: data } ); | ||
configs.push( JSON.parse( config ) ); | ||
var cfgObj = JSON.parse( config ); | ||
grunt.util._.each( configFunc, function( func, key ){ | ||
cfgObj[ key ] = func( data, grunt.config.getRaw() ); | ||
}); | ||
configs.push( cfgObj ); | ||
}); | ||
@@ -152,30 +172,59 @@ | ||
// Let's roll! | ||
grunt.util._.each(configs, function( cfg ){ | ||
if( taskLen > 0 ){ | ||
// Let's roll! | ||
grunt.util._.each(configs, function( cfg ){ | ||
/** | ||
* Remove the `node` and `grunt` from argv,and keep the rest as it is. | ||
* use `--multi-single` to indicate the thread is a single task generated my Multi. | ||
* use `--multi-cfg` to pass the single configuration to child process | ||
* note the configuration is stringify and encoded. | ||
*/ | ||
var args = [ 'multi-single', '--multi-single-info=' + encodeURIComponent(JSON.stringify( { config: cfg, tasks: tasks })) ]; | ||
Util.spawn( grunt, { | ||
grunt: true, | ||
args: args | ||
}, function( error, result, code ){ | ||
taskCount++; | ||
if( error ){ | ||
console.log( '\n\033[1;31mBuild Error: \033[0m\n', error, result, code ); | ||
/** | ||
* Remove the `node` and `grunt` from argv,and keep the rest as it is. | ||
* use `--multi-single` to indicate the thread is a single task generated my Multi. | ||
* use `--multi-cfg` to pass the single configuration to child process | ||
* note the configuration is stringify and encoded. | ||
*/ | ||
var args = [ 'multi-single', '--multi-single' ]; | ||
if( grunt.util._.indexOf( process.argv, '--debug' ) >= 0 ){ | ||
args.push( '--debug' ); | ||
} | ||
else { | ||
console.log( result.stdout.replace( '\n\u001b[32mDone, without errors.\u001b[39m', '' ) ); | ||
} | ||
if( taskCount == taskLen ){ | ||
done(); | ||
} | ||
Util.spawn( grunt, { | ||
grunt: true, | ||
args: args, | ||
opts: { | ||
env: grunt.util._.merge( process.env, { _grunt_multi_info: JSON.stringify({ config: cfg, tasks: tasks }) } ) | ||
}, | ||
force: ifContinued | ||
}, function( error, result, code ){ | ||
if( !ifContinued ){ | ||
taskCount++; | ||
if( error ){ | ||
console.log( '\n\033[1;31mBuild Error: \033[0m\n', error, result, code ); | ||
} | ||
else { | ||
console.log( result.stdout.replace( '\n\u001b[32mDone, without errors.\u001b[39m', '' ) ); | ||
} | ||
if( taskCount == taskLen ){ | ||
done(); | ||
} | ||
} | ||
}, function( child ){ | ||
if( ifContinued ){ | ||
child.stdout.on('data', function (data) { | ||
console.log( data.toString( 'utf8') ); | ||
taskCount++; | ||
if( taskCount == taskLen ){ | ||
done(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
else { | ||
grunt.log.warn( 'No task to run.' ); | ||
done(); | ||
} | ||
}); | ||
}; |
@@ -149,2 +149,27 @@ /** | ||
it('USE config func', function (done) { | ||
// 执行grunt | ||
// 先安装npm 依赖 | ||
ChildProcess.exec( 'grunt multi:constant_func --debug', function (error, stdout, stderr) { | ||
console.log( stdout); | ||
console.log( stderr); | ||
if (error) { | ||
done( error ); | ||
} | ||
else { | ||
assertFiles( [ | ||
'build/a/app.js', | ||
'build/a/index.js', | ||
'build/b/home.js', | ||
'build/b/profile.js', | ||
'build/c/dashboard.js', | ||
'build/c/list.js', | ||
'build/mod2.js' | ||
]); | ||
done(null); | ||
} | ||
}); | ||
}); | ||
it('USE func', function (done) { | ||
@@ -151,0 +176,0 @@ |
@@ -83,2 +83,15 @@ module.exports = function (grunt) { | ||
}, | ||
constant_func: { | ||
options: { | ||
vars: { | ||
page_list: [ 'a', 'b', 'c' ], | ||
out_target: 'mod2.js' | ||
}, | ||
config: { | ||
targetPage: function( vars, rawConfig ){ return vars.page_list; }, | ||
outTarget: function( vars, rawConfig ){ return vars.out_target; } | ||
}, | ||
tasks: [ 'copy' ] | ||
} | ||
}, | ||
func: { | ||
@@ -85,0 +98,0 @@ options: { |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
25430
567
155
3