Permalink
Browse files

way better command line

  • Loading branch information...
1 parent 3c0eb36 commit 3d9ac8c319603155bb866e316f967931d23cf876 @malgorithms committed Sep 5, 2012
Showing with 90 additions and 33 deletions.
  1. +2 −1 Cakefile
  2. +42 −16 lib/command_line.js
  3. +7 −2 package.json
  4. +39 −14 src/command_line.coffee
View
3 Cakefile
@@ -2,7 +2,6 @@
fs = require 'fs'
jison = require 'jison'
path = require 'path'
-{getCommonHeadersJs} = require './lib/view'
task 'build', 'build the whole jam', (cb) ->
console.log "Building"
@@ -43,6 +42,7 @@ buildParser = (cb) ->
cb()
buildCommonHeaders = (cb) ->
+ {getCommonHeadersJs} = require './lib/view'
headers = getCommonHeadersJs true, true
fs.writeFileSync "./toffee.js", headers, "utf8"
cb()
@@ -58,6 +58,7 @@ generateExpressTest = (cb) ->
process.exit(1) if status isnt 0
cb() if typeof cb is 'function'
+ {getCommonHeadersJs} = require './lib/view'
headers = getCommonHeadersJs true, true
fs.writeFileSync "./test/express3/public/javascripts/toffee.js", headers, "utf8"
View
58 lib/command_line.js
@@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.3.3
(function() {
- var compile, fs, getCommonHeadersJs, getVersionNumber, path, program, recurseRun, run, view, _ref;
+ var compile, fs, getCommonHeadersJs, getVersionNumber, maybeAttachHeaders, mkdirp, path, program, recurseRun, run, view, _ref;
fs = require('fs');
@@ -10,6 +10,8 @@
program = require('commander');
+ mkdirp = require('mkdirp');
+
getVersionNumber = function() {
var o, p;
p = fs.readFileSync("" + __dirname + "/../package.json", "utf8");
@@ -21,7 +23,7 @@
return console.log("\n Examples:\n\n toffee views # recurses through views and builds views.js\n toffee foo.toffee # builds foo.js\n toffee views -o templates # builds templates.js\n toffee -p foo.toffee # outputs JS to stdout\n\n\n Then use in your <html>:\n\n <script src=\"views.js\"></script>\n <script>\n var pubvars = { name: \"Hans Gruber\", criminal: true };\n var some_html = toffee.render (\"views/layout.toffee\", pubvars);\n </script>\n ");
});
- program.version(getVersionNumber()).option('-o, --output [path]', 'output to single file').option('-p, --print', 'print output to stdout').option('-m, --minimize', 'minimize output (ugly, smaller file)').option('-c, --coffee', 'output to CoffeeScript (not JS)').option('-b, --bundle_path [path]', 'bundle_path (instead of "/") for templates').option('-n, --no_headers', 'exclude boilerplate toffee (requires toffee.js included separately)').parse(process.argv);
+ program.version(getVersionNumber()).option('-o, --output [path]', 'file (bundles all output into a single .js)').option('-d, --output_dir [path]', 'compiles templates into parallel .js files').option('-p, --print', 'print to stdout').option('-m, --minimize', 'minimize output (ugly, smaller file(s))').option('-c, --coffee', 'output to CoffeeScript (not JS)').option('-b, --bundle_path [path]', 'bundle_path (instead of "/") for templates').option('-n, --no_headers', 'exclude boilerplate toffee (requires toffee.js included separately)').parse(process.argv);
compile = function(start_path, full_path) {
/*
@@ -53,11 +55,11 @@
process.stderr.write(v.error.getPrettyPrintText());
process.exit(1);
}
- return output;
+ return [output, bundle_path];
};
recurseRun = function(start_path, curr_path, out_text) {
- var file, files, stats, sub_path, sub_stats, _i, _len;
+ var comp, file, file_out_path, files, stats, sub_path, sub_stats, _i, _len;
stats = fs.statSync(curr_path);
if (stats.isDirectory()) {
files = fs.readdirSync(curr_path);
@@ -74,13 +76,38 @@
}
}
} else {
- out_text += "\n;\n" + compile(start_path, curr_path);
+ comp = compile(start_path, curr_path);
+ out_text += "\n;\n" + comp[0];
+ if (program.output_dir) {
+ file_out_path = path.normalize("" + program.output_dir + "/" + comp[1]);
+ file_out_path = "" + (path.dirname(file_out_path)) + "/" + (path.basename(file_out_path, '.toffee'));
+ file_out_path += program.coffee ? '.coffee' : '.js';
+ if (!program.print) {
+ console.log("Outputting " + file_out_path);
+ }
+ mkdirp.sync(path.dirname(file_out_path));
+ fs.writeFileSync(file_out_path, maybeAttachHeaders(comp[0]), "utf8");
+ }
}
return out_text;
};
+ maybeAttachHeaders = function(pre_output) {
+ var header_out;
+ if (program.no_headers) {
+ return pre_output;
+ } else {
+ header_out = getCommonHeadersJs(true, true);
+ if (program.coffee) {
+ return "`" + header_out + "`\n" + template_out;
+ } else {
+ return "" + header_out + template_out;
+ }
+ }
+ };
+
run = exports.run = function() {
- var header_out, out_text, start_path, template_out;
+ var out_text, start_path, template_out;
if (program.args.length !== 1) {
console.log("Unexpected input. toffee --help for examples");
console.log(program.args);
@@ -92,18 +119,17 @@
console.log("Input file/path not found. toffee --help for examples");
process.exit(1);
}
+ if (program.output_dir) {
+ try {
+ mkdirp.sync(program.output_dir);
+ } catch (e) {
+ console.log("Couldn't make/use " + program.output_dir + "; " + e);
+ process.exit(1);
+ }
+ }
start_path = path.normalize(start_path);
template_out = recurseRun(start_path, start_path, '');
- if (program.no_headers) {
- header_out = "";
- } else {
- header_out = getCommonHeadersJs(true, true);
- }
- if (program.coffee) {
- out_text = "`" + header_out + "`\n" + template_out;
- } else {
- out_text = "" + header_out + template_out;
- }
+ out_text = maybeAttachHeaders(template_out);
if (program.print) {
console.log(out_text);
}
View
9 package.json
@@ -10,8 +10,13 @@
"bin": "./bin/toffee",
"dependencies": {
"coffee-script": "*",
- "commander": "*",
- "uglify-js": "*"
+ "commander": "*",
+ "uglify-js": "*"
+ },
+ "devDependencies": {
+ "jison" : "*",
+ "mkdirp": "*",
+ "commander": "*"
},
"repository": {
"type": "git",
View
53 src/command_line.coffee
@@ -2,6 +2,7 @@ fs = require 'fs'
path = require 'path'
{view, getCommonHeadersJs} = require '../lib/view'
program = require 'commander'
+mkdirp = require 'mkdirp'
# -----------------------------------------------------------------------------
@@ -33,9 +34,10 @@ program.on '--help', ->
"
program.version(getVersionNumber())
- .option('-o, --output [path]', 'output to single file')
- .option('-p, --print', 'print output to stdout')
- .option('-m, --minimize', 'minimize output (ugly, smaller file)')
+ .option('-o, --output [path]', 'file (bundles all output into a single .js)')
+ .option('-d, --output_dir [path]', 'compiles templates into parallel .js files')
+ .option('-p, --print', 'print to stdout')
+ .option('-m, --minimize', 'minimize output (ugly, smaller file(s))')
.option('-c, --coffee', 'output to CoffeeScript (not JS)')
.option('-b, --bundle_path [path]', 'bundle_path (instead of "/") for templates')
.option('-n, --no_headers', 'exclude boilerplate toffee (requires toffee.js included separately)')
@@ -57,7 +59,6 @@ compile = (start_path, full_path) ->
if program.bundle_path
bundle_path = path.normalize "#{program.bundle_path}/#{bundle_path}"
-
v = new view source,
fileName: full_path
bundlePath: bundle_path
@@ -70,7 +71,8 @@ compile = (start_path, full_path) ->
if v.error
process.stderr.write v.error.getPrettyPrintText()
process.exit 1
- output
+
+ [output, bundle_path]
# -----------------------------------------------------------------------------
@@ -87,12 +89,33 @@ recurseRun = (start_path, curr_path, out_text) ->
if sub_stats.isDirectory()
out_text = recurseRun start_path, sub_path, out_text
else
- out_text += "\n;\n" + compile start_path, curr_path
+ comp = compile start_path, curr_path
+ out_text += "\n;\n" + comp[0]
+ if program.output_dir
+ file_out_path = path.normalize "#{program.output_dir}/#{comp[1]}"
+ file_out_path = "#{path.dirname file_out_path}/#{path.basename file_out_path, '.toffee'}"
+ file_out_path += if program.coffee then '.coffee' else '.js'
+ if not program.print
+ console.log "Outputting #{file_out_path}"
+ mkdirp.sync path.dirname file_out_path
+ fs.writeFileSync file_out_path, maybeAttachHeaders(comp[0]), "utf8"
return out_text
# -----------------------------------------------------------------------------
+maybeAttachHeaders = (pre_output) ->
+ if program.no_headers
+ return pre_output
+ else
+ header_out = getCommonHeadersJs true, true
+ if program.coffee
+ return "`#{header_out}`\n#{template_out}"
+ else
+ return "#{header_out}#{template_out}"
+
+# -----------------------------------------------------------------------------
+
run = exports.run = ->
if program.args.length isnt 1
@@ -105,19 +128,21 @@ run = exports.run = ->
catch e
console.log "Input file/path not found. toffee --help for examples"
process.exit 1
+
+ if program.output_dir
+ try
+ mkdirp.sync program.output_dir
+ catch e
+ console.log "Couldn't make/use #{program.output_dir}; #{e}"
+ process.exit 1
+
start_path = path.normalize start_path
template_out = recurseRun start_path, start_path, ''
- if program.no_headers
- header_out = ""
- else
- header_out = getCommonHeadersJs true, true
- if program.coffee
- out_text = "`#{header_out}`\n#{template_out}"
- else
- out_text = "#{header_out}#{template_out}"
+ out_text = maybeAttachHeaders template_out
if program.print
console.log out_text
+
if program.output
try
console.log "Writing #{program.output}"

0 comments on commit 3d9ac8c

Please sign in to comment.