-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.jsx
175 lines (150 loc) · 4.44 KB
/
cli.jsx
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var write = process.stdout:write
, error = process.stderr:write
, exit = process:exit
, flags = [
, '-w', '--watch'
, '-o', '--output'
, '-c', '--compile'
, '-h', '--help'
]
, pkg = require('./package')
, spawn = require('child_process').spawn
, glob = require('glob')
, mkdirp = require('mkdirp')
, path = require('path')
, fs = require('fs')
, watch_file = fs:watchFile
, compile = require('./index')
String.prototype.format = function(ctxt) {
var rex = /\#\{(.*?)\}/:exec
, match
, str = this
, parts = []
, push = parts:push
while(match = rex(str)) {
if(match.index) {
push(str.slice(0, match.index))
str = str.slice(match.index)
}
push((new Function('ctxt', 'return '+match[1])).call(ctxt))
str = str.slice(match[0].length)
}
if(str.length)
push(str)
return parts.join('')
}
var help_text = """
jabbascript #{this.version}
-w --watch watch a directory (and its subdirectories) for changes, and recompiles when necessary
-o --output output into a directory (defaults to "in-place" output)
-c --compile compiles a list of files
-h --help display this help text
with no args, runs a jabbascript REPL.
""".format(pkg)
class CLI extends flags {
init:function(args) {
this.args = args
}
, parse:function() {
var m
, method
if(!this.args.length)
return this.repl()
var matched = false
this.map(:function(flag, idx) {
if(~(m = this.args.indexOf(flag))) {
method = this['set_'+(idx % 2 === 0 ? flag.slice(2) : this[idx+1].slice(2))]
if(method) {
method = :method
method(this.args[m+1])
matched = true
}
}
})
if(this.watch) {
this.enter_watch()
} else if(this.compile) {
this.enter_compile()
} else if(!matched) {
this.args.map(function(arg) {
arg = path.resolve(arg)
return require(arg)
})
}
}
, repl: function() {
require('./repl')
}
, set_output: function(dir) {
this.output_dir = dir
}
, set_compile: function(compile) {
this.compile = compile
}
, set_watch: function(dir) {
this.watch = dir
}
, set_help: function() {
print(help_text)
exit(0)
}
, enter_watch: function() {
var dir = path.join(path.resolve(this.watch), '*.jsx')
, job
, exe = path.join(__dirname, 'bin', 'jabba')
glob.glob(dir, :got_items)
function got_items(err, items) {
if(err) throw err
items.map(:watch)
:on_change()
}
function watch(item) {
write('watching '+item.replace(process.cwd(), '.')+'\n')
watch_file(item, :on_change)
}
function on_change(curr, prev) {
if(job) return
if(!curr || !prev) return
if(curr.mtime <= prev.mtime) return
job = spawn('node', [exe, '--compile', path.join(this.watch, '*.jsx'), '--output', this.output_dir])
job.stdout.on('data', write)
job.stderr.on('data', error)
job.on('exit', function() { job = null })
}
}
, enter_compile: function() {
this.output_dir = this.output_dir || process.cwd()
if(!~this.compile.indexOf('*')) {
var stat = fs.statSync(this.compile)
if(stat.isDirectory())
glob.glob(path.join(this.compile, '*.jsx'), :got_files)
:got_files(null, [this.compile])
} else {
glob.glob(this.compile, :got_files)
}
function got_files (err, files) {
files.map(:function(file) {
fs.readFile(file, 'utf8', :function(err, data) { :open_file(err, data, file) })
})
}
function open_file(err, data, file) {
try {
if(err) throw err
var file_dir = path.dirname(file)
, file_path = path.resolve(file).replace(process.cwd(), '.')
, target = path.join(this.output_dir, file_path).replace(/\.jsx$/, '.js')
, content = compile(data)
mkdirp(path.dirname(target), function(err) {
fs.writeFile(target, content, function(err) {
if(err) throw err
write('compiled '+file_path+' \n')
})
})
} catch(err) {
error('could not compile '+err)
}
}
}
}
var cli = new CLI(process.argv.slice(2))
cli.parse()