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

Correcting and cleaning up --join ... now it works in conjunction with --watch

This commit is contained in:
Jeremy Ashkenas 2011-12-18 10:27:22 -05:00
parent ee8a1a3b68
commit 7c6bc95a84
2 changed files with 67 additions and 88 deletions

View file

@ -1,5 +1,5 @@
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compileScript, compileScripts, compileStdio, contents, exec, forkNode, fs, helpers, lint, loadRequires, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sources, spawn, usage, version, watch, writeJs, _ref;
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compileScript, compileScripts, compileStdio, exec, forkNode, fs, helpers, lint, loadRequires, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sourceCode, sources, spawn, usage, version, watch, writeJs, _ref;
fs = require('fs');
@ -33,7 +33,7 @@
sources = [];
contents = [];
sourceCode = [];
optionParser = null;
@ -58,41 +58,17 @@
};
compileScripts = function() {
var base, compile, remainingFiles, source, trackCompleteFiles, trackUnprocessedFiles, unprocessed, _i, _j, _len, _len2, _results;
unprocessed = [];
remainingFiles = function() {
var total, x, _i, _len;
total = 0;
for (_i = 0, _len = unprocessed.length; _i < _len; _i++) {
x = unprocessed[_i];
total += x;
}
return total;
};
trackUnprocessedFiles = function(sourceIndex, fileCount) {
if (unprocessed[sourceIndex] == null) unprocessed[sourceIndex] = 0;
return unprocessed[sourceIndex] += fileCount;
};
trackCompleteFiles = function(sourceIndex, fileCount) {
unprocessed[sourceIndex] -= fileCount;
if (opts.join) {
if (helpers.compact(contents).length > 0 && remainingFiles() === 0) {
return compileJoin();
}
}
};
for (_i = 0, _len = sources.length; _i < _len; _i++) {
source = sources[_i];
trackUnprocessedFiles(sources.indexOf(source), 1);
}
var base, compile, index, source, _len, _results;
_results = [];
for (_j = 0, _len2 = sources.length; _j < _len2; _j++) {
source = sources[_j];
for (index = 0, _len = sources.length; index < _len; index++) {
source = sources[index];
sourceCode[index] = null;
base = path.join(source);
compile = function(source, sourceIndex, topLevel) {
compile = function(source, topLevel) {
return path.exists(source, function(exists) {
if (topLevel && !exists && source.slice(-7) !== '.coffee') {
return compile("" + source + ".coffee", sourceIndex, topLevel);
source = sources[sources.indexOf(source)] = "" + source + ".coffee";
return compile(source, topLevel);
}
if (topLevel && !exists) {
console.error("File not found: " + source);
@ -102,33 +78,38 @@
if (err) throw err;
if (stats.isDirectory()) {
return fs.readdir(source, function(err, files) {
var file, _k, _len3;
var file, _i, _len2, _ref2, _results2;
if (err) throw err;
trackUnprocessedFiles(sourceIndex, files.length);
for (_k = 0, _len3 = files.length; _k < _len3; _k++) {
file = files[_k];
compile(path.join(source, file), sourceIndex);
files = files.map(function(file) {
return path.join(source, file);
});
index = sources.indexOf(source);
[].splice.apply(sources, [index, index - index + 1].concat(files)), files;
[].splice.apply(sourceCode, [index, index - index + 1].concat(_ref2 = files.map(function() {
return null;
}))), _ref2;
_results2 = [];
for (_i = 0, _len2 = files.length; _i < _len2; _i++) {
file = files[_i];
_results2.push(compile(file));
}
return trackCompleteFiles(sourceIndex, 1);
return _results2;
});
} else if (topLevel || path.extname(source) === '.coffee') {
fs.readFile(source, function(err, code) {
if (err) throw err;
if (opts.join) {
contents[sourceIndex] = helpers.compact([contents[sourceIndex], code.toString()]).join('\n');
} else {
compileScript(source, code.toString(), base);
}
return trackCompleteFiles(sourceIndex, 1);
return compileScript(source, code.toString(), base);
});
if (opts.watch && !opts.join) return watch(source, base);
if (opts.watch) return watch(source, base);
} else {
return trackCompleteFiles(sourceIndex, 1);
index = sources.indexOf(source);
sources.splice(index, 1);
return sourceCode.splice(index, 1);
}
});
});
};
_results.push(compile(source, sources.indexOf(source), true));
_results.push(compile(source, true));
}
return _results;
};
@ -150,6 +131,8 @@
return printLine(CoffeeScript.nodes(t.input).toString().trim());
} else if (o.run) {
return CoffeeScript.run(t.input, t.options);
} else if (o.join && file !== o.join) {
return compileJoin(t.file, t.input);
} else {
t.output = CoffeeScript.compile(t.input, t.options);
CoffeeScript.emit('success', task);
@ -182,10 +165,13 @@
});
};
compileJoin = function() {
var code;
code = contents.join('\n');
return compileScript(opts.join, code, opts.join);
compileJoin = function(file, code) {
sourceCode[sources.indexOf(file)] = code;
if (!sourceCode.some(function(code) {
return code === null;
})) {
return compileScript(opts.join, sourceCode.join('\n'), opts.join);
}
};
loadRequires = function() {

View file

@ -49,7 +49,7 @@ SWITCHES = [
# Top-level objects shared by all the functions.
opts = {}
sources = []
contents = []
sourceCode = []
optionParser = null
# Run `coffee` by parsing passed options and determining what action to take.
@ -78,27 +78,17 @@ exports.run = ->
# compile them. If a directory is passed, recursively compile all
# '.coffee' extension source files in it and all subdirectories.
compileScripts = ->
unprocessed = []
remainingFiles = ->
total = 0
total += x for x in unprocessed
total
trackUnprocessedFiles = (sourceIndex, fileCount) ->
unprocessed[sourceIndex] ?= 0
unprocessed[sourceIndex] += fileCount
trackCompleteFiles = (sourceIndex, fileCount) ->
unprocessed[sourceIndex] -= fileCount
if opts.join
if helpers.compact(contents).length > 0 and remainingFiles() is 0
compileJoin()
for source in sources
trackUnprocessedFiles sources.indexOf(source), 1
for source in sources
base = path.join(source)
compile = (source, sourceIndex, topLevel) ->
for source, index in sources
sourceCode[index] = null
base = path.join source
compile = (source, topLevel) ->
path.exists source, (exists) ->
if topLevel and not exists and source[-7..] isnt '.coffee'
return compile "#{source}.coffee", sourceIndex, topLevel
source = sources[sources.indexOf(source)] = "#{source}.coffee"
return compile source, topLevel
if topLevel and not exists
console.error "File not found: #{source}"
process.exit 1
@ -107,22 +97,22 @@ compileScripts = ->
if stats.isDirectory()
fs.readdir source, (err, files) ->
throw err if err
trackUnprocessedFiles sourceIndex, files.length
for file in files
compile path.join(source, file), sourceIndex
trackCompleteFiles sourceIndex, 1
files = files.map (file) -> path.join source, file
index = sources.indexOf source
sources[index..index] = files
sourceCode[index..index] = files.map -> null
compile file for file in files
else if topLevel or path.extname(source) is '.coffee'
fs.readFile source, (err, code) ->
throw err if err
if opts.join
contents[sourceIndex] = helpers.compact([contents[sourceIndex], code.toString()]).join('\n')
else
compileScript(source, code.toString(), base)
trackCompleteFiles sourceIndex, 1
watch source, base if opts.watch and not opts.join
compileScript(source, code.toString(), base)
watch source, base if opts.watch
else
trackCompleteFiles sourceIndex, 1
compile source, sources.indexOf(source), true
index = sources.indexOf source
sources.splice index, 1
sourceCode.splice index, 1
compile source, true
# Compile a single source script, containing the given code, according to the
# requested options. If evaluating the script directly sets `__filename`,
@ -136,6 +126,8 @@ compileScript = (file, input, base) ->
if o.tokens then printTokens CoffeeScript.tokens t.input
else if o.nodes then printLine CoffeeScript.nodes(t.input).toString().trim()
else if o.run then CoffeeScript.run t.input, t.options
else if o.join and file isnt o.join
compileJoin t.file, t.input
else
t.output = CoffeeScript.compile t.input, t.options
CoffeeScript.emit 'success', task
@ -159,11 +151,12 @@ compileStdio = ->
stdin.on 'end', ->
compileScript null, code
# After all of the source files are done being read, concatenate and compile
# If all of the source files are done being read, concatenate and compile
# them together.
compileJoin = ->
code = contents.join '\n'
compileScript opts.join, code, opts.join
compileJoin = (file, code) ->
sourceCode[sources.indexOf(file)] = code
unless sourceCode.some((code) -> code is null)
compileScript opts.join, sourceCode.join('\n'), opts.join
# Load files that are to-be-required before compilation occurs.
loadRequires = ->