-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.js
More file actions
233 lines (202 loc) · 6.89 KB
/
framework.js
File metadata and controls
233 lines (202 loc) · 6.89 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
#! /usr/bin/env node
var fs = require('fs');
var path = require('path');
var program = require('commander');
var inquirer = require('inquirer');
var unzip = require("unzip2");
var rimraf = require("rimraf");
const { exec } = require('child_process');
program
.command('init')
.description('选择框架')
.action(function () {
var prompt = [];
var config = {};
prompt.push({
type: 'list',
name: 'chooseFramework',
message: '请选择你需要的框架',
choices: ['vuejs', 'backbone']
});
prompt.push({
type: 'input',
name: 'path',
message: "请输入生成框架的目录",
default: function () {
return process.cwd();
},
validate: function(path) {
if (path==process.cwd() ) {
return "请先输入地址,并且避免使用默认的 Framework 文件地址";
}
if (fs.existsSync(path)) {
return true;
}else{
fs.mkdirSync(path);
var isDir = fs.statSync(path).isDirectory();
if (isDir) {
return true;
}else{
fs.rmdirSync(path);
}
}
return '请输入合法地址!';
}
});
inquirer.prompt(prompt).then(answers => {
config = Object.assign(config, answers);
console.log("config",config);
var fileName = "vue_space.zip";
var sourceFile = path.join(__dirname,"src/vuejs",fileName);
var destPath = path.join(answers.path, fileName);
var readStream = fs.createReadStream(sourceFile);
var writeStream = fs.createWriteStream(destPath);
readStream.pipe(writeStream);
writeStream.on('finish', () => {
console.log("Zip移动完成");
var unzipExtract= unzip.Extract({ path: answers.path})
fs.createReadStream(destPath).pipe(unzipExtract);
fs.unlinkSync(destPath);
console.log("删除zip了")
});
});
})
program
.command('addpage')
.description('增加页面')
.action(function () {
var prompt = [];
var config = {};
prompt.push({
type: 'input',
name: 'name',
message: "请输入html页面(module)的名字",
default: function () {
return 'index';
},
validate: function(value) {
if (/^[0-9a-zA-Z-_]+$/.test(value)) {
return true;
}
return '页面名字只能包括数字字母和-_';
}
});
prompt.push({
type: 'input',
name: 'path',
message: "请输入框架根目录",
default: function () {
return process.cwd();
},
validate: function(path) {
if (path==process.cwd() ) {
return "请先输入地址,并且避免使用默认的 Framework 文件地址";
}
if (fs.existsSync(path)) {
return true;
}else{
fs.mkdirSync(path);
var isDir = fs.statSync(path).isDirectory();
if (isDir) {
return true;
}else{
fs.rmdirSync(path);
}
}
return '请输入合法地址!';
}
});
inquirer.prompt(prompt).then(answers => {
config = Object.assign(config, answers);
console.log(config);
var fileName = "index.zip";
var sourceFile = path.join(__dirname,"src\\pages",fileName);
var destPath = path.join(answers.path, "src\\modules", fileName);
var destDir = path.join(answers.path, "src\\modules");
var readStream = fs.createReadStream(sourceFile);
var writeStream = fs.createWriteStream(destPath);
readStream.pipe(writeStream);
writeStream.on('finish', () => {
var unzipExtract= unzip.Extract({ path: answers.path+"\\src\\modules"})
fs.createReadStream(destPath).pipe(unzipExtract);
fs.unlink(destPath, (err) => {
if (err) throw err;
fs.rename(destDir+"\\index", destDir+"\\"+answers.name, (err) => {
if (err) throw err;
fs.renameSync(destDir+"\\"+answers.name+"\\index.html", destDir+"\\"+answers.name+"\\"+answers.name+".html");
fs.renameSync(destDir+"\\"+answers.name+"\\index.js", destDir+"\\"+answers.name+"\\"+answers.name+".js");
console.log("添加完成");
});
});
});
});
})
program
.command('delpage')
.description('删除页面')
.action(function () {
var prompt = [];
var config = {};
prompt.push({
type: 'input',
name: 'name',
message: "请输入html页面(module)的名字",
default: function () {
return 'index';
},
validate: function(value) {
if (/^[0-9a-zA-Z-_]+$/.test(value)) {
return true;
}
return '页面名字只能包括数字字母和-_';
}
});
prompt.push({
type: 'input',
name: 'path',
message: "请输入框架根目录",
default: function () {
return process.cwd();
},
validate: function(path) {
if (path==process.cwd() ) {
return "请先输入地址,并且避免使用默认的 Framework 文件地址";
}
if (fs.existsSync(path)) {
return true;
}
return '请输入合法地址!';
}
});
inquirer.prompt(prompt).then(answers => {
config = Object.assign(config, answers);
console.log(config);
var destPath = path.join(answers.path, "src\\modules", answers.name);
try{
if(fs.statSync(destPath).isDirectory()){
rimraf(destPath, (err) =>{
if (err) throw err;
console.log("删除成功!")
});
}
}catch(err){
console.log("目录不存在!");
}
});
})
program
.command('design')
.description('设计页面')
.action(function () {
var exePath = path.resolve(__dirname, './design_frontend_start.bat');
var batPath = path.resolve(__dirname, './src/design_frontend');
exec('node ./build/dev-server.js', {cwd: batPath}, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
});
program.parse(process.argv)