利用NodeJS生成项目文件模板

生成之后的结构如下

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();
posted @ 2020-11-15 13:19  BNTang  阅读(769)  评论(0编辑  收藏  举报