-
Notifications
You must be signed in to change notification settings - Fork 10
/
command_line.coffee
109 lines (94 loc) · 3.33 KB
/
command_line.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
fs = require 'fs'
path = require 'path'
{view, getCommonHeadersJs} = require '../lib/view'
program = require 'commander'
# -----------------------------------------------------------------------------
getVersionNumber = ->
p = fs.readFileSync "#{__dirname}/../package.json", "utf8"
o = JSON.parse p
o.version
# -----------------------------------------------------------------------------
program.on '--help', ->
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 file')
.option('-p, --print', 'print output to stdout')
.option('-m, --minimize', 'minimize output (ugly, smaller file)')
.option('-c, --coffee', 'output to CoffeeScript (not JS)')
.parse process.argv
# -----------------------------------------------------------------------------
compile = (start_path, path) ->
###
e.g., if start_path is /foo/bar
and path is /foo/bar/car/thing.toffee
###
source = fs.readFileSync path, 'utf8'
bundle_path = path[start_path.length...]
v = new view source,
fileName: path
bundlePath: bundle_path
browserMode: true
minimize: program.minimize? and program.minimize
js = v._toJavaScript()
if v.error
process.stderr.write v.error.getPrettyPrintText()
process.exit 1
js
# -----------------------------------------------------------------------------
recurseRun = (start_path, curr_path, out_text) ->
stats = fs.statSync curr_path
if stats.isDirectory()
files = fs.readdirSync curr_path
for file in files
sub_path = path.normalize "#{curr_path}/#{file}"
if file.match /\.toffee$/
out_text = recurseRun start_path, sub_path, out_text
else if not (file in ['.','..'])
sub_stats = fs.statSync sub_path
if sub_stats.isDirectory()
out_text = recurseRun start_path, sub_path, out_text
else
out_text += "\n;\n" + compile start_path, curr_path
return out_text
run = exports.run = ->
if program.args.length isnt 1
console.log "Unexpected input. toffee --help for examples"
console.log program.args
process.exit 1
else
try
start_path = fs.realpathSync program.args[0]
catch e
console.log "Input file/path not found. toffee --help for examples"
process.exit 1
start_path = path.normalize start_path
out_text = """#{getCommonHeadersJs true, true}\n#{recurseRun start_path, start_path, ''}"""
if program.print
console.log out_text
if program.output
try
console.log "Writing #{program.output}"
fs.writeFileSync program.output, out_text, "utf8"
catch e
console.log e
process.exit 1
# -----------------------------------------------------------------------------
if require.main is module
run()