Skip to content

Commit 65737bb

Browse files
committed
Merge pull request #12 from BiYuqi/use-node-create-page
Use node create page
2 parents c877de3 + 5681d00 commit 65737bb

File tree

16 files changed

+279
-24
lines changed

16 files changed

+279
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## 迭代日志
2+
2019.02.06
3+
* 新增命令行创建页面[README](./bin/README.md)
4+
25
2018.10.22
36
* 增加个性化模板案例[layoutAuth](https://github.com/BiYuqi/webpack-seed/tree/master/src/layout/layoutAuth)
47

INSTRODUCTION.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ eg:
3737
* 页面支持ejs模板开发,模板文件建议统一规划到一个文件夹(本项目目前放在[templates/](https://github.com/BiYuqi/webpack-seed/tree/master/src/templates)注:views/目录只能放页面模块,不可放模板) 具体请看 [模板](https://github.com/BiYuqi/webpack-seed/blob/master/src/templates/index.test.ejs) [使用](https://github.com/BiYuqi/webpack-seed/blob/master/src/views/index/index.js#L7)
3838
3939
## 目录结构 (Source)
40-
40+
* **bin**
41+
* ---命令行配置文件,支持创建页面
4142
* **build/**
4243
* ---config.js 开发,打包基础配置(包含输出文件名, 路径配置等都在此配置)
4344
* ---utils.js 多入口,多页面基础配置

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747
- 支持ES6编写源码,编译生成生产代码
4848
- 开发(生产)环境代码自动注入页面, 专注于开发
4949
- 编译后的程序不依赖于外部的资源, 可自动替换线上资源地址
50-
- 开箱即用的单元测试环境(当然了,测试用例还得您自己写)
50+
- 开箱即用的单元测试环境(当然了,测试用例还得您自己写
5151
- ...
5252

5353
注:本项目不依赖Jquery. lib库引入jquery仅仅是为了演示该项目可以自动加载第三方脚本作为链接使用, 如果不需要,在src/common/libs.js配置文件中移除即可
5454

55+
5556
## What do we want?
5657

5758
展示下页面呈现结果(页面资源加载逻辑),这可能就是我们想要的模块化吧:sparkles:
@@ -131,6 +132,14 @@ npm run ci
131132
```
132133
注意:本项目有提交代码前跑lint的功能,请看package.json`husky`字段配置
133134

135+
**支持命令行创建页面**
136+
137+
该命令支持创建页面四件套,帮助快速构建页面,不再一个文件一个文件写
138+
[README](./bin/README.md)
139+
```js
140+
npm run new
141+
```
142+
134143
## Coding Standard
135144

136145
[INSTRODUCTION](https://github.com/BiYuqi/webpack-seed/blob/master/INSTRODUCTION.md)

bin/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## 运行
2+
3+
创建新页面, 暂时支持两种模板
4+
```js
5+
npm run new
6+
```
7+
## screenShots
8+
![](./screenShots/step.png)
9+
![](./screenShots/screen.png)
10+
11+
## 模板
12+
页面种类config配置即可
13+
14+
```js
15+
目前:standed & notStanded
16+
standed: 含有header footer
17+
notStanded: 不含有header footer
18+
```
19+
20+
## TODO
21+
22+
- [ ] 支持多目录嵌套生成
23+
- [ ] 重构代码

bin/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#! /usr/bin/env node
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const program = require('commander')
6+
const inquirer = require('inquirer')
7+
const handlebars = require('handlebars')
8+
9+
const descCheck = require('./middleware/desc')
10+
const { getFileName, log, successLog, errorLog } = require('./utils/utils')
11+
const templeteConfig = require('./templates/config')
12+
const writeFiles = require('./middleware/writefile')
13+
14+
program.action(() => {
15+
log(`
16+
0. 模块是基于views层创建.
17+
1. 文件名不包含数字, 模块名即为文件名, 不能与页面现存重复.
18+
2. 常规的就是直接输入文件名(模块名)即可自动创建页面.
19+
3. e.g. 输入new-page 即可交互式创建该页面.
20+
4. 创建嵌套页面, 需要输入嵌套规则 path/path/path.[暂未实现 => TODO]
21+
`)
22+
inquirer
23+
.prompt([
24+
{
25+
name: 'description',
26+
message: '请输入页面模块名称',
27+
validate: descCheck
28+
},
29+
{
30+
type: 'list',
31+
name: 'templete',
32+
message: '请选择页面模板',
33+
choices: Object.keys(templeteConfig)
34+
},
35+
{
36+
name: 'title',
37+
message: '请输入页面title(非必填,建议填写)'
38+
}
39+
])
40+
.then(answer => {
41+
const { description, templete, title } = answer
42+
const { fileName } = getFileName(description)
43+
44+
fs.mkdir(path.resolve(__dirname, '../', `src/views/${fileName}`), err => {
45+
if (err) {
46+
errorLog(err)
47+
throw err
48+
}
49+
successLog('创建目录成功')
50+
const tplContent = fs.readFileSync(path.resolve(__dirname, 'templates/tpl.txt')).toString()
51+
const indexContent = fs.readFileSync(path.resolve(__dirname, 'templates/index.txt')).toString()
52+
53+
const parseTplResult = handlebars.compile(tplContent)({
54+
pageTitle: title ? title : '',
55+
templateDir: templeteConfig[templete].dir,
56+
templateName: templeteConfig[templete].name,
57+
fileName
58+
})
59+
const parseIndexResult = handlebars.compile(indexContent)({
60+
fileName
61+
})
62+
63+
writeFiles(`src/views/${fileName}/tpl.js`, parseTplResult)
64+
writeFiles(`src/views/${fileName}/index.js`, parseIndexResult)
65+
writeFiles(`src/views/${fileName}/${fileName}.ejs`)
66+
writeFiles(`src/views/${fileName}/${fileName}.scss`)
67+
successLog('文件创建成功')
68+
})
69+
})
70+
})
71+
program.parse(process.argv)

bin/middleware/desc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { getDirectoryName } = require('../utils/utils')
2+
3+
const hasNest = name => {
4+
return name.indexOf('/') > -1
5+
}
6+
7+
const descCheck = desc => {
8+
if (!/^[a-z_-]+$/.test(desc)) {
9+
return '文件仅支持字母,下划线,中划线,暂不支持数字, 且不能为空'
10+
}
11+
const result = getDirectoryName()
12+
if (result.includes(desc)) {
13+
return '文件夹已存在,请换个文件名重试'
14+
}
15+
return true
16+
}
17+
18+
module.exports = descCheck

bin/middleware/writefile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
const { log } = require('../utils/utils')
4+
5+
module.exports = (address, template = '') => {
6+
fs.writeFileSync(path.resolve(__dirname, '../../', address), template, 'utf8')
7+
log('create: ' + address)
8+
}

bin/screenShots/screen.png

54.3 KB
Loading

bin/screenShots/step.png

192 KB
Loading

bin/templates/config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// 模板文件夹名字 & 文件名
2+
// 供生成页面使用
3+
module.exports = {
4+
standed: {
5+
dir: 'layout',
6+
name: 'layout'
7+
},
8+
notStanded: {
9+
dir: 'layoutAuth',
10+
name: 'layoutAuth'
11+
}
12+
}

0 commit comments

Comments
 (0)