Skip to content

Commit 13a26cd

Browse files
committed
add --busy-ignore and --target
1 parent aa9a2ca commit 13a26cd

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99

1010
> 在最初的开发过程中,本来的目的是想实现一个自动实时的文件同步工具. 将一些目录内容实时同步到远端的服务器和开发机上. 但考虑使用scp和rsync的条件下, 唯一需要的就是监测文件变化. 并产生一个通知. 于是便在设计上进行了终级简化. 最终变为现在这个小的命令行工具. 用于配合rsync,scp等标准命令行工具使用.
1111
12+
## Change log
13+
14+
* Version 0.0.1 ~ 0.0.6
15+
16+
fix bug
17+
18+
* Version 0.0.7
19+
20+
添加 --target 参数用于明确指定需要监视的目录名.
21+
22+
添加 --busy-ignore 参数用于解决一些文件变动间隔时间过短而处理时间较长而造成的处理堆叠问题.
23+
24+
25+
----------
26+
1227

1328
## Install
1429

@@ -19,12 +34,16 @@
1934
1. fsfb help - 显示帮助信息
2035

2136

22-
2. fsfb dirname [ --exec="commend Tpl" | --exec-tpl-file="file path" [ --charset=utf-8 ]] [ --enable-log ] [ --version | version ] [ --help | help ]
37+
2. fsfb "dirname" | --target="dirname" [ --exec="commend Tpl" | --exec-tpl-file="file path" [ --charset=utf-8 ]] [ --enable-log ] [ --version | version ] [ --help | help ]
2338

2439
Params :
2540

26-
dirname - 将进行监视的目录名
27-
41+
dirname - 将进行监视的目录名,当第一个参数未指定参数名时, 认为是target,如果后面指定了--target将会被覆盖.
42+
43+
--target - 将进行监视的目录名. (>= v_0.0.7)
44+
45+
--busy-ignore - 当一次执行未完成时,忽略后续的执行请求. (>= v_0.0.7)
46+
2847
--enable-log 显示一些调式信息.大部份情况下没用.
2948
3049
--exec 指定命令行模版, 当文件系统产生变化时, 将替换变量值后产生的一个完整的命令行指令并直接执行.
@@ -35,6 +54,10 @@
3554
3655
--version 显示版本信息
3756

57+
58+
> ***Tips : 关于--busy-ignore , 在处理一些耗时操作时, 添加这个参数将后续请求忽略有可能是必要的. 但是也可能造成一些必要的操作被忽略,所以使用时应当小心评估使用场景. 在未来会添加执行周期限制用于解决这个问题***
59+
60+
3861
### 支持的替换标记 : {xxx}
3962

4063

@@ -106,5 +129,5 @@ The last thing, run the scrip by fsfb.
106129
......
107130

108131

109-
132+
----------------
110133

index.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ args = args.slice(2);
2424
var dirname = null;
2525

2626
if(args.length >= 2){
27-
//取目标目录, 否则为单参数
28-
dirname = args.shift();
27+
// 第一个参数不存在'='时,认为第一个参数为目录名
28+
if(args[0].indexOf("=") == -1){
29+
//取目标目录, 否则为单参数
30+
dirname = args.shift();
31+
}
2932
}
3033

3134
var enableLog = false;
3235
var charset = 'utf-8';
3336
var execTpl = '';
3437
var execTplFile = '';
38+
var busyIgnore = false;
39+
40+
// 是否正在执行中
41+
var working = false;
3542

3643
var shellTpl = "echo {type}:{fullname}";
3744

@@ -48,6 +55,12 @@ while(paramItem = args.pop()){
4855
key = key.toLowerCase();
4956

5057
switch(key){
58+
case '--target':
59+
dirname = value || false;
60+
break;
61+
case '--busy-ignore':
62+
busyIgnore = true;
63+
break;
5164
case '--enable-log':
5265
// 启用log
5366
enableLog = true;
@@ -111,10 +124,12 @@ if(execTplFile){
111124
*/
112125
function help () {
113126

114-
var info = '\nUsage : fsfb dirname [ --exec="commend Tpl" | --exec-tpl-file="file path" [ --charset=utf-8 ]] [ --enable-log ] [ --version | version ] [ --help | help ]';
127+
var info = '\nUsage : fsfb dirname|--target="dirname" [ --exec="commend Tpl" | --exec-tpl-file="file path" [ --charset=utf-8 ]] [ --enable-log ] [ --version | version ] [ --help | help ]';
115128
info += '\n\nParams :';
116129
info += '\n\tdirname - 将进行监视的目录名';
117130
info += '\n\t--enable-log - 显示一些调式信息.大部份情况下没用.';
131+
info += '\n\t--target - 将进行监视的目录名';
132+
info += '\n\t--busy-ignore - 当一次执行未完成时,忽略后续的执行请求.';
118133
info += '\n\t--exec - 指定命令行模版, 当文件系统产生变化时, 将替换变量值后产生的一个完整的命令行指令并直接执行.';
119134
info += '\n\t--exec-tpl-file - 从文件读取命令行模版, *考虑性能问题, 在启动监视后,不再读取tpl-file文件变化.';
120135
info += '\n\t--charset - 指定tpl-file的文件编码,默认为utf-8';
@@ -164,6 +179,13 @@ function start(tplContent){
164179

165180
var feedback = function (type, fname, dirname) {
166181

182+
if(busyIgnore === true && working !== false){
183+
enableLog && console.log("ignore execute at : %s, the last execution at: $s;",Date.now(), working);
184+
return false;
185+
}
186+
187+
working = Date.now();
188+
167189
var callback = tplContent.replace(/\{(.*?)\}/g, function (match, p1, index) {
168190

169191
//type, fname, dirname, fulldir, fullname
@@ -185,12 +207,18 @@ function start(tplContent){
185207
enableLog && console.log("will to exec : %s ",callback);
186208

187209
exec(callback, function (err, out, errout) {
188-
if (err) {
189-
console.error(err.stack);
210+
try{
211+
if (err) {
212+
console.error(err.stack);
213+
}
214+
215+
out && process.stdout.write(out);
216+
errout && process.stderr.write(errout);
217+
}finally{
218+
// 只有当--busy-ignore打开时,才有可能统计执行时间, 否则working有可能被反复覆盖.
219+
enableLog && busyIgnore && console.log("cost time : %s ms. ", Date.now() - working);
220+
working = false;
190221
}
191-
192-
out && process.stdout.write(out);
193-
errout && process.stderr.write(errout);
194222
});
195223
};
196224

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fsfb",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "file system feedback : watch files change and execute a command",
55
"main": "common/observer.js",
66
"bin": {

0 commit comments

Comments
 (0)