-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiz.js
167 lines (138 loc) · 4.13 KB
/
fiz.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
#!/usr/bin/env node
// vi fiz/bin/fiz.js
var Liftoff = require("liftoff");
var argv = require("minimist")(process.argv.slice(2));
var path = require("path");
var fs = require("fs");
var cli = new Liftoff({
name: "fiz",
processTitle: "fiz",
moduleName: "fiz",
configName: "fis-conf",
//only js supported
extensions: {
".js": null
}
});
cli.launch({
cwd: argv.r || argv.root,
configPath: argv.f || argv.file
}, function(env){
//捕获命令行
var command = argv["_"];
switch(command[0]){
// fiz get
case "get":
//提供的服务
var WEB_GIT_NAME = "fiz-web-template";
var targetHash = {
"web": {
name: WEB_GIT_NAME,
url: "https://github.com/luozt/"+WEB_GIT_NAME+".git"
}
};
//检测是否存在该服务
var cmd1 = command[1];
var targetObj = targetHash[cmd1];
if(!targetObj){
console.log("Not such \"" + cmd1 + "\" get-target!");
return false;
}
var noDir = !!argv["n"];
var folder = targetObj.name;
var childProcess = require("child_process");
var exec = childProcess.exec;
console.log("start get [ " + cmd1 + " ]");
var child = exec("git clone " + targetObj.url, function(err, stdout, stderr){
if(err){
console.log("git clone err: " + err);
return;
}
console.log("get [ " + cmd1 + " ] success");
var child2 = exec("rm -rf " + folder + "/.git", function(err, stdout){
if (err){
console.log("rm "+ folder +"/.git error");
return;
}
if (noDir){
var child3 = exec("cp -rf " + folder + "/* ./", function(err, stdout){
if(err){
console.log("cp " + folder + " folder error");
return;
}
var child4 = exec("rm -rf " + folder, function(err, stdout){
if (err){
console.log("rm "+ folder +" folder error")
return;
}
});
});
}
});
});
break;
// 移动文件夹,不支持文件
case "mvDirContent":
var childProcess, exec, dest, folder, child, cwd;
cwd = process.cwd();
folder = argv["src"];
dest = argv["to"];
if(!dest || "string" != typeof dest){
console.log("dest: "+dest+", operate destination required!");
return false;
}
if(!folder || "string" !== typeof folder){
folder = cwd;
}
childProcess = require("child_process");
exec = childProcess.exec;
if(!fs.existsSync(folder)){
console.log("source: "+folder+", not such a source Foler!");
return;
}
if("/" === folder.slice(-1)){
folder = folder.slice(0,-1);
}
console.log("source folder: "+folder);
console.log("destination folder: "+dest);
child = exec("mv -f "+folder+"/** "+dest, function(err, stdout){
var child2;
if(err){
console.log(err);
console.log("mv "+folder+" folder content error when execing: "+"mv -f "+folder+"/** "+dest);
return;
}
child2 = exec("rm -rf "+folder, function(err, stdout){
var child3, clearFolder;
if(err){
console.log(err);
console.log("rm "+folder+" folder error");
return;
}
// 检测是否还有要删除的文件夹
clearFolder = argv["clear"];
if(clearFolder){
child3 = exec("rm -rf "+clearFolder, function(err, stdout){
if(err){
console.log(err);
console.log("rm "+clearFolder+" folder error");
return;
}
});
}
});
});
break;
// 引用fis
default:
var fis;
if(!env.modulePath){
fis = require("./");
}else{
fis = require(env.modulePath);
}
fis.set("system.localNPMFolder", path.join(env.cwd, "node_modules/fiz"));
// fis.set('system.globalNPMFolder', path.dirname(__dirname));
fis.cli.run(argv, env);
}
});