CLI: Run index.coffee when called on a directory

This commit is contained in:
Marc Häfner
2013-12-24 01:53:27 +01:00
parent 308299fe04
commit 8a3ebb9181
2 changed files with 39 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.6.3
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, 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,
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,
__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');
@@ -117,6 +117,10 @@
notSources[source] = true;
return;
}
if (opts.run) {
compilePath(findDirectoryIndex(source), topLevel, base);
return;
}
if (opts.watch) {
watchDir(source, base);
}
@@ -159,6 +163,27 @@
}
};
findDirectoryIndex = function(source) {
var err, ext, index, _i, _len, _ref1;
_ref1 = CoffeeScript.FILE_EXTENSIONS;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
ext = _ref1[_i];
index = path.join(source, "index" + ext);
try {
if ((fs.statSync(index)).isFile()) {
return index;
}
} catch (_error) {
err = _error;
if (err.code !== 'ENOENT') {
throw err;
}
}
}
console.error("Missing index.coffee or index.litcoffee in " + source);
return process.exit(1);
};
compileScript = function(file, input, base) {
var compiled, err, message, o, options, t, task;
if (base == null) {

View File

@@ -104,6 +104,9 @@ compilePath = (source, topLevel, base) ->
if path.basename(source) is 'node_modules'
notSources[source] = yes
return
if opts.run
compilePath findDirectoryIndex(source), topLevel, base
return
watchDir source, base if opts.watch
try
files = fs.readdirSync source
@@ -124,6 +127,16 @@ compilePath = (source, topLevel, base) ->
else
notSources[source] = yes
findDirectoryIndex = (source) ->
for ext in CoffeeScript.FILE_EXTENSIONS
index = path.join source, "index#{ext}"
try
return index if (fs.statSync index).isFile()
catch err
throw err unless err.code is 'ENOENT'
console.error "Missing index.coffee or index.litcoffee in #{source}"
process.exit 1
# Compile a single source script, containing the given code, according to the
# requested options. If evaluating the script directly sets `__filename`,
# `__dirname` and `module.filename` to be correct relative to the script's path.