Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement -p

  • Loading branch information...
commit 94505b6e55b91f596f21bc2a7f643d93c3f674c4 1 parent a324563
@sstephenson authored
Showing with 54 additions and 36 deletions.
  1. +32 −20 lib/command.js
  2. +22 −16 src/command.coffee
View
52 lib/command.js
@@ -12,7 +12,7 @@
};
printVersion = function() {
var package;
- package = JSON.parse(fs.readFileSync(__dirname + "/../../package.json", "utf8"));
+ package = JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8"));
console.error("Eco version " + package.version);
return process.exit(0);
};
@@ -95,30 +95,24 @@
return callback(values.shift(), proceed);
})();
};
- compileFile = function(infile, outdir, callback) {
- var basename, dirname, outfile;
- dirname = path.dirname(infile);
+ compileFile = function(infile, callback) {
+ var basename;
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;
+ var 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);
- });
+ return callback(null, basename, "(function() {\n this.ecoTemplates || (this.ecoTemplates = {});\n this.ecoTemplates[" + (JSON.stringify(basename)) + "] = " + (template.slice(2)) + ";\n}).call(this);");
});
};
- exports.run = function() {
+ exports.run = function(args) {
var options;
- options = parseOptions(process.argv.slice(2));
+ if (args == null) {
+ args = process.argv.slice(2);
+ }
+ options = parseOptions(args);
if (options.stdio) {
if (options.files.length || options.output) {
printUsage();
@@ -127,20 +121,38 @@
return read(process.stdin, function(source) {
return sys.puts(eco.compile(source));
});
+ } else if (options.print) {
+ if (options.files.length !== 1 || options.output) {
+ printUsage();
+ }
+ return compileFile(options.files[0], function(err, basename, result) {
+ if (err) {
+ throw err;
+ }
+ return sys.puts(result);
+ });
} else {
if (!options.files.length) {
printUsage();
}
return each(options.files, function(infile, proceed) {
- if (!infile) {
+ if (infile == null) {
return;
}
- return compileFile(infile, options.output, function(err, outfile) {
+ return compileFile(infile, function(err, basename, result) {
+ var outdir, outfile, _ref;
if (err) {
throw err;
}
- console.error("Compiled " + infile + " -> " + outfile);
- return proceed();
+ 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();
+ });
});
});
}
View
38 src/command.coffee
@@ -20,7 +20,7 @@ printUsage = ->
process.exit 1
printVersion = ->
- package = JSON.parse fs.readFileSync __dirname + "/../../package.json", "utf8"
+ package = JSON.parse fs.readFileSync __dirname + "/../package.json", "utf8"
console.error "Eco version #{package.version}"
process.exit 0
@@ -62,40 +62,46 @@ read = (stream, callback) ->
each = ([values...], callback) ->
do proceed = -> callback values.shift(), proceed
-compileFile = (infile, outdir, callback) ->
- dirname = path.dirname infile
+compileFile = (infile, callback) ->
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 = """
+ callback null, basename, """
(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 = ->
- options = parseOptions process.argv.slice 2
+exports.run = (args = process.argv.slice 2) ->
+ options = parseOptions args
if options.stdio
printUsage() if options.files.length or options.output
process.openStdin()
read process.stdin, (source) ->
sys.puts eco.compile source
+
+ else if options.print
+ printUsage() if options.files.length isnt 1 or options.output
+ compileFile options.files[0], (err, basename, result) ->
+ throw err if err
+ sys.puts result
+
else
printUsage() unless options.files.length
each options.files, (infile, proceed) ->
- return unless infile
- compileFile infile, options.output, (err, outfile) ->
+ return unless infile?
+ compileFile infile, (err, basename, result) ->
throw err if err
- console.error "Compiled #{infile} -> #{outfile}"
- proceed()
+
+ 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()
Please sign in to comment.
Something went wrong with that request. Please try again.