Search code examples
javascriptyeomanyeoman-generator

How to check if directory exists using Yeoman?


I am asking the user for a directory name, and then if the directory exists I want to ask them if they want to archive it. However I am not sure what function I can use inside Yeoman to achieve this. Here is my code.

prompting: function () {
    return this.prompt([{
        type: 'input',
        name: 'project_directory',
        message: 'What is the project directory name?'
    }, {
        type: 'confirm',
        name: 'archive',
        message: 'That project already exists. Do you want to archive it?',
        when: function (answers) {
            var destDir = 'project/' + answers.project_directory.replace(/[^0-9a-zA-Z\-.]/g, "").toLowerCase();
            var fso = new ActiveXObject("Scripting.FileSystemObject");

            //Return true if the folder exists
            return (fso.FolderExists(destDir));
        }
    }]).then(function (answers) {


    }.bind(this));
}

Solution

  • Yeoman doesn't provide any built-in methods to verify if a file or a directory exists.

    But Yeoman is just Node.js, it's just JavaScript.

    So you actually want to ask how to detect if a directory exist with Node.