2013-07-02 29 views
4

我在Grunt中写了一个简单的任务。现在,我想将此任务导出到另一个文件,并且我遇到了问题。
如何找到我的任务文件?该任务仅从网站搜索字符串并将其放入文件中。我试图加载它:grunt.loadTasks('grunt-find'); 我有文件(grunt-find)和find.js里面。但它不起作用...我可以在其他地方添加find.js吗?在另一个文件中的grunt中创建任务

在此先感谢。

回答

3

grunt.loadTask()将加载作为参数提供的目录中的每个JS文件;所以,基本上,你必须做一些事情,如:

grunt.loadTasks("tasks"); 

,你可能有一个目录名为“任务”:

project root 
|- tasks 
|--- file.js 
|- Gruntfile.js 
1

如果您发现grunt.loadTask()烦人(即“目录中唯一的”行为要保持旁边的外部任务配置外部任务定义),你可以尝试这样的事情:

module.exports = function(grunt) { 

    var env = process.env.NODE_ENV || 'dev'; 
    var _ = require('lodash'); 

    /*** External config & tasks filepaths ***/ 

    //we have 1 base config, and possibly many module-specific config 
    var configLocations = ['./grunt-config/default_config.js', './grunt-config/**/config.js']; 

    //we have 1 base tasks definition, and possibly many module-specific config 
    var tasksLocations = ['./grunt-config/default_tasks.js', './grunt-config/**/tasks.js']; 


    /* Typical project layout (matching with the globbing pattern above - adapt to your project structure) : 

    ├── Gruntfile.js 
    ├── package.json 
    ├── grunt-config 
    │ ├── homepage 
    │ │ └── config.js 
    │ ├── navigation 
    │ │ └── config.js 
    │ ├── module1 
    │ │ ├── config.js 
    │ │ └── tasks.js 
    │ ├── default_config.js 
    │ ├── default_tasks.js 
    │ └── template_module_grunt.txt 
    ├── website_directory1 
    │ ├── mdp 
    │ ├── multimedia-storage 
    │ ├── mv-commit.sh 
    │ ├── query 
    │ ├── temp 
    │ └── tmp 
    └── website_directory2 
     ├── crossdomain.xml 
     ├── css 
     ├── favicon.ico 
     ├── fonts 
     : 
     : 
     : 

    */ 

    /***************** External configuration management ***********************************/ 

    var configFiles = grunt.file.expand({ 
    filter: "isFile" 
    }, configLocations); 

    grunt.log.writeln('Gathering external configuration files'.underline.green); 
    grunt.log.writeln("configFiles : " + grunt.log.wordlist(configFiles, { 
    separator: ', ', 
    color: 'cyan' 
    })); 

    var configArray = configFiles.map(function(file) { 
    grunt.log.writeln("=> importing : " + file); 
    return require(file)(grunt, env); 
    }); 

    var config = {}; 

    configArray.forEach(function(element) { 
    config = _.merge(config, element); 
    }); 

    grunt.initConfig(config); 

    /***************** Task loading & registering *******************************************/ 
    // We load grunt tasks listed in package.json file 
    require('load-grunt-tasks')(grunt); 

    /****** External tasks registering ****************/ 
    grunt.log.writeln('Gathering external task files'.underline.green); 

    var taskFiles = grunt.file.expand({ 
    filter: "isFile" 
    }, tasksLocations); 

    grunt.log.writeln("task files : " + grunt.log.wordlist(taskFiles, { 
    separator: ', ', 
    color: 'cyan' 
    })); 

    taskFiles.forEach(function(path) { 
    grunt.log.writeln("=> loading & registering : " + path); 
    require(path)(grunt); 
    }); 

    grunt.registerTask('default', ['jshint:gruntfile', 'logConfig']); 

    grunt.registerTask('checkGruntFile', 'Default task - check the gruntfile', function() { 
    grunt.log.subhead('* Tâche par défaut - aide et vérification du gruntfile *'); 
    grunt.log.writeln('Exécutez "grunt -h" pour avoir plus d\'informations sur les tâches disponibles'); 
    grunt.log.writeln('...'); 
    grunt.log.subhead('Vérification du gruntfile...'); 
    grunt.task.run(['jshint:gruntfile']); 
    }); 

    //write the generated configuration (for debug) 
    grunt.registerTask('logConfig', 'Write the generated conf', function() { 
    //grunt.task.run(['attention:gruntfile']); 
    grunt.log.subhead('* Configuration générée : *'); 
    grunt.log.writeln(JSON.stringify(config, undefined, 2)); 
    }); 

}; 

来源:https://gist.github.com/0gust1/7683132

相关问题