-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·77 lines (68 loc) · 1.95 KB
/
cli.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
75
76
77
#!/usr/bin/env node
import yargs from 'yargs'
import chalk from 'chalk'
import { commands } from './commands/index.js'
import { handleError } from './utility/errors.js'
import { hideBin } from 'yargs/helpers'
import { readConnection } from './utility/connection.js'
import { configure } from './utility/configure.js'
const dimWhite = chalk.white.dim
const brightYellow = chalk.yellowBright
/**
* if package was linked to global strip relative path from output
* @param {String} $0 raw value of argv.$0
* @returns {String} script name with relative path stripped if linked
*/
function getScriptName ($0) {
if ($0.startsWith('.')) {
return $0.substring($0.lastIndexOf('/') + 1)
}
return $0
}
function showCompletionHelp (scriptName) {
console.log(`
${dimWhite('Install command completions for ZSH and BASH')}
${scriptName} completion`)
}
function showExamples (scriptName) {
console.log(`
${dimWhite('Examples:')}
${scriptName} run 'count(//p)'
${scriptName} list --tree --depth 1 /db/apps
${scriptName} package install ./my-package.xar
`)
}
function showLogo () {
console.log(brightYellow(`
╲ ╱ ╓─── ──┰──
╳ ╰───╮ │
╱ ╲ ▂▁▁▁│ ┇
`))
console.log('A modern exist-db command line interface')
}
const parser = yargs(hideBin(process.argv))
.config('config', 'Read configuration file', configure)
.middleware(readConnection)
.command('$0 [<command>]', 'Interact with an eXist-db', () => {}, async (argv) => {
showLogo()
const scriptName = getScriptName(argv.$0)
showCompletionHelp(scriptName)
// append examples
showExamples(scriptName)
})
.help()
.completion('completion', false)
.command(commands)
.demandCommand(1)
.usageConfiguration({ 'hide-types': true })
// .recommendCommands()
.strict(false)
.fail(false)
parser.wrap(parser.terminalWidth())
try {
await parser.parse()
} catch (error) {
handleError(error)
parser.getHelp()
process.exit(1)
}