Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP on new command-line compiler

  • Loading branch information...
commit a3245635f815d12403d178a7bc1de0a041c27f4d 1 parent 4c952f2
Sam Stephenson authored
Showing with 240 additions and 19 deletions.
  1. +141 −11 lib/command.js
  2. +99 −8 src/command.coffee
152 lib/command.js
View
@@ -1,18 +1,148 @@
(function() {
- var eco, fs;
- fs = "fs";
+ var compileFile, each, eco, fs, indent, parseOptions, path, preprocessArgs, printUsage, printVersion, read, sys;
+ var __slice = Array.prototype.slice;
+ fs = require("fs");
+ path = require("path");
+ sys = require("sys");
eco = require(".");
- exports.run = function() {
- var source, stdin;
- source = "";
- stdin = process.openStdin();
- stdin.on("data", function(buffer) {
- if (buffer) {
- return source += buffer.toString();
+ 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");
+ return process.exit(1);
+ };
+ printVersion = function() {
+ var package;
+ package = JSON.parse(fs.readFileSync(__dirname + "/../../package.json", "utf8"));
+ console.error("Eco version " + package.version);
+ return process.exit(0);
+ };
+ preprocessArgs = function(args) {
+ var arg, char, match, result, _i, _j, _len, _len2, _ref;
+ result = [];
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
+ arg = args[_i];
+ if (match = /^-([a-z]{2,})/.exec(arg)) {
+ _ref = match[1].split('');
+ for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
+ char = _ref[_j];
+ result.push("-" + char);
+ }
+ } else {
+ result.push(arg);
}
+ }
+ return result;
+ };
+ parseOptions = function(args) {
+ var arg, option, options, _i, _len, _ref;
+ options = {
+ files: []
+ };
+ option = null;
+ _ref = preprocessArgs(args);
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ arg = _ref[_i];
+ if (option) {
+ options[option] = arg;
+ option = null;
+ } else {
+ switch (arg) {
+ case "-o":
+ case "--output":
+ option = "output";
+ break;
+ case "-p":
+ case "--print":
+ options.print = true;
+ break;
+ case "-s":
+ case "--stdio":
+ options.stdio = true;
+ break;
+ case "-v":
+ case "--version":
+ printVersion();
+ break;
+ default:
+ if (/^-/.test(arg)) {
+ printUsage();
+ } else {
+ options.files.push(arg);
+ }
+ }
+ }
+ }
+ if (option) {
+ printUsage();
+ }
+ return options;
+ };
+ read = function(stream, callback) {
+ var buffer;
+ buffer = "";
+ stream.setEncoding("utf8");
+ stream.on("data", function(data) {
+ return buffer += data;
+ });
+ return stream.on("end", function() {
+ return typeof callback === "function" ? callback(buffer) : void 0;
});
- return stdin.on("end", function() {
- return console.log(eco.compile(source));
+ };
+ each = function(_arg, callback) {
+ var proceed, values;
+ values = 1 <= _arg.length ? __slice.call(_arg, 0) : [];
+ return (proceed = function() {
+ return callback(values.shift(), proceed);
+ })();
+ };
+ compileFile = function(infile, outdir, callback) {
+ var basename, dirname, outfile;
+ dirname = path.dirname(infile);
+ basename = path.basename(infile).replace(/(\.eco)?$/, "");
+ outdir || (outdir = dirname);
+ outfile = "" + (path.join(outdir, basename)) + ".js";
+ return fs.readFile(infile, "utf8", function(err, source) {
+ var output, template;
+ if (err) {
+ return callback(err);
+ }
+ template = indent(eco.compile(source), 2);
+ output = "(function() {\n this.ecoTemplates || (this.ecoTemplates = {});\n this.ecoTemplates[" + (JSON.stringify(basename)) + "] = " + (template.slice(2)) + ";\n}).call(this);";
+ return fs.writeFile(outfile, output, "utf8", function(err) {
+ if (err) {
+ return callback(err);
+ }
+ return callback(null, outfile);
+ });
});
};
+ exports.run = function() {
+ var options;
+ options = parseOptions(process.argv.slice(2));
+ if (options.stdio) {
+ if (options.files.length || options.output) {
+ printUsage();
+ }
+ process.openStdin();
+ return read(process.stdin, function(source) {
+ return sys.puts(eco.compile(source));
+ });
+ } else {
+ if (!options.files.length) {
+ printUsage();
+ }
+ return each(options.files, function(infile, proceed) {
+ if (!infile) {
+ return;
+ }
+ return compileFile(infile, options.output, function(err, outfile) {
+ if (err) {
+ throw err;
+ }
+ console.error("Compiled " + infile + " -> " + outfile);
+ return proceed();
+ });
+ });
+ }
+ };
}).call(this);
107 src/command.coffee
View
@@ -1,10 +1,101 @@
-fs = "fs"
-eco = require "."
+fs = require "fs"
+path = require "path"
+sys = require "sys"
+eco = require "."
+
+{indent} = require "./util"
+
+printUsage = ->
+ console.error """
+
+ Usage: eco [options] path/to/template.eco
+
+ -o, --output [DIR] set the directory for compiled JavaScript
+ -p, --print print the compiled JavaScript to stdout
+ -s, --stdio listen for and compile templates over stdio
+ -v, --version display Eco version
+ -h, --help display this help message
+
+ """
+ process.exit 1
+
+printVersion = ->
+ package = JSON.parse fs.readFileSync __dirname + "/../../package.json", "utf8"
+ console.error "Eco version #{package.version}"
+ process.exit 0
+
+preprocessArgs = (args) ->
+ result = []
+ for arg in args
+ if match = /^-([a-z]{2,})/.exec arg
+ for char in match[1].split ''
+ result.push "-#{char}"
+ else
+ result.push arg
+ result
+
+parseOptions = (args) ->
+ options = files: []
+ option = null
+
+ for arg in preprocessArgs args
+ if option
+ options[option] = arg
+ option = null
+ else
+ switch arg
+ when "-o", "--output" then option = "output"
+ when "-p", "--print" then options.print = true
+ when "-s", "--stdio" then options.stdio = true
+ when "-v", "--version" then printVersion()
+ else (if /^-/.test arg then printUsage() else options.files.push arg)
+
+ printUsage() if option
+ options
+
+read = (stream, callback) ->
+ buffer = ""
+ stream.setEncoding "utf8"
+ stream.on "data", (data) -> buffer += data
+ stream.on "end", -> callback? buffer
+
+each = ([values...], callback) ->
+ do proceed = -> callback values.shift(), proceed
+
+compileFile = (infile, outdir, callback) ->
+ dirname = path.dirname infile
+ basename = path.basename(infile).replace /(\.eco)?$/, ""
+ outdir or= dirname
+ outfile = "#{path.join outdir, basename}.js"
+
+ fs.readFile infile, "utf8", (err, source) ->
+ return callback err if err
+ template = indent eco.compile(source), 2
+
+ output = """
+ (function() {
+ this.ecoTemplates || (this.ecoTemplates = {});
+ this.ecoTemplates[#{JSON.stringify basename}] = #{template.slice 2};
+ }).call(this);
+ """
+
+ fs.writeFile outfile, output, "utf8", (err) ->
+ return callback err if err
+ callback null, outfile
exports.run = ->
- source = ""
- stdin = process.openStdin()
- stdin.on "data", (buffer) ->
- source += buffer.toString() if buffer
- stdin.on "end", ->
- console.log eco.compile source
+ options = parseOptions process.argv.slice 2
+
+ if options.stdio
+ printUsage() if options.files.length or options.output
+ process.openStdin()
+ read process.stdin, (source) ->
+ sys.puts eco.compile source
+ else
+ printUsage() unless options.files.length
+ each options.files, (infile, proceed) ->
+ return unless infile
+ compileFile infile, options.output, (err, outfile) ->
+ throw err if err
+ console.error "Compiled #{infile} -> #{outfile}"
+ proceed()
Please sign in to comment.
Something went wrong with that request. Please try again.