forked from jd-opensource/nutui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createCptTpl.js
220 lines (205 loc) · 6.34 KB
/
createCptTpl.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const conf = require('../src/config.json');
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const copy = require('copy');
const createPkgDeclare = require('./createPkgDeclare');
let sorts = [...conf.sorts];
let newCpt = {
version: '1.0.0'
};
function init() {
inquirer.prompt([
{
type: 'input',
name: 'name',
message: '组件英文名(每个单词的首字母都大写,如TextBox):',
validate(value) {
const repeat = conf.packages.find(item => {
return item.name === value;
})
if (repeat) {
return '该组件名已存在!';
}
const pass = value && value.match(/^[A-Z]/);
if (pass) {
return true;
}
return '不能为空,且每个单词的首字母都要大写,如TextBox';
}
},
{
type: 'input',
name: 'chnName',
message: '组件中文名(十个字以内):',
validate(value) {
const pass = value && value.length <= 10;
if (pass) {
return true;
}
return '不能为空,且不能超过十个字符';
}
},
{
type: 'input',
name: 'desc',
message: '组件描述(五十个字以内):'
},
{
type: 'rawlist',
name: 'type',
message: '请选择组件类型(输入编号):',
choices: ['component', 'filter', 'directive', 'method'],
validate(value) {
const pass = value && /^[1-4]$/.test(value);
if (pass) {
return true;
}
return '输入有误!请输入选项前编号';
}
},
{
type: 'rawlist',
name: 'sort',
message: '请选择组件分类(输入编号):',
choices: sorts,
validate(value) {
const pass = /^[1-7]$/.test(value);
if (pass) {
return true;
}
return '输入有误!请输入选项前编号';
}
},
{
type: 'confirm',
name: 'showDemo',
message: '是否需要DEMO页面?',
default: true
},
{
type: 'input',
name: 'author',
message: '组件作者(可署化名):'
}
]).then(answers => {
answers.sort = String(sorts.indexOf(answers.sort));
newCpt = Object.assign(newCpt, answers);
createDir();
});
}
function createIndexJs() {
if (newCpt.type == 'method') return;
return new Promise((resolve, reject) => {
const nameLc = newCpt.name.toLowerCase();
let content = `import ${newCpt.name} from './${nameLc}.vue';
import './${nameLc}.scss';
${newCpt.name}.install = function(Vue) {
Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
};
export default ${newCpt.name}`;
let content2 = `${newCpt.name}.install = function(Vue) {
Vue.${newCpt.type}(${newCpt.name}.name, ${newCpt.name});
};
export default ${newCpt.name}`;
const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
const filePath = path.join(dirPath, `index.js`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
if (newCpt.type == 'filter' || newCpt.type == 'directive') {
content = content2;
}
fs.writeFile(filePath, content, (err) => {
if (err) throw err;
resolve(`生成index.js文件成功`);
});
});
}
function createVue() {
return new Promise((resolve, reject) => {
const nameLc = newCpt.name.toLowerCase();
let content = `<template>
<div class="nut-${nameLc}"></div>
</template>
<script>
export default {
name:'nut-${nameLc}',
props: {
},
data() {
return {};
},
methods: {
}
}
</script>`;
const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
const filePath = path.join(dirPath, `${nameLc}.vue`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFile(filePath, content, (err) => {
if (err) throw err;
resolve(`生成${newCpt.name}.vue文件成功`);
});
});
}
function createScss() {
return new Promise((resolve, reject) => {
const nameLc = newCpt.name.toLowerCase();
let content = `.nut-${nameLc}{
}`;
const dirPath = path.join(__dirname, `../src/packages/${nameLc}`);
const filePath = path.join(dirPath, `${nameLc}.scss`);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
fs.writeFile(filePath, content, (err) => {
if (err) throw err;
resolve(`生成${newCpt.name}.scss文件成功`);
});
});
}
function createDir() {
const nameLc = newCpt.name.toLowerCase();
const destPath = path.join(__dirname, '../src/packages/' + nameLc);
if (!fs.existsSync(destPath)) {
fs.mkdirSync(destPath);
}
copy(path.join(__dirname, './__template__/**.*'), destPath, function (err, file) {
if (err) {
console.log('拷贝__template__目录失败!');
}
createNew();
});
}
function addToPackageJson() {
return new Promise((resolve, reject) => {
conf.packages.push(newCpt);
const dirPath = path.join(__dirname, `../src/`);
const filePath = path.join(dirPath, `config.json`);
fs.writeFile(filePath, JSON.stringify(conf, null, 2), (err) => {
if (err) throw err;
resolve(`修改config.json文件成功`);
});
});
}
function createNew() {
createIndexJs().then(() => {
if (newCpt.type == 'component' || newCpt.type == 'method') {
return createVue();
} else {
return;
}
}).then(() => {
return createScss();
}).then(() => {
return addToPackageJson();
}).then(() => {
createPkgDeclare(newCpt.name);
console.log('组件模板生成完毕,请开始你的表演~');
process.exit();
});
}
init();