-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
80 lines (66 loc) · 1.81 KB
/
template.js
File metadata and controls
80 lines (66 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* pages模版快速生成脚本,执行命令 npm run tep `文件名`
*/
const fs = require('fs')
const dirName = process.argv[2]
if (!dirName) {
console.log('文件夹名称不能为空!')
console.log('事例: npm run tep test')
process.exit(0)
}
const dirArrs = dirName.split('/')
// 如果只有一个目录结构,则默认在src/containers/views下面生成
let directory = `containers/views/${dirName}`
let fileName = dirName
if (dirArrs.length > 1) {
directory = dirName
fileName = dirArrs[dirArrs.length - 1]
}
const titleCase = str => {
const array = str.toLowerCase().split()
for (let i = 0; i < array.length; i++) {
array[i] =
array[i][0].toUpperCase() + array[i].substring(1, array[i].length)
}
const string = array.join(' ')
return string
}
const lowerCase = str => {
const string = str.toLowerCase().split()
return string
}
const indexTep = `import * as React from 'react'
import {IProps} from './type'
import './style.scss';
class ${titleCase(fileName)} extends React.Component<IProps> {
constructor(props) {
super(props);
}
componentDidMount() {
// 数据挂载之后 可以操作数据和dom
}
componentWillUnmount() {
// 数据挂载之前 可以操作数据 不可以操作dom
}
render() {
return (
<div className="${lowerCase(fileName)}-page">
${fileName}
</div>
)
}
}
export default ${titleCase(fileName)}
`
// scss文件模版
const scssTep = ``;
const typeTep = `export interface IProps {}
export interface IState {}
`
fs.mkdirSync(`./src/${directory}`) // mkdir $1 创建文件夹
process.chdir(`./src/${directory}`) // cd $1 进入该目录
fs.writeFileSync('index.tsx', indexTep)
fs.writeFileSync('style.scss', scssTep)
fs.writeFileSync('type.d.ts', typeTep)
console.log(`模版${fileName}已创建`)
process.exit(0)