This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c952f2
commit a324563
Showing
2 changed files
with
240 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |