This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
forked from TrevorBurnham/Jitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jitter.js
370 lines (318 loc) · 11.5 KB
/
jitter.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Generated by CoffeeScript 1.6.2
/*
Jitter, a CoffeeScript compilation utility
The latest version and documentation, can be found at:
http://github.com/TrevorBurnham/Jitter
Copyright (c) 2010 Trevor Burnham
http://iterative.ly
Based on command.coffee by Jeremy Ashkenas
http://jashkenas.github.com/coffee-script/documentation/docs/command.html
Growl notification code contributed by Andrey Tarantsov
http://www.tarantsov.com/
SourceMaps support by Aria Minaei
https://twitter.com/ariaminaei
https://github.com/AriaMinaei
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
(function() {
var BANNER, CoffeeScript, baseSource, baseTarget, baseTest, color, compile, compileScript, compileScripts, die, e, exec, fs, isSubpath, isWatched, jsPath, notifyGrowl, optionParser, options, optparse, parseOptions, path, print, puts, q, readScript, rootCompile, runTests, target_ext, targetlib, testFiles, usage, watchScript, writeJS, writeSourceMap, _ref,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
fs = require('fs');
path = require('path');
optparse = require('./optparse');
color = require('./ansi-color').set;
if (path.basename(process.argv[1]) === 'witter') {
targetlib = 'coco';
target_ext = '.coco';
} else {
targetlib = 'coffee-script';
target_ext = '.coffee';
}
CoffeeScript = require(targetlib);
exec = require('child_process').exec;
_ref = (function() {
try {
return require('util');
} catch (_error) {
e = _error;
return require('sys');
}
})(), puts = _ref.puts, print = _ref.print;
q = require('sink').q;
BANNER = 'Jitter takes a directory of *.coffee files and recursively compiles\nthem to *.js files, preserving the original directory structure.\n\nJitter also watches for changes and automatically recompiles as\nneeded. It even detects new files, unlike the coffee utility.\n\nIf passed a test directory, it will run each test through node on\neach change.\n\nUsage:\n jitter coffee-path js-path [test-path]';
options = {};
baseSource = baseTarget = baseTest = '';
optionParser = null;
isWatched = {};
testFiles = [];
exports.run = function() {
options = parseOptions();
if (!baseTarget) {
return usage();
}
return compileScripts(options);
};
compileScripts = function(options) {
var dir, dirs, name;
dirs = {
Source: baseSource,
Target: baseTarget
};
if (baseTest) {
dirs.Test = baseTest;
}
for (name in dirs) {
dir = dirs[name];
q(fs.exists, dir, function(exists) {
if (!exists) {
return die("" + name + " directory '" + dir + "' does not exist.");
} else if (!fs.statSync(dir).isDirectory()) {
return die("" + name + " '" + dir + "' is a file; Jitter needs a directory.");
}
});
}
q(function() {
return rootCompile(options);
});
q(runTests);
return q(function() {
puts('Watching for changes and new files. Press Ctrl+C to stop.\n');
return setInterval(function() {
return rootCompile(options);
}, 500);
});
};
compile = function(source, target, options) {
var item, sourcePath, _i, _len, _ref1, _results;
_ref1 = fs.readdirSync(source);
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
item = _ref1[_i];
sourcePath = "" + source + "/" + item;
if (item[0] === '.') {
continue;
}
if (isWatched[sourcePath]) {
continue;
}
try {
if (path.extname(sourcePath) === target_ext) {
_results.push(readScript(sourcePath, target, options));
} else if (fs.statSync(sourcePath).isDirectory()) {
_results.push(compile(sourcePath, target, options));
} else {
_results.push(void 0);
}
} catch (_error) {
e = _error;
}
}
return _results;
};
rootCompile = function(options) {
compile(baseSource, baseTarget, options);
if (baseTest) {
return compile(baseTest, baseTest, options);
}
};
readScript = function(source, target, options) {
compileScript(source, target, options);
return watchScript(source, target, options);
};
watchScript = function(source, target, options) {
if (isWatched[source]) {
return;
}
isWatched[source] = true;
return fs.watchFile(source, {
persistent: true,
interval: 250
}, function(curr, prev) {
if (curr.mtime.getTime() === prev.mtime.getTime()) {
return;
}
compileScript(source, target, options);
return q(runTests);
});
};
compileScript = function(source, target, options) {
var code, coffeePath, currentJS, err, js, jsFilename, mapFilename, mapPath, msg, numOfParentDirs, pathToRoot, slashedTargetPath, sourceMap, targetPath;
targetPath = jsPath(source, target);
try {
code = fs.readFileSync(source).toString();
try {
currentJS = fs.readFileSync(targetPath).toString();
} catch (_error) {}
if (!(options != null ? options.map : void 0)) {
if (options != null) {
options.map = false;
}
}
if (options.map) {
slashedTargetPath = targetPath.split('\\').join('/');
numOfParentDirs = slashedTargetPath.split('/').length - 1;
mapFilename = slashedTargetPath.replace('.js', '.map');
if (mapFilename.lastIndexOf('/') !== -1) {
mapFilename = mapFilename.substr(mapFilename.lastIndexOf('/') + 1);
}
jsFilename = mapFilename.replace('.map', '.js');
pathToRoot = (function() {
var i, _i, _path;
_path = [];
for (i = _i = 1; 1 <= numOfParentDirs ? _i <= numOfParentDirs : _i >= numOfParentDirs; i = 1 <= numOfParentDirs ? ++_i : --_i) {
_path.push('..');
}
return _path.join('\\');
})();
mapPath = slashedTargetPath.substr(0, slashedTargetPath.length - 2) + 'map';
coffeePath = source.split('\\').join('/').split('/').join('\\');
}
js = CoffeeScript.compile(code, {
source: source,
bare: options != null ? options.bare : void 0,
sourceMap: options.map,
generatedFile: jsFilename,
sourceRoot: pathToRoot,
sourceFiles: [coffeePath]
});
if (options.map) {
sourceMap = js.v3SourceMap;
js = js.js + '\n/*\n' + ("//@ sourceMappingURL=" + mapFilename + "\n") + '*/\n';
}
if (js === currentJS) {
return;
}
writeJS(js, targetPath);
if (sourceMap) {
writeSourceMap(sourceMap, mapPath);
}
if (currentJS != null) {
return puts('Recompiled ' + source + '\n');
} else {
return puts('Compiled ' + source + '\n');
}
} catch (_error) {
err = _error;
msg = (function() {
var name, ret;
name = path.basename(source);
ret = 'Error: ';
ret += source.substr(0, source.length - name.length);
ret += color(name + ':' + (err.location.first_line + 1), 'bold');
ret = color(ret, 'red');
ret += '\n\n ' + err.message;
return ret;
})();
puts(msg);
if (!(options != null ? options.beep : void 0)) {
console.log("\007");
}
return notifyGrowl(source, err.message);
}
};
jsPath = function(source, target) {
var base, dir, filename;
base = target === baseTest ? baseTest : baseSource;
filename = path.basename(source, path.extname(source)) + '.js';
dir = target + path.dirname(source).substring(base.length);
return path.join(dir, filename);
};
writeJS = function(js, targetPath) {
return q(exec, "mkdir -p " + (path.dirname(targetPath)), function() {
fs.writeFileSync(targetPath, js);
if (baseTest && isSubpath(baseTest, targetPath) && (__indexOf.call(testFiles, targetPath) < 0)) {
return testFiles.push(targetPath);
}
});
};
writeSourceMap = function(content, targetPath) {
return q(exec, "mkdir -p " + (path.dirname(targetPath)), function() {
return fs.writeFileSync(targetPath, content);
});
};
notifyGrowl = function(source, errMessage) {
var args, basename, m, message;
basename = source.replace(/^.*[\/\\]/, '');
if (m = errMessage.match(/Parse error on line (\d+)/)) {
message = "Parse error in " + basename + "\non line " + m[1] + ".";
} else {
message = "Error in " + basename + ".";
}
args = ['growlnotify', '-n', 'CoffeeScript', '-p', '2', '-t', "\"Compilation failed\"", '-m', "\"" + message + "\""];
return exec(args.join(' '));
};
runTests = function() {
var output, test, _i, _len, _results;
_results = [];
for (_i = 0, _len = testFiles.length; _i < _len; _i++) {
test = testFiles[_i];
output = "Running " + test;
_results.push(exec("node " + test, function(error, stdout, stderr) {
if (stderr) {
notifyGrowl(test, stderr);
}
if (stderr) {
if (!(options != null ? options.silent : void 0)) {
console.log("\007");
}
output += color(' ' + 'FAILED ->', 'red');
output += '\n' + stdout + stderr + '\n';
} else {
output += color(' ' + 'PASSED\n', 'green');
}
return puts(output);
}));
}
return _results;
};
parseOptions = function() {
var arg, _ref1;
optionParser = new optparse.OptionParser([['-b', '--bare', 'compile without the top-level function wrapper'], ['-m', '--map', 'compile with source maps'], ['-s', '--silent', 'don\'t make beep sounds on errors']], BANNER);
options = optionParser.parse(process.argv);
_ref1 = (function() {
var _i, _results;
_results = [];
for (arg = _i = 2; _i <= 4; arg = ++_i) {
_results.push(options["arguments"][arg] || '');
}
return _results;
})(), baseSource = _ref1[0], baseTarget = _ref1[1], baseTest = _ref1[2];
if (/\/$/.test(baseSource)) {
baseSource = baseSource.substr(0, baseSource.length - 1);
}
if (/\/$/.test(baseTarget)) {
baseTarget = baseTarget.substr(0, baseTarget.length - 1);
}
return options;
};
usage = function() {
puts(optionParser.help());
return process.exit(0);
};
die = function(message) {
puts(message);
return process.exit(1);
};
isSubpath = function(parent, sub) {
parent = fs.realpathSync(parent);
sub = fs.realpathSync(sub);
return sub.indexOf(parent) === 0;
};
}).call(this);