Javascript 通过命令行更改Grunt配置变量
我有两个不同的路径,我想编译移动与桌面代码。我想通过在命令行中传递grunt参数来进行替换Javascript 通过命令行更改Grunt配置变量,javascript,command-line-interface,gruntjs,Javascript,Command Line Interface,Gruntjs,我有两个不同的路径,我想编译移动与桌面代码。我想通过在命令行中传递grunt参数来进行替换 /** * @module Build * @class Build.Config * @static */ module.exports = function(grunt) { var config = {}; var NewPath; var env = grunt.option('target') || "Mobile"; if (env == "Desk
/**
* @module Build
* @class Build.Config
* @static
*/
module.exports = function(grunt) {
var config = {};
var NewPath;
var env = grunt.option('target') || "Mobile";
if (env == "Desktop") { // MAKE THIS DYNAMIC WITH COMMAND LINE ARGUMENT
newPath = "source/desktop/";
}
else {
newPath = "source/mobile/";
}
config.root = newPath;
config.stylesheets = config.root + '/stylesheets';
config.javascripts = config.root + '/javascripts';
config.images = config.root + '/images';
config.jsbin = config.javascripts + '/generated';
config.cssbin = config.stylesheets + '/generated';
config.docsbin = 'docs';
// Project configuration.
grunt.initConfig({
'beautifier': {
'options': {
'indentSize': 1,
'indentChar': '\t',
'spaceAfterAnonFunction': true
}
},
'beautify': {
'files': [ config.javascripts + '/app/**/*.js' ]
},
'requirejs': require('./build/config/requirejs.js')(config),
'watch': require('./build/config/watch.js')(config),
'stylus':require('./build/config/stylus.js')(config)
});
// Default task.
grunt.registerTask('default', ['stylus:compile','requirejs']);
grunt.registerTask('dev', ['stylus:dev']);
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-stylus');
};
结果证明我做得很好,我只需要正确地为env传入变量: $grunt--target=“Desktop”另一个选项是通过冒号传递。例如,将其传递给jshint
GruntJShint:桌面
然后使用process.argv
配置grunt以获取该命令行参数,您可以使用它来配置路径或其他可能需要的内容:
module.exports = function(grunt) {
"use strict";
//dynamic config after the ':'. 'desktop' here
var env = process.argv[2].split(':')[1];
var config = {
pkg: grunt.file.readJSON('package.json'),
jshint: {
options: {
jshintrc: '.jshintrc',
"force": true
}
},
};
//...
config.jshint[env] = { // ex: $ grunt jshint:desktop
src: ['public/'+env+'/js/main.js']
};
//...
// Project configuration.
grunt.initConfig(config);
//...
};
使用process
时需要注意的一点是,当您使用一个像有用的任务一样重新启动流程的繁重任务时,它将不起作用。在这种情况下,最好按照@im_benton所示进行。传递grunt-mytask--myvar=myval
并将其作为grunt.option('myvar')
`