Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support custom template dir #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.1.32

- [feat] support templateDir
- [fix] custom block container

## 1.1.31

- [fix] 使用 webpack 打包时,找不到 `lib/configs/` 的配置文件
Expand Down
39 changes: 22 additions & 17 deletions lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -325,8 +326,9 @@ 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}`);
}
Expand Down Expand Up @@ -357,22 +359,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) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/configs/formatFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ice-scaffold-generator",
"version": "1.1.31",
"version": "1.1.32",
"description": "scaffold generator",
"main": "lib/index.js",
"files": [
Expand Down