-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·91 lines (72 loc) · 2.26 KB
/
cli.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
#!/usr/bin/env node
const dir = process.cwd(), // System under test
validateFile = require(__dirname + '/validate_file'),
fs = require('fs'),
log = require(__dirname + '/log'),
lockedFiles = {};
require(__dirname + '/validate/calls').warmup();
validateDir(dir);
function validateDir (dir) {
log.out("-- watching", dir);
// Watch changes in this directory
fs.watch(
dir,
{encoding: 'buffer'},
validateFilesOf(dir)
);
// fs.watch does not support 'recursive:true' option on Linux
// --> manually watch all subdirectories
fs.readdir(dir, (err, list) => {
if (err) return;
list.forEach( (name) => {
if (isDirIgnored(name))
return;
name = dir + '/' + name;
fs.stat(name, (err, stat) => {
if (stat && stat.isDirectory())
validateDir(name);
});
});
});
};
function validateFilesOf (dir) {
return (eventType, filename) => {
// Do not track rename/deletion
if (eventType === 'rename')
return;
if (isFileIgnored(filename))
return;
// Event always triggered 2x (change+rename, even if logged as 2x change), process it 1x
if (filename in lockedFiles)
return;
lockedFiles[filename] = true;
//process.stdout.write(clc.reset);
log.hLine();
log.fileChange(filename+' '+eventType);
setTimeout(
() => validateFile(
dir+'/'+filename,
() => delete lockedFiles[filename]
),
50 // Give some time for fs to perform file 'rename', or it will open inexistant file
);
}
};
function isFileIgnored (filename) {
// Do not track temp files (e.g. emacs buffers)
if (/[~#]/.exec(filename))
return true;
// Track javascript files only
if (! (/.*.js$/.exec(filename)) )
return true;
return false;
};
function isDirIgnored (dirname) {
// Do not track hidden directories, like .git/
if (/^\./.exec(dirname))
return true;
// Do not track modules that are not part of the project
if (dirname === 'node_modules')
return true;
return false;
};