2016-09-28 2 views
1

사용자에게 디렉토리 이름을 묻는 중입니다. 그런 다음 디렉토리가 있으면 아카이브 할 것인지 묻습니다. 그러나 나는 이것을 달성하기 위해 Yeoman 내부에서 어떤 기능을 사용할 수 있는지 잘 모르겠습니다. 여기 내 코드가있다.Yeoman을 사용하여 디렉토리가 있는지 확인하는 방법은 무엇입니까?

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)); 
} 

답변

3

Yeoman은 파일이나 디렉토리가 있는지 확인하기위한 기본 제공 방법을 제공하지 않습니다.

하지만 Yeoman은 Node.js 일 뿐이며 JavaScript입니다.

실제로 문의 하시려는 사람은 how to detect if a directory exist with Node입니다.

0

Generator에서 상속받은 경우 메서드를 가진 this.fs 개체가 있습니다. 사이먼 Boudrias에서 언급 한 바와 같이

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

기능이 아무튼 this.fs.exists를 ' t는 디렉토리를 위해 일한다 ... 단지 파일을 위해 –

0

사실, 보좌관 그것을 위해 내장 된 방법을 제공하지 않습니다,하지만 당신은 다음을 수행 할 수

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. 
     } 

     }) ;    
    }   
} 
관련 문제