-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·92 lines (83 loc) · 2.55 KB
/
index.js
File metadata and controls
executable file
·92 lines (83 loc) · 2.55 KB
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
#!/usr/bin/env node
'use strict';
var yargs = require('yargs'); // Parses arguments and displays help message
var gaze = require('gaze'); // Watches for file changes
var execshell = require('exec-sh'); // Runs commands just like npm scripts
var prettyHrtime = require('pretty-hrtime'); // Format elapsed times nicely
// Define our command line arguments and help stuff
yargs
.usage('Usage: gaze <command> <pattern> ...\n\nIf present, the string $path in <command> will be replaced by the full path to the file that changed.')
.example('gaze "jshint $path" "lib/**/*.js"', 'Runs jshint when a js file in the lib folder changes')
.example('gaze "jshint $path" "**/*.js" "!node_modules/**/*"', 'Runs jshint when a js file anywhere except in node_modules changes')
.option('version', {
describe: 'Show version number',
type: 'boolean'
})
.option('help', {
describe: 'Print this help message',
type: 'boolean'
})
.option('silent', {
describe: 'Do not print messages',
type: 'boolean'
})
.option('ignore-rename', {
describe: 'Ignore when a file is renamed',
type: 'boolean'
});
var argv = yargs.argv;
// If requested, display help or version message then quit
if (argv.help) {
console.log(yargs.help());
return;
}
if (argv.version) {
console.log('v' + require('./package').version);
return;
}
// Ensure we have the proper arguments
if (argv._.length < 2) {
console.log('You must provide a single command and at least one pattern');
return;
}
var command = argv._[0];
var pattern = argv._.slice(1, argv._.length);
// Start the file watcher on the pattern
gaze(pattern, function(err, watcher) {
if (err) {
throw err;
}
if (!argv.silent) {
var fileCount = 0;
Object.keys(watcher._watched).forEach(function(watched) {
fileCount = fileCount + watcher._watched[watched].length;
});
console.log('Watching ' + fileCount + ' files/directories, pattern: ', pattern);
if (fileCount > 10000) {
console.log('This is a lot of files, for better performance you should reduce it.');
}
}
watcher.on('changed', function(filepath) {
run(filepath);
});
watcher.on('added', function(filepath) {
run(filepath);
});
watcher.on('renamed', function(newPath) {
if (!argv['ignore-rename']) {
run(newPath);
}
});
});
// Function to run when something changes
function run(filepath) {
var startTime = process.hrtime();
var uniqueCommand = command.replace('$path', filepath);
if (!argv.silent) {
console.log('>', uniqueCommand);
}
execshell(uniqueCommand);
if (!argv.silent) {
console.log('Finished in', prettyHrtime(process.hrtime(startTime)));
}
}