-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (132 loc) · 4.42 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
var spawn = require('child_process').spawn;
var gaze = require('gaze');
// The process where the commands are being run
var cmdProcess;
var queue = [];
var running = false;
// Default options
var options = {
verbose: false,
interrupt: true,
useQueue: false,
pattern: ['**/*', '!**/node_modules/**']
};
module.exports = function (argv) {
// Get rid of 'node' and 'bin' arguments
argv = argv.slice(2);
if (argv.length === 0) {
console.log('Usage: eye <command>');
console.log('');
console.log('Options:');
console.log('');
console.log(' --*glob=<pattern> Specify which files you want to watch');
console.log('');
console.log(' --*queue Have commands form a queue and be run one at a time until the queue is empty');
console.log('');
console.log(' --*continue If a file event is triggered while a command is being run, don\'t interrupt it');
console.log('');
console.log(' --*verbose Log files watched, text of the comand being run and more');
process.exit(1);
}
// Add all of the arguments to the commandArguments array unelss they are eye
// options
var commandArguments = [];
for (var i = 0; i <argv.length; i++) {
var argument = argv[i];
if (argument.indexOf('--*glob=') !== -1) {
// Get everything after '=' and replace '%' with '!' because Unix
// exectutes everything after '!' so we can't use it. And finally split
// at comma to get an array of globs
options.pattern = argument.split('=')[1].split('%').join('!').split(',');
} else if (argument.indexOf('--*verbose') !== -1) {
options.verbose = true;
} else if (argument.indexOf('--*queue') !== -1) {
options.useQueue = true;
} else if (argument.indexOf('--*continue') !== -1) {
options.interrupt = false;
} else {
commandArguments.push(argument);
}
}
var commands = processArguments(commandArguments, []);
if (options.verbose) {
console.log('pattern is:');
console.log(options.pattern);
}
// Watch file selected by glob for changes
gaze(options.pattern, function(err) {
if (err) throw err;
// Log startup message
console.log('eye is watching...');
// Log watched files
if (options.verbose) {
console.log('watched files:');
console.log(this.watched());
}
this.on('error', function(err) {
throw err;
});
this.on('all', function(event, filepath) {
// Log file event
if (options.verbose) console.log(filepath + ' was ' + event);
if (options.useQueue) {
// Add commands to queue
queue = queue.concat(commands);
} else if (! queue.length) {
queue = [].concat(commands);
}
// If queue isn't being run, run it
if (! running) {
runCommands(commands);
// If commands are in the process of running
} else if (options.interrupt && cmdProcess) {
// The closing of the command process will remove the first queue item
// so we add and empty queue item so it won't remove the next command
if (! options.queue) queue.unshift('');
cmdProcess.kill();
}
});
});
};
function runCommands (commands) {
running = true;
// Run the first command in the queue
var command = queue[0];
if (options.verbose) {
console.log('running: ' + command.cmd + ' ' + command.options.join(' '));
console.log('result:');
}
cmdProcess = spawn(command.cmd, command.options);
cmdProcess.stdout.on('data', function (data) {
process.stdout.write('' + data);
});
cmdProcess.stderr.on('data', function (data) {
process.stdout.write('' + data);
});
cmdProcess.on('close', function () {
// Remove executed command from queue
queue.shift();
// If there are more commands in the queue, recurse
if (queue.length > 0) runCommands(commands);
running = false;
});
}
function processArguments (argv, commands) {
// Create command, first argument is the command and the rest are options
var command = {
cmd: argv.splice(0, 1)[0],
options: []
};
// Add arguemnts as options. If argument is 'and', add current command to
// commands array and recurse to get next command
for (var i = 0; i < argv.length; i++) {
if (argv[i] !== 'and') {
command.options.push(argv[i]);
} else {
commands.push(command);
return processArguments(argv.slice(i + 1), commands);
}
}
commands.push(command);
return commands;
}