-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
66 lines (62 loc) · 1.75 KB
/
cli.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
let fs = require('fs')
let basepath = 'src/components/';
let moment = require('moment');
let cptName = process.argv.splice(2)[0];
let path = cptName.split('/');
let name = path[path.length - 1];
let file = [];
async function creatCpt() {
try {
await exists(); // 检测文件夹
await writeFile(await readFile()); //写入组件
}
catch (err) {
console.error(err);
}
}
let exists = function () {
return new Promise((res) => {
(async ()=> {
for (let fileName of path) {
fs.existsSync(basepath + fileName) ? basepath = `${basepath}${fileName}/` : await mkdir(fileName);
}
res(basepath);
})()
})
}
let mkdir = function (a) {
return new Promise((res, rej) => {
fs.mkdir(basepath + a, (err) => {
if (err) rej(err);
basepath = `${basepath}${a}/`
res(basepath);
});
})
}
let reads = [`${basepath}template/index.tsx`, ];//要读取的文件
let readFile = function () {
return new Promise((res) => {
for (let a of reads) {
let text = fs.readFileSync(a).toString();
text = text.replace(/time/g, moment().format('YYYY/MM/DD'))
.replace(/temp/g, name);
file.push(text)
}
res(file);
})
}
let writes = [ `index.tsx`,`index.less`,];
let writeFile = function (file) {
return new Promise((res, rej) => {
(async function () {
for (let template of writes) {
await fs.writeFile(`${basepath}${template}`,
template == writes[0] ?file[0]: '', (err) => {
if (err) rej(err)
})
}
res('succ');
})()
})
}
creatCpt()