1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Got --watch with directory watching and addition / removal of inner subdirectories working...

This commit is contained in:
Jeremy Ashkenas 2011-12-18 12:26:04 -05:00
parent b9805f3f80
commit 460b5d6edb
2 changed files with 93 additions and 47 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, watch, watchDir, writeJs, _ref;
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, watch, watchDir, watchers, writeJs, _ref;
fs = require('fs');
@ -37,6 +37,8 @@
notSources = {};
watchers = {};
optionParser = null;
exports.run = function() {
@ -60,7 +62,7 @@
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
source = sources[_i];
_results.push(compilePath(source, true, path.join(source)));
_results.push(compilePath(source, true, path.normalize(source)));
}
return _results;
};
@ -105,7 +107,7 @@
});
} else {
notSources[source] = true;
return removeSource(source);
return removeSource(source, base);
}
});
});
@ -164,6 +166,7 @@
};
compileJoin = function() {
if (!opts.join) return;
if (!sourceCode.some(function(code) {
return code === null;
})) {
@ -210,15 +213,8 @@
compile();
return watcher = fs.watch(source, callback);
} else {
removeSource(source);
if (opts.join) {
compileJoin();
} else {
fs.unlink(outputPath(source, base), function(err) {
if (err) throw err;
});
}
return timeLog("removed " + source);
removeSource(source, base, true);
return compileJoin();
}
});
}, 250);
@ -229,30 +225,67 @@
watchDir = function(source, base) {
var watcher;
return watcher = fs.watch(source, function() {
return fs.readdir(source, function(err, files) {
var file, _i, _len, _results;
if (err) throw err;
files = files.map(function(file) {
return path.join(source, file);
});
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (!(!notSources[file] && sources.indexOf(file) < 0)) continue;
sources.push(file);
sourceCode.push(null);
_results.push(compilePath(file, false, base));
return path.exists(source, function(exists) {
var file, toRemove, _i, _len;
if (exists) {
return fs.readdir(source, function(err, files) {
var file, _i, _len, _results;
if (err) throw err;
files = files.map(function(file) {
return path.join(source, file);
});
_results = [];
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;
})) {
continue;
}
sources.push(file);
sourceCode.push(null);
_results.push(compilePath(file, false, base));
}
return _results;
});
} else {
watcher.close();
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();
}
return _results;
});
});
};
removeSource = function(source) {
var index;
removeSource = function(source, base, removeJs) {
var index, jsPath;
index = sources.indexOf(source);
sources.splice(index, 1);
return sourceCode.splice(index, 1);
sourceCode.splice(index, 1);
if (removeJs && !opts.join) {
jsPath = outputPath(source, base);
return path.exists(jsPath, function(exists) {
if (exists) {
return fs.unlink(jsPath, function(err) {
if (err) throw err;
return timeLog("removed " + source);
});
}
});
}
};
outputPath = function(source, base) {

View file

@ -51,6 +51,7 @@ opts = {}
sources = []
sourceCode = []
notSources = {}
watchers = {}
optionParser = null
# Run `coffee` by parsing passed options and determining what action to take.
@ -74,7 +75,7 @@ exports.run = ->
process.argv[0] = 'coffee'
process.execPath = require.main.filename
for source in sources
compilePath source, yes, path.join source
compilePath source, yes, path.normalize source
# Compile a path, which could be a script or a directory. If a directory
# is passed, recursively compile all '.coffee' extension source files in it
@ -105,7 +106,7 @@ compilePath = (source, topLevel, base) ->
compileScript(source, code.toString(), base)
else
notSources[source] = yes
removeSource source
removeSource source, base
# Compile a single source script, containing the given code, according to the
@ -149,6 +150,7 @@ compileStdio = ->
# If all of the source files are done being read, concatenate and compile
# them together.
compileJoin = ->
return unless opts.join
unless sourceCode.some((code) -> code is null)
compileScript opts.join, sourceCode.join('\n'), opts.join
@ -187,31 +189,42 @@ watch = (source, base) ->
compile()
watcher = fs.watch source, callback
else
removeSource source
if opts.join
compileJoin()
else
fs.unlink outputPath(source, base), (err) ->
throw err if err
timeLog "removed #{source}"
removeSource source, base, yes
compileJoin()
, 250
# Watch a directory of files for new additions.
watchDir = (source, base) ->
watcher = fs.watch source, ->
fs.readdir source, (err, files) ->
throw err if err
files = files.map (file) -> path.join source, file
for file in files when not notSources[file] and sources.indexOf(file) < 0
sources.push file
sourceCode.push null
compilePath file, no, base
path.exists source, (exists) ->
if exists
fs.readdir source, (err, files) ->
throw err if err
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
else
watcher.close()
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
compileJoin()
# Remove a file from our source list, and source code cache.
removeSource = (source) ->
# Remove a file from our source list, and source code cache. Optionally remove
# the compiled JS version as well.
removeSource = (source, base, removeJs) ->
index = sources.indexOf source
sources.splice index, 1
sourceCode.splice index, 1
if removeJs and not opts.join
jsPath = outputPath source, base
path.exists jsPath, (exists) ->
if exists
fs.unlink jsPath, (err) ->
throw err if err
timeLog "removed #{source}"
# Get the corresponding output JavaScript path for a source file.
outputPath = (source, base) ->