从另一个gruntfile加载grunt任务

时间:2015-03-18 10:20:31

标签: javascript gruntjs

我想避免重复的代码,所以我试图从Grunt文件“a”加载grunt任务并在gruntfile“b”中使用它们。

这意味着:我希望在文件“b”(但没有代码)中看到“a”的所有任务,只需将其设置为另一个gruntfile的引用或模板。

这里是grunt文件“b”:

module.exports = function (grunt) {
'use strict';

var karmaGrunt = './../../grunt',
    abortHandler = function () {
        var errors = grunt.fail.errorcount,
            warnings = grunt.fail.warncount;

        if (errors > 0 || warnings > 0) {
            //run rocketlauncher python script and then stop the grunt runner.
            grunt.task.run(["shell:rocketlauncher", "fatal"]);
        }
    },
    fatal = function () {
        // this function stops grunt and make the jenkins build red.
        grunt.fail.fatal('failed');
    };

require("grunt-load-gruntfile")(grunt);

// load grunt task from another file and add it.
grunt.loadGruntfile(karmaGrunt);

//grunt needs to continue on error or warnings, that's why we have to set the force property true
grunt.option('force', true);

grunt.initConfig({
    shell: {
        options: {
            execOptions: {
                cwd: '../scripts'
            }
        },
        'rocketlauncher': {
            command: './runRocketLauncher.sh'
        }
    }
});

grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-shell');

grunt.registerTask('build-process', ['karma', 'abortHandler']);
grunt.registerTask('abortHandler', abortHandler);
grunt.registerTask('fatal', fatal);
}

这里是文件“a”:

module.exports = function (grunt) {
"use strict";
var eConfig = '../e-specs/karma.config.js',
    dConfig = '../d-specs/karma.config.js',
    cConfig = '../c-specs/karma.config.js';
grunt.initConfig({
    karma: {
        options: {
            reporters: ['progress', 'coverage', 'threshold']
        },
        c: {
            configFile: cConfig
        },
        d: {
            configFile: dConfig
        },
        e: {
            configFile: eConfig
        }
    }
});

grunt.loadNpmTasks('grunt-karma');
};

我的文件b加载任务“Karma”但是如果我只运行ai的grunt文件有3个嵌套任务(“e”,“c”,“d”)但是如果我从另一个文件加载它们,那么只有我能看到的任务是“业力”

错误是:

  
    

找不到“业力”目标。     警告:任务“业力”失败。使用--force,继续。

  

完成,但有警告。

如果我直接在文件“a”中运行相同的任务,那么任务就像魅力一样。

1 个答案:

答案 0 :(得分:2)

有一个grunt插件可以加载另一个Gruntfile:grunt-load-gruntfile

通过这种方式,您可以合并两个Grunt配置,包括已定义的任务。

以下是一个例子:

CREATE OR REPLACE PACKAGE types AS TYPE cursor_type IS REF CURSOR; END Types;/ CREATE PROCEDURE P1(DATA OUT types.cursor_type) IS BEGIN OPEN DATA FOR SELECT COLUMN_A FROM MY_TABLE; END;/ /***************Here is the procedure that I want to define but I have no idea how to do it *********/ CREATE PROCEDURE P2(MY_OUTPUT OUT types.cursor_type) IS NUM NUMBER := 0; BEGIN SELECT COUNT(*) INTO NUM FROM XXX; /*****dynamic create ref cursor array based on NUM (How to define dynamic ref cursor array???)*******/ REF CURSOR ARRAY my_array[NUM]; for i in 1... num loop P1(my_array[i]); i := i+1; end loop; /**********combine all cursors together into one output ref cursor (How to combine them together into a single output ref cusor???)******/ MY_OUTPUT := my_array[1] + my_array[2] + ... + my_array[3]; end;/

./Gruntfile.js

module.exports = function (grunt) { require("grunt-load-gruntfile")(grunt); grunt.loadGruntfile("web"); //loads the Gruntfile from the folder web/ grunt.registerTask('showConfig', "shows the current config", function(){ console.log(JSON.stringify(grunt.config(), null, 2)); }); }; 中的第二个Gruntfile。

./web/Gruntfile.js

运行module.exports = function (grunt) { grunt.config("WebConfig", "Configuration from the Gruntfile in web/Gruntfile.js"); grunt.registerTask('server', "runs the server",function(){ console.log("just shows this message"); }); }; 从第一个Gruntfile执行任务并显示配置,包括grunt showConfig中定义的参数。

正在运行./web/Gruntfile.jsgrunt server执行任务。