生成之后的結構如下
projectName
|---images
|---css
|---js
|---index.html
緊接着貼出源碼
let fs = require("fs");
let path = require("path");
class CreateProject {
constructor(rootPath, projectName) {
this.rootPath = rootPath;
this.projectName = projectName;
this.subFiles = ["images", "css", "js", "index.html"];
}
initProject() {
// 1.創建站點文件夾
let projectPath = path.join(this.rootPath, this.projectName);
fs.mkdirSync(projectPath);
// 2.創建子文件和子目錄
this.subFiles.forEach(function (fileName) {
if (path.extname(fileName) === "") {
let dirPath = path.join(projectPath, fileName);
fs.mkdirSync(dirPath);
} else {
let filePath = path.join(projectPath, fileName);
fs.writeFileSync(filePath, "");
}
})
}
}
let cp = new CreateProject(__dirname, "taobao");
cp.initProject();
