-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·85 lines (75 loc) · 2.17 KB
/
cli.js
File metadata and controls
executable file
·85 lines (75 loc) · 2.17 KB
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
78
79
80
81
82
83
84
85
#!/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'
// colored terminal output
const bb = chalk.blackBright
const ma = chalk.magenta
const rb = chalk.redBright
const yl = chalk.yellow
/**
* 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(`
${bb('Install command completions for ZSH and BASH')}
${scriptName} completion`)
}
function showExamples (scriptName) {
console.log(`
${bb('Examples:')}
${scriptName} run 'count(//p)'
${scriptName} list --tree --depth 1 /db/apps
${scriptName} package install from-registry demo-apps
`)
}
function showLogo () {
console.log('\n' +
ma` ╲ ╱ ╓─── ──┰──` + '\n' +
rb` ╳ ╰───╮ │ ` + '\n' +
yl` ╱ ╲ ▂▁▁▁│ ┇ ` + '\n'
)
console.log(yl`A modern command line interface for exist-db`)
}
const parser = yargs(hideBin(process.argv))
.config('config', 'Read configuration file', configure)
.middleware(readConnection)
.usageConfiguration({ 'hide-types': true })
.completion('completion', false)
.strictCommands(true)
.strictOptions(false)
.help()
.command('$0 [<command>]', 'Interact with an eXist-db', () => {}, async (argv) => {
if (argv.command) {
console.error(`Command "${argv.command}" not recognized.
Try xst --help`)
}
showLogo()
const scriptName = getScriptName(argv.$0)
showCompletionHelp(scriptName)
// append examples
showExamples(scriptName)
})
.command(commands)
// .recommendCommands()
.fail(false)
parser.wrap(parser.terminalWidth())
try {
await parser.parse()
} catch (error) {
handleError(error)
parser.getHelp()
process.exit(1)
}