Grunt - 从命令行传递filename变量

spa*_*dow 3 terminal command-line-arguments gruntjs

我正在努力理解如何从grunt命令行传递部分文件名,以便在特定文件上运行任务(来自已安装的grunt模块).

我希望能够做的是配置一系列任务以从命令行获取filename参数.

我已经尝试在这个页面http://chrisawren.com/posts/Advanced-Grunt-tooling上重新编写最后的例子,但我有点在黑暗中刺伤了一下.以为有人会快速回答.

这是我的Gruntfile:

module.exports = function (grunt) {
    grunt.initConfig({
      globalConfig: globalConfig,

        uglify: {
          js: {
            options: {
              mangle: true
            },
            files: {
              'js/<%= globalConfig.file %>.min.js': ['js/<%= globalConfig.file %>.js']
            }
          }
        },



    });

    // Load tasks so we can use them



    grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.registerTask('go', 'Runs a task on a specified file', function (fileName){
      globalConfig.file = fileName;
      grunt.task.run('uglify:js');
    });
};
Run Code Online (Sandbox Code Playgroud)

我尝试从命令行运行它,如下所示:

grunt go:app
Run Code Online (Sandbox Code Playgroud)

以js/app.js为目标

我收到此错误:

Aborted due to warnings.
roberts-mbp:150212 - Grunt Tasks robthwaites$ grunt go:app
Loading "Gruntfile.js" tasks...ERROR
>> ReferenceError: globalConfig is not defined
Warning: Task "go:app" not found. Use --force to continue.
Run Code Online (Sandbox Code Playgroud)

谢谢

Tru*_*hal 7

你可以使用grunt.option.

你的grunt注册任务看起来像这样.

> grunt.option('fileName'); grunt.registerTask('go', 'Runs a task on a
> specified file', function (){     
>       grunt.task.run('uglify:js');
>     });
Run Code Online (Sandbox Code Playgroud)

你的咕噜声配置将是

module.exports = function (grunt) {
 var fileName=grunt.option('fileName');
    grunt.initConfig({
        uglify: {
          js: {
            options: {
              mangle: true
            },
            files: {
              'js/fileName.min.js': ['js/fileName.js']
            }
          }
        },   
    });
Run Code Online (Sandbox Code Playgroud)

命令从终端运行任务:

$ grunt go --fileName ='xyzfile'