-
Notifications
You must be signed in to change notification settings - Fork 4
/
pageTemplate.js
170 lines (139 loc) · 3.53 KB
/
pageTemplate.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* pages模版快速生成脚本,执行命令 npm run page `文件名`
*/
const fs = require('fs');
const dirName = process.argv[2];
if (!dirName) {
console.log('文件夹名称不能为空!');
console.log('示例:npm run page test');
process.exit(0);
}
if (fs.existsSync(`./src/pages/${dirName}`)) {
console.log(`${dirName} 文件夹已存在!`);
process.exit(0);
}
// 页面模版
const indexTep = `import Taro, { Component } from '@tarojs/taro';
import { View } from '@tarojs/components';
import { connect } from '@tarojs/redux';
import styles from './index.scss';
@connect(({${dirName}, loading}) => ({
${dirName},
isLoad: loading.effects['${dirName}/load'],
}))
class Page extends Component {
config = {
navigationBarTitleText: '${dirName}',
};
state = {};
componentDidMount = () => {
const { dispatch } = this.props;
dispatch({
type: '${dirName}/load',
});
};
render() {
const {
${dirName}: { test },
} = this.props;
return (
<View className={styles.${dirName}Page}>
{test}
</View>
);
}
}
export default Page;
`;
// scss文件模版
const scssTep = `@import '../../base.scss';
.${dirName}Page {
color: red;
}
`;
// model文件模版
const modelTep = `import reducers from '@/utils/reducers';
import api from '@/server/api';
const namespace = '${dirName}';
const selectState = state => state[namespace];
export default {
namespace,
state: {
test: namespace,
},
reducers,
effects: {
*load(_, { call, put, select }) {
const { test } = yield select(selectState);
const response = yield call(api.demo, { test });
switch (response.code) {
case 0:
yield put({
type: 'save',
payload: {
test: response.content,
},
});
break;
default:
break;
}
},
},
};
`;
// service页面模版
// const serviceTep = `import Request from '../../utils/request';
// export const demo = data => Request({
// url: '路径',
// method: 'POST',
// data,
// });
// `;
fs.mkdirSync(`./src/pages/${dirName}`); // mkdir $1
let appTemplateArry = fs.readFileSync(`./src/app.js`, 'utf-8').split('// <?=pageList=?>');
let pageList = fs.readdirSync('./src/pages');
const index = pageList.indexOf('index');
pageList.splice(index, 1);
pageList.unshift('index');
appTemplateArry[1] = '';
pageList.forEach(item => {
appTemplateArry[1] =
appTemplateArry[1] +
`'pages/${item}/index',
`;
});
fs.writeFileSync(
'./src/app.js',
appTemplateArry.join(`// <?=pageList=?>
`)
);
let modelsTemplate = '';
pageList.forEach(item => {
modelsTemplate =
modelsTemplate +
`import ${item} from '@/pages/${item}/model';
`;
});
modelsTemplate = modelsTemplate + `import globalModel from './globalModel';`;
modelsTemplate =
modelsTemplate +
`
export default [${pageList.join(', ')} , globalModel];`;
fs.writeFileSync('./src/models/index.js', modelsTemplate);
process.chdir(`./src/pages/${dirName}`); // cd $1
fs.writeFileSync('index.jsx', indexTep);
fs.writeFileSync('index.scss', scssTep);
fs.writeFileSync('model.js', modelTep);
// fs.writeFileSync('service.js', serviceTep);
console.log(pageList);
console.log(`页面 ${dirName} 已创建!`);
// function 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;
// }
process.exit(0);