-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcreateFile.js
More file actions
134 lines (131 loc) · 3.53 KB
/
createFile.js
File metadata and controls
134 lines (131 loc) · 3.53 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
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
/**
* @author yanglei
* @date 201600928
* @fileoverview 命令行创建文件
*/
const fs = require("fs"),
_path = require("path"),
moment = require("moment");
let propram = require("commander"),
path = _path.join(__dirname),
type = "js",
author = null,
catalog = null,
name = null,
myCommand = propram.version("0.0.1");
myCommand
.option("-c, --create", "创建文件", (path) => {
createFile(path);
})
.option("-d, --delete", "删除文件", (path) => {
deleteFile(path)
})
.option("-t, --type <type>", "添加文件类型", (type) => {
addType(type)
})
.option("-l, --catalog <catalog>", "添加文件目录", (catalog) => {
addCatalog(catalog)
})
.option("-a, --author <author>", "添加作者", (author) => {
addAuthor(author)
})
.option("-n, --name <name>", "添加文件名称", (n) => {
addName(n)
})
.option("--ca, --createAll", "创建所有文件", () => {
createAllFile()
})
.option("--da, --deleteAll", "删除所有文件", () => {
deleteAllFile()
})
.option("--cs, --createSome", "创建部分文件", () => {
createSomeFile()
})
.option("--ds, --deleteSome", "删除部分文件", () => {
deleteSomeFile()
})
.parse(process.argv);
function addType(t) {
type = t;
}
function addAuthor(a){
author = a;
}
function addCatalog(c) {
catalog = c;
}
function addName(n) {
name = n;
}
function existFile(path) {
return fs.existsSync(path);
}
function createFile(path) {
if(!existFile(path)) {
let p = path.split("\\");
let _p = p[0];
for(let i = 0; i< p.length - 1; i++) {
if(existFile(_p)) {
_p = _p.concat(`\\${p[i + 1]}`);
} else {
fs.mkdirSync(_p);
_p = _p.concat(`\\${p[i + 1]}`);
}
}
}
fs.writeFileSync(path, `/**
* @author ${author}
* @date ${moment(new Date()).format("YYYY-MM-DD")}
* @fileoverview
*/
module.exports = {}`);
console.log(`已经创建文件,${path}`);
}
function deleteFile(path) {
fs.unlinkSync(path);
console.log(`已经删除文件,${path}`);
}
function createAllFile() {
if(!catalog) {
console.log("未填写目录");
return;
}
if(!name) {
console.log("未填写文件名");
return;
}
let _manage = _path.join(path, `/controllers/manage/${catalog}/${name}.${type}`),
_p = _path.join(path, `/controllers/path/${name}.${type}`),
_filter = _path.join(path, `/filters/${catalog}/${name}.${type}`);
createFile(_manage);
createFile(_p);
createFile(_filter);
}
function deleteAllFile() {
let manage = `${path}/controllers/manage/${catalog}`,
path = `${path}/controllers/path`,
filter = `${path}/filters`;
deleteFile(manage, name);
deleteFile(path, name);
deleteFile(filter, name);
}
function createSomeFile() {
if(!catalog) {
console.log("未填写目录");
return;
}
if(!name) {
console.log("未填写文件名");
return;
}
let _manage = _path.join(path, `/controllers/manage/${catalog}/${name}.${type}`),
_filter = _path.join(path, `/filters/${catalog}/${name}.${type}`);
createFile(_manage);
createFile(_filter);
}
function deleteSomeFile() {
let manage = `${path}/controllers/manage/${catalog}`,
filter = `${path}/filters`;
deleteFile(manage, name);
deleteFile(filter, name);
}