From b385772d9ed25564093d01a119daaf152214d366 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Tue, 30 Mar 2021 14:29:38 +0800 Subject: [PATCH 1/2] feat: support custom template dir --- CHANGELOG.md | 5 +++++ lib/Generator.js | 37 ++++++++++++++++++++----------------- lib/configs/formatFile.js | 2 +- package.json | 2 +- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbd2c67..94dc1b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.1.32 + +- [feat] support templateDir +- [fix] custom block container + ## 1.1.31 - [fix] 使用 webpack 打包时,找不到 `lib/configs/` 的配置文件 diff --git a/lib/Generator.js b/lib/Generator.js index 9858b47..23d3339 100644 --- a/lib/Generator.js +++ b/lib/Generator.js @@ -16,13 +16,14 @@ module.exports = class Generator { rootDir = process.cwd(), configFile = '.template/scaffold.json', template, + templateDir, useLocalBlocks, }) { this.useLocalBlocks = !!useLocalBlocks; this.rootDir = rootDir; this.config = this.getScaffoldConfig(configFile); this.blockTemp = path.join(this.rootDir, './tmp'); - this.templateDir = path.join(__dirname, 'scaffold'); + this.templateDir = templateDir || path.join(__dirname, 'scaffold'); this.customTemplate = template ? path.resolve(rootDir, template) : path.join(rootDir, '.template'); this.pkg = this.getProjectPkg(); @@ -325,8 +326,7 @@ module.exports = class Generator { const pageName = _.upperFirst(name); const pageDir = `src/pages/${pageName}`; const blocks = await this.formatBlocks(packages, pageDir); - - const templatePath = path.resolve(__dirname, `./template/${container || defaultContainer}.ejs`); + const templatePath = container && container !== defaultContainer ? path.resolve(this.rootDir, '.template', container) : path.resolve(__dirname, `./template/${defaultContainer}.ejs`); if (!fse.existsSync(templatePath)) { throw new Error(`fail to read template ${container}`); } @@ -357,22 +357,25 @@ module.exports = class Generator { generateFile() { this.copyFiles.forEach(({ source, targetDir, type }) => { if (type === 'template') { - const stat = fse.lstatSync(source); + const stat = fse.existsSync(source) && fse.lstatSync(source); let files = []; - - if (stat.isFile()) { - files = [{ - sourcePath: source, - targetPath: targetDir, - }]; + if (stat) { + if (stat.isFile()) { + files = [{ + sourcePath: source, + targetPath: targetDir, + }]; + } else { + const allFiles = readDirFiles(source); + files = allFiles.map((item) => { + return { + sourcePath: path.join(source, item), + targetPath: path.join(targetDir, item), + }; + }); + } } else { - const allFiles = readDirFiles(source); - files = allFiles.map((item) => { - return { - sourcePath: path.join(source, item), - targetPath: path.join(targetDir, item), - }; - }); + debug(`${source} is not exists in template folder`); } files.forEach((item) => { diff --git a/lib/configs/formatFile.js b/lib/configs/formatFile.js index a9f0ab2..4f0229d 100644 --- a/lib/configs/formatFile.js +++ b/lib/configs/formatFile.js @@ -9,7 +9,7 @@ module.exports = (api) => { api.writeToProject(`./src/${entryFile}`, entryCode); } [ - '_gitignore', 'public', 'src/global.scss', 'README.md', '.vscode', + '_gitignore', 'public', 'src/global.scss', 'src/global.less', 'src/global.css', 'README.md', '.vscode', '_prettierrc.js', '_prettierignore', '_editorconfig', '_eslintignore', '_stylelintignore', '_stylelintrc.js', diff --git a/package.json b/package.json index 0cce62f..a858fbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ice-scaffold-generator", - "version": "1.1.31", + "version": "1.1.32", "description": "scaffold generator", "main": "lib/index.js", "files": [ From a11fe8767d45e1eede03f1b3ecdc65d7df459c3d Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Tue, 30 Mar 2021 14:34:08 +0800 Subject: [PATCH 2/2] chore: lint --- lib/Generator.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Generator.js b/lib/Generator.js index 23d3339..7b3b869 100644 --- a/lib/Generator.js +++ b/lib/Generator.js @@ -326,7 +326,9 @@ module.exports = class Generator { const pageName = _.upperFirst(name); const pageDir = `src/pages/${pageName}`; const blocks = await this.formatBlocks(packages, pageDir); - const templatePath = container && container !== defaultContainer ? path.resolve(this.rootDir, '.template', container) : path.resolve(__dirname, `./template/${defaultContainer}.ejs`); + const templatePath = container && container !== defaultContainer + ? path.resolve(this.rootDir, '.template', container) + : path.resolve(__dirname, `./template/${defaultContainer}.ejs`); if (!fse.existsSync(templatePath)) { throw new Error(`fail to read template ${container}`); }