1

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));
}
Metropolis
  • 6,542
  • 19
  • 56
  • 86

3 Answers3

3

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.

Community
  • 1
  • 1
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
1

If you inherited from Generator, you have this.fs object that has exists() method.

module.exports = class extends Generator {
      /* ... */
  default() {
    this.alreadyCopied = this.fs.exists(this.destination('myfile.txt'));
  }
}
mrmyke
  • 19
  • 1
0

Actually, as mentioned by Simon Boudrias, Yeoman doesn't provide a built-in method for it, but you can do the following:

var Generator = require('yeoman-generator');
var fs = require('fs');

module.exports = class extends Generator{

    checkIfFolderExists(){    

        fs.stat('YourDirectoryHere'), function(error, stats){            

        if(stats!=undefined && stats.isDirectory()){
           // your directory already exists.          
        }else{
           // create your directory.
        }

        }) ;            
    }         
}
Fábio Filho
  • 679
  • 7
  • 10