Create task in grunt in another file

I wrote a simple task in Grunt. Now I would like to export this task to another file, and I have a problem with this.
How to find a file with my task? This task simply searches for a line from the website and puts it in a file. I am trying to download it: grunt.loadTasks('grunt-find'); I have a file (grunt-find) with find.js inside. But it does not work ... Can I add find.js to another location?

Thanks in advance.

+4
source share
2 answers

grunt.loadTask() will load each JS file into the directory provided as an argument; so basically you need to do something like:

 grunt.loadTasks("tasks"); 

and you may have a directory called "tasks":

 project root |- tasks |--- file.js |- Gruntfile.js 
+3
source

If you find that the "directory-only" behavior of grunt.loadTask() annoying (that is, it wants to keep the definitions of external tasks next to the configuration of external tasks), you can try something like this:

 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)); }); }; 

Source: https://gist.github.com/0gust1/7683132

+1
source

Source: https://habr.com/ru/post/1489358/


All Articles