Metropolis Metropolis - 2 months ago 12
Javascript Question

How can I call grunt prompt task from copy task?

I am using load-grunt-config and I have a simple copy task setup like this inside my Gruntfile.js,

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


And then inside my copy.js file I have this (ignore the invalid code. The copy task is working fine, I am just setting up this example).

'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': [{
}]
}
};
};


My prompt task looks something like this,

'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 is not finding this task though, which I think has to do with how I am calling it considering I am using load-grunt-config.

Answer

I ended up going about this differenlty. Instead of checking to see if the directory exists, I am just getting a list of the directories, passing that to the prompt question and forcing the user to choose a directory before it ever gets to the copy task.

//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')
            }]
        }
    }
};
Comments