-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·64 lines (51 loc) · 1.55 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
#!/usr/bin/env node
import inquire from 'inquirer';
import minimist from 'minimist';
import { readFile } from 'fs/promises';
import OA from './oa.js';
import help from './src/help.js';
const argv = minimist(process.argv, {
boolean: ['help', 'version', 'trace'],
string: ['url', 'username', 'password'],
alias: {
version: 'v',
help: '?'
}
});
runner(argv);
async function runner(argv) {
if (argv.version) {
const settings = JSON.parse(await readFile(new URL('./package.json', import.meta.url)));
console.log(`openaddresses/lib@${settings.version}`);
return;
}
const oa = new OA(argv);
if (argv.help || !argv._[2] || !argv._[3]) {
return help(argv, oa);
}
if (!argv.script) {
const res = await inquire.prompt([{
name: 'url',
message: 'URL to connect to local or remote OA instance. Be sure to include the protocol and port number for local instances, e.g. \'http://localhost:8000\'',
type: 'string',
required: 'true',
default: oa.url
}]);
oa.url = new URL(res.url).toString();
}
argv.cli = true;
try {
const res = await oa.cmd(argv._[2], argv._[3], argv);
if (res instanceof Buffer) {
process.stdout.write(res);
} else {
console.log(JSON.stringify(res, null, 4));
}
} catch (err) {
if (argv.trace) throw err;
console.error();
console.error(err.message);
console.error();
process.exit(1);
}
}