1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
This commit is contained in:
Jeremy Ashkenas 2011-12-18 15:23:42 -05:00
commit c75c4421a7
2 changed files with 76 additions and 59 deletions

View file

@ -1,5 +1,5 @@
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, forkNode, fs, helpers, lint, loadRequires, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, usage, version, wait, watch, watchDir, watchers, writeJs, _ref;
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, forkNode, fs, helpers, joinTimeout, lint, loadRequires, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, version, wait, watch, watchDir, watchers, writeJs, _ref;
fs = require('fs');
@ -85,7 +85,8 @@
if (opts.watch) watchDir(source, base);
return fs.readdir(source, function(err, files) {
var file, index, _i, _len, _ref2, _results;
if (err) throw err;
if (err && err.code !== 'ENOENT') throw err;
if ((err != null ? err.code : void 0) === 'ENOENT') return;
files = files.map(function(file) {
return path.join(source, file);
});
@ -104,7 +105,8 @@
} else if (topLevel || path.extname(source) === '.coffee') {
if (opts.watch) watch(source, base);
return fs.readFile(source, function(err, code) {
if (err) throw err;
if (err && err.code !== 'ENOENT') throw err;
if ((err != null ? err.code : void 0) === 'ENOENT') return;
return compileScript(source, code.toString(), base);
});
} else {
@ -166,12 +168,17 @@
});
};
joinTimeout = null;
compileJoin = function() {
if (!opts.join) return;
if (!sourceCode.some(function(code) {
return code === null;
})) {
return compileScript(opts.join, sourceCode.join('\n'), opts.join);
clearTimeout(joinTimeout);
return joinTimeout = setTimeout(function() {
return compileScript(opts.join, sourceCode.join('\n'), opts.join);
}, 100);
}
};
@ -193,6 +200,7 @@
compileTimeout = null;
watchErr = function(e) {
if (e.code === 'ENOENT') {
if (sources.indexOf(source) === -1) return;
removeSource(source, base, true);
return compileJoin();
} else {
@ -242,51 +250,25 @@
};
watchDir = function(source, base) {
var watchErr, watcher;
watchErr = function(e) {
var file, toRemove, _i, _len;
toRemove = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
file = sources[_i];
if (file.indexOf(source) >= 0) _results.push(file);
}
return _results;
})();
for (_i = 0, _len = toRemove.length; _i < _len; _i++) {
file = toRemove[_i];
removeSource(file, base, true);
}
return compileJoin();
};
var readdirTimeout, watcher;
readdirTimeout = null;
try {
return watcher = fs.watch(source, function() {
return fs.readdir(source, function(err, files) {
var file, toRemove, _i, _j, _len, _len2, _results;
if (err) {
if (err.code !== 'ENOENT') throw err;
toRemove = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
file = sources[_i];
if (file.indexOf(source) >= 0) _results.push(file);
}
return _results;
})();
for (_i = 0, _len = toRemove.length; _i < _len; _i++) {
file = toRemove[_i];
removeSource(file, base, true);
clearTimeout(readdirTimeout);
return readdirTimeout = setTimeout(function() {
return fs.readdir(source, function(err, files) {
var file, _i, _len, _results;
if (err) {
if (err.code !== 'ENOENT') throw err;
watcher.close();
return unwatchDir(source, base);
}
return compileJoin();
} else {
files = files.map(function(file) {
return path.join(source, file);
});
_results = [];
for (_j = 0, _len2 = files.length; _j < _len2; _j++) {
file = files[_j];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (!(!notSources[file])) continue;
if (sources.some(function(s) {
return s.indexOf(file) >= 0;
@ -298,14 +280,38 @@
_results.push(compilePath(file, false, base));
}
return _results;
}
});
});
}, 25);
});
} catch (e) {
if (e.code !== 'ENOENT') throw e;
}
};
unwatchDir = function(source, base) {
var file, prevSources, toRemove, _i, _len;
prevSources = sources.slice();
toRemove = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
file = sources[_i];
if (file.indexOf(source) >= 0) _results.push(file);
}
return _results;
})();
for (_i = 0, _len = toRemove.length; _i < _len; _i++) {
file = toRemove[_i];
removeSource(file, base, true);
}
if (!sources.some(function(s, i) {
return prevSources[i] !== s;
})) {
return;
}
return compileJoin();
};
removeSource = function(source, base, removeJs) {
var index, jsPath;
index = sources.indexOf(source);

View file

@ -94,7 +94,8 @@ compilePath = (source, topLevel, base) ->
if stats.isDirectory()
watchDir source, base if opts.watch
fs.readdir source, (err, files) ->
throw err if err
throw err if err and err.code isnt 'ENOENT'
return if err?.code is 'ENOENT'
files = files.map (file) -> path.join source, file
index = sources.indexOf source
sources[index..index] = files
@ -103,7 +104,8 @@ compilePath = (source, topLevel, base) ->
else if topLevel or path.extname(source) is '.coffee'
watch source, base if opts.watch
fs.readFile source, (err, code) ->
throw err if err
throw err if err and err.code isnt 'ENOENT'
return if err?.code is 'ENOENT'
compileScript(source, code.toString(), base)
else
notSources[source] = yes
@ -150,10 +152,14 @@ compileStdio = ->
# If all of the source files are done being read, concatenate and compile
# them together.
joinTimeout = null
compileJoin = ->
return unless opts.join
unless sourceCode.some((code) -> code is null)
compileScript opts.join, sourceCode.join('\n'), opts.join
clearTimeout joinTimeout
joinTimeout = setTimeout ->
compileScript opts.join, sourceCode.join('\n'), opts.join
, 100
# Load files that are to-be-required before compilation occurs.
loadRequires = ->
@ -172,6 +178,7 @@ watch = (source, base) ->
watchErr = (e) ->
if e.code is 'ENOENT'
return if sources.indexOf(source) is -1
removeSource source, base, yes
compileJoin()
else throw e
@ -211,29 +218,33 @@ watch = (source, base) ->
# Watch a directory of files for new additions.
watchDir = (source, base) ->
watchErr = (e) ->
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
compileJoin()
readdirTimeout = null
try
watcher = fs.watch source, ->
fs.readdir source, (err, files) ->
if err
throw err unless err.code is 'ENOENT'
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
compileJoin()
else
clearTimeout readdirTimeout
readdirTimeout = setTimeout ->
fs.readdir source, (err, files) ->
if err
throw err unless err.code is 'ENOENT'
watcher.close()
return unwatchDir source, base
files = files.map (file) -> path.join source, file
for file in files when not notSources[file]
continue if sources.some (s) -> s.indexOf(file) >= 0
sources.push file
sourceCode.push null
compilePath file, no, base
, 25
catch e
throw e unless e.code is 'ENOENT'
unwatchDir = (source, base) ->
prevSources = sources.slice()
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
return unless sources.some (s, i) -> prevSources[i] isnt s
compileJoin()
# Remove a file from our source list, and source code cache. Optionally remove
# the compiled JS version as well.
removeSource = (source, base, removeJs) ->