forked from felixSchl/neodoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (31 loc) · 844 Bytes
/
index.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
var neodocLib = require('./lib.js')
module.exports.run = function (helpOrSpec, options = {}) {
options.argv =
(Array.isArray(options.argv)
? options.argv
: (process.argv || [])
).slice(2)
options.env = Object.entries(
options.env && typeof options.env == 'object'
? options.env
: (process.env || {})
)
var result
if (helpOrSpec) {
if (typeof helpOrSpec === 'string') {
result = neodocLib.runStringJs(helpOrSpec)(options)
}
else if (typeof helpOrSpec === 'object') {
result = neodocLib.runSpecJs(helpOrSpec)(options)
}
}
if (!result ) {
throw new Error('Either provide a help text or a command specification')
}
if (result.errors) {
throw new Error(result.errors)
}
return Array.isArray(result)
? Object.fromEntries(result)
: result
}