forked from marcuswestin/require
/
require-command.js
executable file
·60 lines (53 loc) · 1.33 KB
/
require-command.js
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
#!/usr/bin/env node
var path = require('path'),
sys = require('sys'),
server = require('../server'),
compiler = require('../compiler')
var opts = {
port: 1234,
host: 'localhost',
command: null,
path: null
}
var args = [].slice.call(process.argv, 2)
opts.command = args.shift()
opts.path = path.resolve(args.shift())
while (args.length) {
var arg = args.shift()
switch(arg) {
case '--port':
opts.port = args.shift()
break
case '--host':
opts.host = args.shift()
break
default:
console.log('Unknown option', arg)
process.exit(1)
break
}
}
switch (opts.command) {
case 'serve':
if (!opts.path) {
console.log('Specify a path to serve from, e.g. require serve ./example')
process.exit(1)
}
server.listen(opts.port, { host:opts.host, path:opts.path })
console.log('serving from', opts.path, 'on', 'http://'+opts.host+':'+opts.port)
break
case 'compile':
if (!opts.path) {
console.log('Specify a single file to compile, e.g. require compile ./path/to/file.js')
process.exit(1)
}
sys.print(compiler.compile(opts.path))
break
default:
if (opts.command) {
console.log('Unknown command', '"' + opts.command + '".', 'Try "require serve" or "require compile"')
} else {
console.log('You need to give a command, e.g. "require serve" or "require compile"')
}
process.exit(1)
}