简体   繁体   中英

How to disable a task in Grunt?

When grunt.loadNpmTasks is used, a grunt task is automatically available to the command line. It can be useful, but sometimes, I would like this task to be private, so it can be used whithin the Grunt file but not available to the command line.

Here is a contrived example. If I do :

module.exports = function(grunt) {

    grunt.initConfig({  
        clean: {
            test: ['test'],
            release: ['release']
        },  
    });

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

    grunt.registerTask('build', 'Build the project.', function() {
        console.log("building project");
    });
    grunt.registerTask('release', ['clean:release', 'build']);

};

... I can use the following command :

$ grunt release

However, this one is also available, and both clean:release and clean:test will be executed:

$ grunt clean

I do not want that. I want to control what can be called from the command line, since I may not have foreseen some undesirable effects if the user directly calls some tasks or subtasks.

I thought about registering a new clean task to supersedes the main one, and then choose what to call when clean is invoked (or to call nothing at all), but it does not work well since it cannot call the original clean task:

grunt.registerTask('clean', ['clean:release']);

Use grunt.task.renameTask

var ticks = +new Date();
var clean = 'clean-' + ticks;

grunt.task.renameTask('clean', clean);
grunt.registerTask('release', [clean + ':release', 'build']);
grunt.config.set(clean, grunt.config.get('clean'));

Copying the configuration over is important if you want to preserve the target s configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM