-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.js
executable file
·66 lines (60 loc) · 2.56 KB
/
main.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
60
61
62
63
64
65
66
#!/usr/bin/env node
var program = require('commander');
var server = require('./server/server');
var project = require('./server/project');
var exec = require('child_process').exec
var path = require('path')
var fs = require('fs')
var packageJSON = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf-8'))
var checkForDependencies = function(callback) {
exec('which npm', function(err, stdout, stderr) {
if (err) {
console.error('Could not find `npm` command. Is npm installed?')
process.exit(-1)
} else {
callback()
}
})
}
program
.version(packageJSON.version)
.option('-H, --host <ip_address>', 'only accept traffic directed to a specific ip')
.option('-p, --port <number>', 'use a custom http port')
.option('-u, --username <username>', 'require a username for authentication')
.option('-P, --password <password>', 'require a password for authentication')
.option('-d, --no-downgrade', 'do not downgrade, force run as root (must already be root)')
.option('-b, --no-browser', 'do not attempt to launch the default browser')
program
.command('init [directory]')
.description('Initialize a new project and listen for connections.')
.action(function(dir){checkForDependencies(function(){
// Work around name collision caused by "password" function provided by commander
var password = program.password instanceof Function ? undefined : program.password
if (dir && !path.existsSync(dir)) {
console.log('Created `' + dir + '` directory.')
fs.mkdirSync(dir)
}
project.chdir(dir)
project.init()
project.start()
server.listen(program.port || process.env.PORT || 8123, program.host, program.username, password, program.downgrade, program.browser)
})})
program
.command('listen [directory]')
.description('Listen for connections.')
.action(function(dir){checkForDependencies(function(){
// Work around name collision caused by "password" function provided by commander
var password = program.password instanceof Function ? undefined : program.password
project.chdir(dir)
project.start()
server.listen(program.port || process.env.PORT || 8123, program.host, program.username, password, program.downgrade, program.browser)
})})
if (process.argv.length > 2) {
if (process.argv[2].charAt(0) == '-') {
process.argv.splice(2, 0, 'listen')
}
program.parse(process.argv);
} else {
process.argv.push('listen');
program.parse(process.argv);
}