Search code examples
gulpprompt

How to create directory with name given by a prompt question with gulp


I would like to run a gulpfile.js task which asks in prompt the directory name to create it inside the build folder.

Structure

  • gulfile.js
  • build/
    • foo/ <-- directory name given by a question

gulpfile task

gulp.task( 'questions',  function( callback ) {
    return gulp.src('build')
        .pipe( prompt.prompt({
            type: 'input',
            name: 'dir_name',
            message: 'What is directory name ?'
        }, function( res ){
            //console.log(res.dir_name);
            //make directory called res.dir_name in build/
        }))
        .pipe(gulp.dest('build'));
});

How can I do this ?


Solution

  • I found a solution thanks gulp-prompt package. This example copy content base folder in build folder.

    const prompt = require('gulp-prompt');
    
    var my_dirname, project_build_path ;
    
    gulp.task( 'questions',  function( callback ) {
        return gulp.src('./base/**/*')
            .pipe( prompt.prompt({
                type: 'input',
                name: 'dir_name',
                message: 'What is directory name ?'
            }, function( res ){
                my_dirname = res.dir_name;
                project_build_path = 'build/' + my_dirname;
                gulp.src( './base/**/*' ).pipe( gulp.dest( project_build_path ) );
            }));
    });