forked from malgorithms/toffee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_line.coffee
158 lines (132 loc) · 5.03 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
fs = require 'fs'
path = require 'path'
{view, getCommonHeadersJs} = require '../lib/view'
program = require 'commander'
mkdirp = require 'mkdirp'
# -----------------------------------------------------------------------------
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]', '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 = (start_path, full_path) ->
###
e.g., if start_path is /foo/bar
and path is /foo/bar/car/thing.toffee
###
source = fs.readFileSync full_path, 'utf8'
bundle_path = full_path[start_path.length...]
if start_path is full_path
bundle_path = "/" + path.basename 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
browserMode: true
minimize: program.minimize? and program.minimize
if program.coffee
output = v.toCoffee()
else
output = v.toJavaScript()
if v.error
process.stderr.write v.error.getPrettyPrintText()
process.exit 1
[output, bundle_path]
# -----------------------------------------------------------------------------
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
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, true
if program.coffee
return "`#{header_out}`\n\n#{pre_output}"
else
return "#{header_out}\n;\n#{pre_output}"
# -----------------------------------------------------------------------------
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
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, ''
out_text = maybeAttachHeaders template_out
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()