-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
248 lines (201 loc) · 7.03 KB
/
index.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* 命令行实现
*
* @file index.js
* @author [email protected]
*/
var path =require('path');
var fs =require('fs');
var childPorcess = require('child_process');
var exec = childPorcess.exec;
//
var Observer = require('./lib/observer.js');
var Helper = require('./lib/shell_argv_helper.js');
var joinPath = Observer.joinPath;
var args = process.argv;
var currentDirname = process.cwd();
var packageInfo = require("./package.json");
var dirname = null;
var enableLog = false;
var charset = 'utf-8';
var execTpl = '';
var execTplFile = '';
var busyIgnore = false;
// 是否正在执行中
var working = false;
var shellTpl = "echo {type}:{fullname}";
// split command parameters
Helper.init();
var params = Helper.getMap();
var key,value;
for(key in params.options){
key = key.toLowerCase();
value = params.options[key];
switch(key){
case 't':
case 'target':
dirname = value || false;
break;
case 'ignore':
case 'config':
case 'busy-ignore':
busyIgnore = true;
break;
case 'enable-log':
// 启用log
enableLog = true;
break;
case 'charset':
charset = value || "utf-8";
break;
case 'r':
case 'exec':
// 直接替换执行的字符串
execTpl = value;
break;
case 'exec-tpl-file':
execTplFile = value;
break;
case 'version':
version();
return process.exit();
case 'help':
default:
help();
return process.exit();
}
}
if (!dirname) {
if (params.contents) {
dirname = params.contents;
} else {
help();
console.log('missing "dirname".\n');
process.exit();
}
}
if(execTplFile){
var opt = {
encoding:charset,
};
fs.readFile(execTplFile, opt, function (err, content) {
if(err){
console.error(err);
return;
}
enableLog && console.log("read tplContent as : %s ",callback);
start(content);
});
}else if(execTpl){
start(execTpl);
}else{
help();
console.log('required one of [-r ,--exec , --exec-tpl-file]!');
}
/**
* show help information
*/
function help () {
var info = '\nUsage : fsfb [dirname|--target="dirname"] [ --exec="commend Tpl" | --exec-tpl-file="file path" [ --charset=utf-8 ]] [ --enable-log ] [ --version | version ] [ --help | help ]';
info += '\n\nParams :';
info += '\n\tdirname - 将进行监视的目录名';
info += '\n\t--enable-log - 显示一些调式信息.大部份情况下没用.';
info += '\n\t--target - 将进行监视的目录名';
info += '\n\t--busy-ignore - 当一次执行未完成时,忽略后续的执行请求.';
info += '\n\t--exec - 指定命令行模版, 当文件系统产生变化时, 将替换变量值后产生的一个完整的命令行指令并直接执行.';
info += '\n\t--exec-tpl-file - 从文件读取命令行模版, *考虑性能问题, 在启动监视后,不再读取tpl-file文件变化.';
info += '\n\t--charset - 指定tpl-file的文件编码,默认为utf-8';
info += '\n\t--version - 显示版本信息';
info += '\n\nsupport args : {xxx}';
info += '\n\t{type} - change type of the file system,';
info += '\n\t\tpossible value: "modify", "newfile", "rmfile", "mkdir", "rmdir"';
info += '\n\t{fname} - filename,';
info += '\n\t{dirname} - relative path,';
info += '\n\t{fulldir} - full path(AP),';
info += '\n\t{fullname} - full path and file name.\n';
info += '\nmore info \n\t ' + packageInfo.homepage;
info += '\n';
console.log(info);
};
/**
* show version info
*/
function version () {
var versionInfo = "%s [%s] version : %s \n -- %s\n" +
"------------------------------------------\n" +
"Author : %s,\nWebSite : %s\n";
var author = packageInfo.author;
if(typeof(author) == 'object'){
author = author.name;
}
console.log(versionInfo, packageInfo.name, packageInfo.license, packageInfo.version,
packageInfo.description, author, packageInfo.homepage);
}
/**
* start watching
*/
function start(tplContent){
console.log('\n Welecom to use the fs_feedback. \n ------------------------------- \n');
var target = new Observer(dirname);
target.disableLog(enableLog);
var feedback = function (type, fname, dirname) {
if(busyIgnore === true && working !== false){
enableLog && console.log("ignore execute at : %s, the last execution at: $s;",Date.now(), working);
return false;
}
working = Date.now();
var callback = tplContent.replace(/\{(.*?)\}/g, function (match, p1, index) {
//type, fname, dirname, fulldir, fullname
switch (p1) {
case "type":
return type;
case "fname":
return fname;
case "dirname":
return dirname;
case "fulldir":
return path.resolve(dirname);
case "fullname":
return joinPath(path.resolve(dirname), fname);
}
});
enableLog && console.log("will to exec : %s ",callback);
exec(callback, function (err, out, errout) {
try{
if (err) {
console.error(err.stack);
}
out && process.stdout.write(out);
errout && process.stderr.write(errout);
}finally{
// 只有当--busy-ignore打开时,才有可能统计执行时间, 否则working有可能被反复覆盖.
enableLog && busyIgnore && console.log("cost time : %s ms. ", Date.now() - working);
working = false;
}
});
};
target.on("modify", function (fname, fullname) {
enableLog && console.log('watch : modify %s on %s', fname, fullname);
feedback('modify', fname, fullname);
});
target.on("mkdir", function (fname, fullname) {
enableLog && console.log('watch : mkdir %s on %s', fname, fullname);
feedback('mkdir', fname, fullname);
});
target.on("rmdir", function (fname, fullname) {
enableLog && console.log('watch : rmdir %s on %s', fname, fullname);
feedback('rmdir', fname, fullname);
});
target.on("newfile", function (fname, fullname) {
enableLog && console.log('watch : newfile %s on %s', fname, fullname);
feedback('newfile', fname, fullname);
});
target.on("rmfile", function (fname, fullname) {
enableLog && console.log('watch : rmfile %s on %s', fname, fullname);
feedback('rmfile', fname, fullname);
});
target.on("error", function (err) {
console.error('error : %s', err.stack);
// process.exit()
});
}