"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/coffee-script/lib/coffee-script/command.js" (7 Feb 2017, 17933 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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