Permalink
Browse files

Directory compilation

  • Loading branch information...
1 parent 94505b6 commit e428e872fe10db0dba903c9f064da82c0c58d07f @sstephenson committed May 24, 2011
Showing with 156 additions and 35 deletions.
  1. +92 −19 lib/command.js
  2. +64 −16 src/command.coffee
View
111 lib/command.js
@@ -1,10 +1,11 @@
(function() {
- var compileFile, each, eco, fs, indent, parseOptions, path, preprocessArgs, printUsage, printVersion, read, sys;
+ var compile, compileToFile, each, eachFile, eco, exec, fs, indent, mkdir, parseOptions, path, preprocessArgs, printUsage, printVersion, read, stripExtension, sys;
var __slice = Array.prototype.slice;
fs = require("fs");
path = require("path");
sys = require("sys");
eco = require(".");
+ exec = require("child_process").exec;
indent = require("./util").indent;
printUsage = function() {
console.error("\nUsage: eco [options] path/to/template.eco\n\n -o, --output [DIR] set the directory for compiled JavaScript\n -p, --print print the compiled JavaScript to stdout\n -s, --stdio listen for and compile templates over stdio\n -v, --version display Eco version\n -h, --help display this help message\n");
@@ -95,20 +96,95 @@
return callback(values.shift(), proceed);
})();
};
- compileFile = function(infile, callback) {
- var basename;
- basename = path.basename(infile).replace(/(\.eco)?$/, "");
+ eachFile = function(files, callback) {
+ var traverse;
+ traverse = function(root, dir, done) {
+ return fs.readdir(dir, function(err, entries) {
+ if (err) {
+ return callback(err);
+ }
+ return each(entries, function(entry, proceed) {
+ var file;
+ if (entry == null) {
+ return done();
+ }
+ file = path.join(dir, entry);
+ return fs.stat(file, function(err, stat) {
+ if (err) {
+ return callback(err);
+ }
+ if (stat.isFile() && /\.eco$/.test(file)) {
+ return callback(null, file, root, proceed);
+ } else if (stat.isDirectory()) {
+ return traverse(root, file, proceed);
+ } else {
+ return proceed();
+ }
+ });
+ });
+ });
+ };
+ return each(files, function(file, proceed) {
+ if (file == null) {
+ return;
+ }
+ return fs.stat(file, function(err, stat) {
+ var root;
+ if (err) {
+ return callback(err);
+ }
+ if (stat.isDirectory()) {
+ return traverse(file, file, proceed);
+ } else {
+ root = path.dirname(file);
+ return callback(null, file, root, proceed);
+ }
+ });
+ });
+ };
+ stripExtension = function(name) {
+ return name.replace(/(\.eco)?$/, "");
+ };
+ compile = function(infile, name, callback) {
return fs.readFile(infile, "utf8", function(err, source) {
var template;
if (err) {
return callback(err);
}
template = indent(eco.compile(source), 2);
- return callback(null, basename, "(function() {\n this.ecoTemplates || (this.ecoTemplates = {});\n this.ecoTemplates[" + (JSON.stringify(basename)) + "] = " + (template.slice(2)) + ";\n}).call(this);");
+ return callback(null, "(function() {\n this.ecoTemplates || (this.ecoTemplates = {});\n this.ecoTemplates[" + (JSON.stringify(name)) + "] = " + (template.slice(2)) + ";\n}).call(this);");
+ });
+ };
+ mkdir = function(dir, callback) {
+ return exec("mkdir -p " + (JSON.stringify(dir)), function(err, stdout, stderr) {
+ return callback(err);
+ });
+ };
+ compileToFile = function(infile, root, outdir, callback) {
+ var name, outfile;
+ root = path.join(root, "/");
+ if (root === infile.slice(0, root.length)) {
+ name = stripExtension(infile.slice(root.length));
+ } else {
+ name = stripExtension(infile);
+ }
+ outfile = path.join(outdir != null ? outdir : root, name + ".js");
+ return mkdir(path.dirname(outfile), function(err) {
+ if (err) {
+ return callback(err);
+ }
+ return compile(infile, name, function(err, result) {
+ return fs.writeFile(outfile, result, "utf8", function(err) {
+ if (err) {
+ return callback(err);
+ }
+ return callback(null, outfile, name);
+ });
+ });
});
};
exports.run = function(args) {
- var options;
+ var infile, name, options;
if (args == null) {
args = process.argv.slice(2);
}
@@ -125,7 +201,9 @@
if (options.files.length !== 1 || options.output) {
printUsage();
}
- return compileFile(options.files[0], function(err, basename, result) {
+ infile = options.files[0];
+ name = stripExtension(path.basename(infile));
+ return compile(infile, name, function(err, result) {
if (err) {
throw err;
}
@@ -135,24 +213,19 @@
if (!options.files.length) {
printUsage();
}
- return each(options.files, function(infile, proceed) {
+ return eachFile(options.files, function(err, infile, root, proceed) {
+ if (err) {
+ throw err;
+ }
if (infile == null) {
return;
}
- return compileFile(infile, function(err, basename, result) {
- var outdir, outfile, _ref;
+ return compileToFile(infile, root, options.output, function(err, outfile, name) {
if (err) {
throw err;
}
- outdir = (_ref = options.output) != null ? _ref : path.dirname(infile);
- outfile = path.join(outdir, basename + ".eco");
- return fs.writeFile(outfile, result, "utf8", function(err) {
- if (err) {
- return callback(err);
- }
- console.error("Compiled " + infile + " -> " + outfile);
- return proceed();
- });
+ console.error("" + (JSON.stringify(name)) + ": " + infile + " -> " + outfile);
+ return proceed();
});
});
}
View
80 src/command.coffee
@@ -3,6 +3,7 @@ path = require "path"
sys = require "sys"
eco = require "."
+{exec} = require "child_process"
{indent} = require "./util"
printUsage = ->
@@ -62,20 +63,68 @@ read = (stream, callback) ->
each = ([values...], callback) ->
do proceed = -> callback values.shift(), proceed
-compileFile = (infile, callback) ->
- basename = path.basename(infile).replace /(\.eco)?$/, ""
-
+eachFile = (files, callback) ->
+ traverse = (root, dir, done) ->
+ fs.readdir dir, (err, entries) ->
+ return callback err if err
+ each entries, (entry, proceed) ->
+ return done() unless entry?
+ file = path.join dir, entry
+ fs.stat file, (err, stat) ->
+ return callback err if err
+ if stat.isFile() and /\.eco$/.test file
+ callback null, file, root, proceed
+ else if stat.isDirectory()
+ traverse root, file, proceed
+ else
+ proceed()
+
+ each files, (file, proceed) ->
+ return unless file?
+ fs.stat file, (err, stat) ->
+ return callback err if err
+ if stat.isDirectory()
+ traverse file, file, proceed
+ else
+ root = path.dirname file
+ callback null, file, root, proceed
+
+stripExtension = (name) ->
+ name.replace /(\.eco)?$/, ""
+
+compile = (infile, name, callback) ->
fs.readFile infile, "utf8", (err, source) ->
return callback err if err
template = indent eco.compile(source), 2
- callback null, basename, """
+ callback null, """
(function() {
this.ecoTemplates || (this.ecoTemplates = {});
- this.ecoTemplates[#{JSON.stringify basename}] = #{template.slice 2};
+ this.ecoTemplates[#{JSON.stringify name}] = #{template.slice 2};
}).call(this);
"""
+mkdir = (dir, callback) ->
+ exec "mkdir -p #{JSON.stringify dir}", (err, stdout, stderr) ->
+ callback err
+
+compileToFile = (infile, root, outdir, callback) ->
+ root = path.join root, "/"
+
+ if root is infile.slice 0, root.length
+ name = stripExtension infile.slice root.length
+ else
+ name = stripExtension infile
+
+ outfile = path.join outdir ? root, name + ".js"
+
+ mkdir path.dirname(outfile), (err) ->
+ return callback err if err
+ compile infile, name, (err, result) ->
+ fs.writeFile outfile, result, "utf8", (err) ->
+ return callback err if err
+ callback null, outfile, name
+
exports.run = (args = process.argv.slice 2) ->
options = parseOptions args
@@ -87,21 +136,20 @@ exports.run = (args = process.argv.slice 2) ->
else if options.print
printUsage() if options.files.length isnt 1 or options.output
- compileFile options.files[0], (err, basename, result) ->
+ infile = options.files[0]
+ name = stripExtension path.basename infile
+
+ compile infile, name, (err, result) ->
throw err if err
sys.puts result
else
printUsage() unless options.files.length
- each options.files, (infile, proceed) ->
+ eachFile options.files, (err, infile, root, proceed) ->
+ throw err if err
return unless infile?
- compileFile infile, (err, basename, result) ->
- throw err if err
-
- outdir = options.output ? path.dirname infile
- outfile = path.join outdir, basename + ".eco"
- fs.writeFile outfile, result, "utf8", (err) ->
- throw err if err
- console.error "Compiled #{infile} -> #{outfile}"
- proceed()
+ compileToFile infile, root, options.output, (err, outfile, name) ->
+ throw err if err
+ console.error "#{JSON.stringify name}: #{infile} -> #{outfile}"
+ proceed()

0 comments on commit e428e87

Please sign in to comment.