@@ -24,14 +24,21 @@ args = args.slice(2);
24
24
var dirname = null ;
25
25
26
26
if ( args . length >= 2 ) {
27
- //取目标目录, 否则为单参数
28
- dirname = args . shift ( ) ;
27
+ // 第一个参数不存在'='时,认为第一个参数为目录名
28
+ if ( args [ 0 ] . indexOf ( "=" ) == - 1 ) {
29
+ //取目标目录, 否则为单参数
30
+ dirname = args . shift ( ) ;
31
+ }
29
32
}
30
33
31
34
var enableLog = false ;
32
35
var charset = 'utf-8' ;
33
36
var execTpl = '' ;
34
37
var execTplFile = '' ;
38
+ var busyIgnore = false ;
39
+
40
+ // 是否正在执行中
41
+ var working = false ;
35
42
36
43
var shellTpl = "echo {type}:{fullname}" ;
37
44
@@ -48,6 +55,12 @@ while(paramItem = args.pop()){
48
55
key = key . toLowerCase ( ) ;
49
56
50
57
switch ( key ) {
58
+ case '--target' :
59
+ dirname = value || false ;
60
+ break ;
61
+ case '--busy-ignore' :
62
+ busyIgnore = true ;
63
+ break ;
51
64
case '--enable-log' :
52
65
// 启用log
53
66
enableLog = true ;
@@ -111,10 +124,12 @@ if(execTplFile){
111
124
*/
112
125
function help ( ) {
113
126
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 ]' ;
115
128
info += '\n\nParams :' ;
116
129
info += '\n\tdirname - 将进行监视的目录名' ;
117
130
info += '\n\t--enable-log - 显示一些调式信息.大部份情况下没用.' ;
131
+ info += '\n\t--target - 将进行监视的目录名' ;
132
+ info += '\n\t--busy-ignore - 当一次执行未完成时,忽略后续的执行请求.' ;
118
133
info += '\n\t--exec - 指定命令行模版, 当文件系统产生变化时, 将替换变量值后产生的一个完整的命令行指令并直接执行.' ;
119
134
info += '\n\t--exec-tpl-file - 从文件读取命令行模版, *考虑性能问题, 在启动监视后,不再读取tpl-file文件变化.' ;
120
135
info += '\n\t--charset - 指定tpl-file的文件编码,默认为utf-8' ;
@@ -164,6 +179,13 @@ function start(tplContent){
164
179
165
180
var feedback = function ( type , fname , dirname ) {
166
181
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
+
167
189
var callback = tplContent . replace ( / \{ ( .* ?) \} / g, function ( match , p1 , index ) {
168
190
169
191
//type, fname, dirname, fulldir, fullname
@@ -185,12 +207,18 @@ function start(tplContent){
185
207
enableLog && console . log ( "will to exec : %s " , callback ) ;
186
208
187
209
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 ;
190
221
}
191
-
192
- out && process . stdout . write ( out ) ;
193
- errout && process . stderr . write ( errout ) ;
194
222
} ) ;
195
223
} ;
196
224
0 commit comments