]> git.r.bdr.sh - rbdr/dotfiles/blob
357653b74341365731a11e543ac5824abdd55b60
[rbdr/dotfiles] /
1 // Generated by CoffeeScript 1.8.0
2 (function() {
3 var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, findDirectoryIndex, forkNode, fs, helpers, hidden, joinTimeout, mkdirp, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, removeSourceDir, silentUnlink, sourceCode, sources, spawn, timeLog, usage, useWinPathSep, version, wait, watch, watchDir, watchedDirs, writeJs, _ref,
4 __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; };
5
6 fs = require('fs');
7
8 path = require('path');
9
10 helpers = require('./helpers');
11
12 optparse = require('./optparse');
13
14 CoffeeScript = require('./coffee-script');
15
16 mkdirp = require('mkdirp');
17
18 _ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
19
20 EventEmitter = require('events').EventEmitter;
21
22 useWinPathSep = path.sep === '\\';
23
24 helpers.extend(CoffeeScript, new EventEmitter);
25
26 printLine = function(line) {
27 return process.stdout.write(line + '\n');
28 };
29
30 printWarn = function(line) {
31 return process.stderr.write(line + '\n');
32 };
33
34 hidden = function(file) {
35 return /^\.|~$/.test(file);
36 };
37
38 BANNER = 'Usage: coffee [options] path/to/script.coffee -- [args]\n\nIf called without options, `coffee` will run your script.';
39
40 SWITCHES = [['-b', '--bare', 'compile without a top-level function wrapper'], ['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-e', '--eval', 'pass a string from the command line as input'], ['-h', '--help', 'display this help message'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling'], ['-m', '--map', 'generate source map and save as .js.map files'], ['-n', '--nodes', 'print out the parse tree that the parser produces'], ['--nodejs [ARGS]', 'pass options directly to the "node" binary'], ['--no-header', 'suppress the "Generated by" header'], ['-o', '--output [DIR]', 'set the output directory for compiled JavaScript'], ['-p', '--print', 'print out the compiled JavaScript'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-l', '--literate', 'treat stdio as literate style coffee-script'], ['-t', '--tokens', 'print out the tokens that the lexer/rewriter produce'], ['-v', '--version', 'display the version number'], ['-w', '--watch', 'watch scripts for changes and rerun commands']];
41
42 opts = {};
43
44 sources = [];
45
46 sourceCode = [];
47
48 notSources = {};
49
50 watchedDirs = {};
51
52 optionParser = null;
53
54 exports.run = function() {
55 var literals, replCliOpts, source, _i, _len, _ref1, _results;
56 parseOptions();
57 replCliOpts = {
58 useGlobal: true
59 };
60 if (opts.nodejs) {
61 return forkNode();
62 }
63 if (opts.help) {
64 return usage();
65 }
66 if (opts.version) {
67 return version();
68 }
69 if (opts.interactive) {
70 return require('./repl').start(replCliOpts);
71 }
72 if (opts.stdio) {
73 return compileStdio();
74 }
75 if (opts["eval"]) {
76 return compileScript(null, opts["arguments"][0]);
77 }
78 if (!opts["arguments"].length) {
79 return require('./repl').start(replCliOpts);
80 }
81 literals = opts.run ? opts["arguments"].splice(1) : [];
82 process.argv = process.argv.slice(0, 2).concat(literals);
83 process.argv[0] = 'coffee';
84 if (opts.output) {
85 opts.output = path.resolve(opts.output);
86 }
87 if (opts.join) {
88 opts.join = path.resolve(opts.join);
89 console.error('\nThe --join option is deprecated and will be removed in a future version.\n\nIf for some reason it\'s necessary to share local variables between files,\nreplace...\n\n $ coffee --compile --join bundle.js -- a.coffee b.coffee c.coffee\n\nwith...\n\n $ cat a.coffee b.coffee c.coffee | coffee --compile --stdio > bundle.js\n');
90 }
91 _ref1 = opts["arguments"];
92 _results = [];
93 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
94 source = _ref1[_i];
95 source = path.resolve(source);
96 _results.push(compilePath(source, true, source));
97 }
98 return _results;
99 };
100
101 compilePath = function(source, topLevel, base) {
102 var code, err, file, files, stats, _i, _len, _results;
103 if (__indexOf.call(sources, source) >= 0 || watchedDirs[source] || !topLevel && (notSources[source] || hidden(source))) {
104 return;
105 }
106 try {
107 stats = fs.statSync(source);
108 } catch (_error) {
109 err = _error;
110 if (err.code === 'ENOENT') {
111 console.error("File not found: " + source);
112 process.exit(1);
113 }
114 throw err;
115 }
116 if (stats.isDirectory()) {
117 if (path.basename(source) === 'node_modules') {
118 notSources[source] = true;
119 return;
120 }
121 if (opts.run) {
122 compilePath(findDirectoryIndex(source), topLevel, base);
123 return;
124 }
125 if (opts.watch) {
126 watchDir(source, base);
127 }
128 try {
129 files = fs.readdirSync(source);
130 } catch (_error) {
131 err = _error;
132 if (err.code === 'ENOENT') {
133 return;
134 } else {
135 throw err;
136 }
137 }
138 _results = [];
139 for (_i = 0, _len = files.length; _i < _len; _i++) {
140 file = files[_i];
141 _results.push(compilePath(path.join(source, file), false, base));
142 }
143 return _results;
144 } else if (topLevel || helpers.isCoffee(source)) {
145 sources.push(source);
146 sourceCode.push(null);
147 delete notSources[source];
148 if (opts.watch) {
149 watch(source, base);
150 }
151 try {
152 code = fs.readFileSync(source);
153 } catch (_error) {
154 err = _error;
155 if (err.code === 'ENOENT') {
156 return;
157 } else {
158 throw err;
159 }
160 }
161 return compileScript(source, code.toString(), base);
162 } else {
163 return notSources[source] = true;
164 }
165 };
166
167 findDirectoryIndex = function(source) {
168 var err, ext, index, _i, _len, _ref1;
169 _ref1 = CoffeeScript.FILE_EXTENSIONS;
170 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
171 ext = _ref1[_i];
172 index = path.join(source, "index" + ext);
173 try {
174 if ((fs.statSync(index)).isFile()) {
175 return index;
176 }
177 } catch (_error) {
178 err = _error;
179 if (err.code !== 'ENOENT') {
180 throw err;
181 }
182 }
183 }
184 console.error("Missing index.coffee or index.litcoffee in " + source);
185 return process.exit(1);
186 };
187
188 compileScript = function(file, input, base) {
189 var compiled, err, message, o, options, t, task;
190 if (base == null) {
191 base = null;
192 }
193 o = opts;
194 options = compileOptions(file, base);
195 try {
196 t = task = {
197 file: file,
198 input: input,
199 options: options
200 };
201 CoffeeScript.emit('compile', task);
202 if (o.tokens) {
203 return printTokens(CoffeeScript.tokens(t.input, t.options));
204 } else if (o.nodes) {
205 return printLine(CoffeeScript.nodes(t.input, t.options).toString().trim());
206 } else if (o.run) {
207 CoffeeScript.register();
208 return CoffeeScript.run(t.input, t.options);
209 } else if (o.join && t.file !== o.join) {
210 if (helpers.isLiterate(file)) {
211 t.input = helpers.invertLiterate(t.input);
212 }
213 sourceCode[sources.indexOf(t.file)] = t.input;
214 return compileJoin();
215 } else {
216 compiled = CoffeeScript.compile(t.input, t.options);
217 t.output = compiled;
218 if (o.map) {
219 t.output = compiled.js;
220 t.sourceMap = compiled.v3SourceMap;
221 }
222 CoffeeScript.emit('success', task);
223 if (o.print) {
224 return printLine(t.output.trim());
225 } else if (o.compile || o.map) {
226 return writeJs(base, t.file, t.output, options.jsPath, t.sourceMap);
227 }
228 }
229 } catch (_error) {
230 err = _error;
231 CoffeeScript.emit('failure', err, task);
232 if (CoffeeScript.listeners('failure').length) {
233 return;
234 }
235 message = err.stack || ("" + err);
236 if (o.watch) {
237 return printLine(message + '\x07');
238 } else {
239 printWarn(message);
240 return process.exit(1);
241 }
242 }
243 };
244
245 compileStdio = function() {
246 var code, stdin;
247 code = '';
248 stdin = process.openStdin();
249 stdin.on('data', function(buffer) {
250 if (buffer) {
251 return code += buffer.toString();
252 }
253 });
254 return stdin.on('end', function() {
255 return compileScript(null, code);
256 });
257 };
258
259 joinTimeout = null;
260
261 compileJoin = function() {
262 if (!opts.join) {
263 return;
264 }
265 if (!sourceCode.some(function(code) {
266 return code === null;
267 })) {
268 clearTimeout(joinTimeout);
269 return joinTimeout = wait(100, function() {
270 return compileScript(opts.join, sourceCode.join('\n'), opts.join);
271 });
272 }
273 };
274
275 watch = function(source, base) {
276 var compile, compileTimeout, err, prevStats, rewatch, startWatcher, watchErr, watcher;
277 watcher = null;
278 prevStats = null;
279 compileTimeout = null;
280 watchErr = function(err) {
281 if (err.code !== 'ENOENT') {
282 throw err;
283 }
284 if (__indexOf.call(sources, source) < 0) {
285 return;
286 }
287 try {
288 rewatch();
289 return compile();
290 } catch (_error) {
291 removeSource(source, base);
292 return compileJoin();
293 }
294 };
295 compile = function() {
296 clearTimeout(compileTimeout);
297 return compileTimeout = wait(25, function() {
298 return fs.stat(source, function(err, stats) {
299 if (err) {
300 return watchErr(err);
301 }
302 if (prevStats && stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
303 return rewatch();
304 }
305 prevStats = stats;
306 return fs.readFile(source, function(err, code) {
307 if (err) {
308 return watchErr(err);
309 }
310 compileScript(source, code.toString(), base);
311 return rewatch();
312 });
313 });
314 });
315 };
316 startWatcher = function() {
317 return watcher = fs.watch(source).on('change', compile).on('error', function(err) {
318 if (err.code !== 'EPERM') {
319 throw err;
320 }
321 return removeSource(source, base);
322 });
323 };
324 rewatch = function() {
325 if (watcher != null) {
326 watcher.close();
327 }
328 return startWatcher();
329 };
330 try {
331 return startWatcher();
332 } catch (_error) {
333 err = _error;
334 return watchErr(err);
335 }
336 };
337
338 watchDir = function(source, base) {
339 var err, readdirTimeout, startWatcher, stopWatcher, watcher;
340 watcher = null;
341 readdirTimeout = null;
342 startWatcher = function() {
343 return watcher = fs.watch(source).on('error', function(err) {
344 if (err.code !== 'EPERM') {
345 throw err;
346 }
347 return stopWatcher();
348 }).on('change', function() {
349 clearTimeout(readdirTimeout);
350 return readdirTimeout = wait(25, function() {
351 var err, file, files, _i, _len, _results;
352 try {
353 files = fs.readdirSync(source);
354 } catch (_error) {
355 err = _error;
356 if (err.code !== 'ENOENT') {
357 throw err;
358 }
359 return stopWatcher();
360 }
361 _results = [];
362 for (_i = 0, _len = files.length; _i < _len; _i++) {
363 file = files[_i];
364 _results.push(compilePath(path.join(source, file), false, base));
365 }
366 return _results;
367 });
368 });
369 };
370 stopWatcher = function() {
371 watcher.close();
372 return removeSourceDir(source, base);
373 };
374 watchedDirs[source] = true;
375 try {
376 return startWatcher();
377 } catch (_error) {
378 err = _error;
379 if (err.code !== 'ENOENT') {
380 throw err;
381 }
382 }
383 };
384
385 removeSourceDir = function(source, base) {
386 var file, sourcesChanged, _i, _len;
387 delete watchedDirs[source];
388 sourcesChanged = false;
389 for (_i = 0, _len = sources.length; _i < _len; _i++) {
390 file = sources[_i];
391 if (!(source === path.dirname(file))) {
392 continue;
393 }
394 removeSource(file, base);
395 sourcesChanged = true;
396 }
397 if (sourcesChanged) {
398 return compileJoin();
399 }
400 };
401
402 removeSource = function(source, base) {
403 var index;
404 index = sources.indexOf(source);
405 sources.splice(index, 1);
406 sourceCode.splice(index, 1);
407 if (!opts.join) {
408 silentUnlink(outputPath(source, base));
409 silentUnlink(outputPath(source, base, '.js.map'));
410 return timeLog("removed " + source);
411 }
412 };
413
414 silentUnlink = function(path) {
415 var err, _ref1;
416 try {
417 return fs.unlinkSync(path);
418 } catch (_error) {
419 err = _error;
420 if ((_ref1 = err.code) !== 'ENOENT' && _ref1 !== 'EPERM') {
421 throw err;
422 }
423 }
424 };
425
426 outputPath = function(source, base, extension) {
427 var basename, dir, srcDir;
428 if (extension == null) {
429 extension = ".js";
430 }
431 basename = helpers.baseFileName(source, true, useWinPathSep);
432 srcDir = path.dirname(source);
433 if (!opts.output) {
434 dir = srcDir;
435 } else if (source === base) {
436 dir = opts.output;
437 } else {
438 dir = path.join(opts.output, path.relative(base, srcDir));
439 }
440 return path.join(dir, basename + extension);
441 };
442
443 writeJs = function(base, sourcePath, js, jsPath, generatedSourceMap) {
444 var compile, jsDir, sourceMapPath;
445 if (generatedSourceMap == null) {
446 generatedSourceMap = null;
447 }
448 sourceMapPath = outputPath(sourcePath, base, ".js.map");
449 jsDir = path.dirname(jsPath);
450 compile = function() {
451 if (opts.compile) {
452 if (js.length <= 0) {
453 js = ' ';
454 }
455 if (generatedSourceMap) {
456 js = "" + js + "\n//# sourceMappingURL=" + (helpers.baseFileName(sourceMapPath, false, useWinPathSep)) + "\n";
457 }
458 fs.writeFile(jsPath, js, function(err) {
459 if (err) {
460 printLine(err.message);
461 return process.exit(1);
462 } else if (opts.compile && opts.watch) {
463 return timeLog("compiled " + sourcePath);
464 }
465 });
466 }
467 if (generatedSourceMap) {
468 return fs.writeFile(sourceMapPath, generatedSourceMap, function(err) {
469 if (err) {
470 printLine("Could not write source map: " + err.message);
471 return process.exit(1);
472 }
473 });
474 }
475 };
476 return fs.exists(jsDir, function(itExists) {
477 if (itExists) {
478 return compile();
479 } else {
480 return mkdirp(jsDir, compile);
481 }
482 });
483 };
484
485 wait = function(milliseconds, func) {
486 return setTimeout(func, milliseconds);
487 };
488
489 timeLog = function(message) {
490 return console.log("" + ((new Date).toLocaleTimeString()) + " - " + message);
491 };
492
493 printTokens = function(tokens) {
494 var strings, tag, token, value;
495 strings = (function() {
496 var _i, _len, _results;
497 _results = [];
498 for (_i = 0, _len = tokens.length; _i < _len; _i++) {
499 token = tokens[_i];
500 tag = token[0];
501 value = token[1].toString().replace(/\n/, '\\n');
502 _results.push("[" + tag + " " + value + "]");
503 }
504 return _results;
505 })();
506 return printLine(strings.join(' '));
507 };
508
509 parseOptions = function() {
510 var o;
511 optionParser = new optparse.OptionParser(SWITCHES, BANNER);
512 o = opts = optionParser.parse(process.argv.slice(2));
513 o.compile || (o.compile = !!o.output);
514 o.run = !(o.compile || o.print || o.map);
515 return o.print = !!(o.print || (o["eval"] || o.stdio && o.compile));
516 };
517
518 compileOptions = function(filename, base) {
519 var answer, cwd, jsDir, jsPath;
520 answer = {
521 filename: filename,
522 literate: opts.literate || helpers.isLiterate(filename),
523 bare: opts.bare,
524 header: opts.compile && !opts['no-header'],
525 sourceMap: opts.map
526 };
527 if (filename) {
528 if (base) {
529 cwd = process.cwd();
530 jsPath = outputPath(filename, base);
531 jsDir = path.dirname(jsPath);
532 answer = helpers.merge(answer, {
533 jsPath: jsPath,
534 sourceRoot: path.relative(jsDir, cwd),
535 sourceFiles: [path.relative(cwd, filename)],
536 generatedFile: helpers.baseFileName(jsPath, false, useWinPathSep)
537 });
538 } else {
539 answer = helpers.merge(answer, {
540 sourceRoot: "",
541 sourceFiles: [helpers.baseFileName(filename, false, useWinPathSep)],
542 generatedFile: helpers.baseFileName(filename, true, useWinPathSep) + ".js"
543 });
544 }
545 }
546 return answer;
547 };
548
549 forkNode = function() {
550 var args, nodeArgs, p;
551 nodeArgs = opts.nodejs.split(/\s+/);
552 args = process.argv.slice(1);
553 args.splice(args.indexOf('--nodejs'), 2);
554 p = spawn(process.execPath, nodeArgs.concat(args), {
555 cwd: process.cwd(),
556 env: process.env,
557 customFds: [0, 1, 2]
558 });
559 return p.on('exit', function(code) {
560 return process.exit(code);
561 });
562 };
563
564 usage = function() {
565 return printLine((new optparse.OptionParser(SWITCHES, BANNER)).help());
566 };
567
568 version = function() {
569 return printLine("CoffeeScript version " + CoffeeScript.VERSION);
570 };
571
572 }).call(this);