繁体   English   中英

如何从复制任务中调用grunt提示任务?

[英]How can I call grunt prompt task from copy task?

我正在使用load-grunt-config,并且在Gruntfile.js中有一个简单的复制任务设置,

grunt.registerTask('copy-css', 'Blah.', ['copy:css'])

然后在我的copy.js文件中有这个(忽略无效的代码。复制任务运行正常,我只是在设置此示例)。

'use strict';

module.exports = function(grunt, options) {
    if(!grunt.file.exists(foldername)) { 
        //I NEED TO RUN A PROMPT HERE BUT THIS IS NOT WORKING
        grunt.task.run('prompt:directory-exists');
    }

    return {
        'css': {
            'files': [{
            }]
        }
    };
};

我的即时任务看起来像这样,

'use strict';

module.exports = {
    'directory-exists': {
        'options': {
            'questions': [{
                'type': 'confirm',
                'message': 'That folder already exists. Are you sure you want to continue?',
                'choices': ['Yes (overwrite my project files)', 'No (do nothing)']
            }]
        }
    }
};

Grunt并未找到此任务,考虑到我正在使用load-grunt-config,我认为这与我如何称呼它有关。

如果注册任务,则可以在grunt config中访问任务(如提示符)。

这是您可以在Gruntfile.js中创建的任务的示例:

grunt.registerTask('myowntask', 'My precious, my own task...', function(){
    // init
    var runCopy = true;

    // You'll need to retrieve or define foldername somewhere before...
    if( grunt.file.exists( foldername )) {
        // If your file exists, you call the prompt task, with the specific 'directory-exists' configuration you put in your prompt.js file
        grunt.task.call('prompt:directory-exists');
    }

}

然后,运行grunt myowntask

我最终谈到了这种差异。 我没有检查目录是否存在,而是获取目录列表,将其传递给提示问题,并强迫用户选择目录,然后再执行复制任务。

//Get directories
var projects = grunt.file.expand({filter: "isDirectory", cwd: "project"}, ["*"]);

//Format these for grunt-prompt
grunt.option('global.project.list', projects.map(function (proj_name) { return { 'value': proj_name, 'name': proj_name}; }));

//Prompt.js
return {
    //Prompt the user to choose their directory
    'project-name': {
        'options': {
            'questions': [{
                config: 'my.selection', 
                type: 'list',
                message: 'Choose directory to copy to?', 
                choices: grunt.option('global.project.list')
            }]
        }
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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