-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathset.js
78 lines (73 loc) · 2.13 KB
/
set.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
/**
* Created by 51375 on 2017/7/8.
*/
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 writes = [`${name}.js`, `${name}.html`, `${name}.less`, `index.js`];
let reads = [`${basepath}cptTemp/index.js`, `${basepath}cptTemp/cptTemp.js`];
let file = [];
let author = require('os').homedir().split('\\').pop();
//检测是否存在文件夹
let exists = function () {
return new Promise((res, rej) => {
(async function () {
for (let a of path) {
fs.existsSync(basepath + a) ? basepath = `${basepath}${a}/` : await mkdir(a);
}
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 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)
.replace(/author/g, author)
file.push(text)
}
res(file);
})
}
//生成文件,并填入之前读取的文件内容
let writeFile = function (file) {
return new Promise((res, rej) => {
(async function () {
for (let a of writes) {
await fs.writeFile(`${basepath}${a}`,
a == writes[3] ? file[0] : a == writes[0] ? file[1] : '', (err) => {
if (err) rej(err)
})
}
res('succ');
})()
})
}
async function creatCpt() {
try {
await exists();
await readFile()
await writeFile(await readFile());
return console.log(`Successfully created ${name} component`)
}
catch (err) {
console.error(err);
}
}
creatCpt();