-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathzenbrain.js
74 lines (73 loc) · 2.27 KB
/
zenbrain.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
67
68
69
70
71
72
73
74
var motley = require('motley')
var commander = require('commander')
var path = require('path')
module.exports = function zenbrain (p, app_name) {
p = path.resolve(p)
return {
get_version: function () {
try {
return require(path.join(p, 'package.json')).version
}
catch (e) {
return '0.0.0'
}
},
get_config: function () {
var config = require('./config.js')
try {
var more_config = require(path.join(p, 'config.js'))
Object.keys(more_config).forEach(function (k) {
config[k] = more_config[k]
})
}
catch (e) {
// ignore
}
return config
},
get_codemaps: function () {
return [require('./_codemap'), require(path.join(p, '_codemap'))]
},
make_app: function () {
return motley({
_ns: 'zenbrain',
_maps: this.get_codemaps(),
root: p,
app_name: app_name,
config: this.get_config()
})
},
cli: function () {
var app = this.make_app()
app.set('zenbrain:app', app)
var launcher = app.get('zenbrain:launcher')
var program = require('commander')
.version(this.get_version())
.option('--config <path>', 'specify a path for config.js overrides')
.option('--rs <suffix>', 'suffix to give the run_state.id')
program._name = 'zenbot'
app.set('zenbrain:program', program)
var command = process.argv[2]
app.set('zenbrain:command', command || null)
var cmds = app.get('zenbrain:commands').map(function (command) {
var cmd = program
.command(command.spec || command.name)
.description(command.description)
;(command.options || []).forEach(function (option) {
cmd = cmd.option(option.spec || ('--' + option.name), option.description, option.number ? Number : String, option.default)
})
var action = app.get('zenbrain:actions.' + (command.action || command.name))
cmd.action(launcher(action))
if (command.name === 'launch') {
cmd.allowUnknownOption(true)
}
return command.name
})
if (!command || cmds.indexOf(command) === -1) {
program.outputHelp()
process.exit(1)
}
program.parse(process.argv)
}
}
}