forked from e2ebridge/e2e-bridge-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·98 lines (80 loc) · 2.32 KB
/
cli.js
File metadata and controls
executable file
·98 lines (80 loc) · 2.32 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
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
'use strict';
const app = require('./main');
const lib = require('./lib/lib');
const prompts = require('prompts');
/**
* @implements IIOInterface
*/
class IOInterface {
out(str) {
return process.stdout.write(str);
}
error(str) {
return process.stderr.write(str);
}
async prompt(questions) {
return await prompts(questions);
}
}
const ioInterface = new IOInterface();
/**
* Displays usage help.
* @param {?string=} message Additional message to display
*/
function showHelp(message) {
ioInterface.out(lib.helpText(message));
}
function reportErrors(errors) {
if((errors || []).some(e => {
ioInterface.error(`${e.level}: ${e.message}\n`);
return e.level === 'error';
})) {
process.exit(1);
}
}
const cliParseSettings = require('./minimist.json');
const argErrors = [];
cliParseSettings['unknown'] = function unknownArg(arg) {
if(/^(-|--)/.test(arg)) {
argErrors.push({
level: 'error',
message: `${arg} not understood`
});
return false; // don't add to parsed args
} else {
return true;
}
};
const argv = require('minimist')(process.argv.slice(2), cliParseSettings);
if(argv['help']) {
showHelp();
process.exit(0);
}
reportErrors(argErrors);
const positionalArguments = argv._;
delete argv._;
if(positionalArguments.length < 1) {
showHelp('Incorrect number of arguments');
process.exit(1);
}
let operation = String.prototype.toLowerCase.call(positionalArguments.shift());
if(!app.isKnownOperation(operation)) {
showHelp(`Unknown operation: ${operation}`);
process.exit(1);
}
let {errors: settingErrors, settings} = app.createSettings(operation, argv, positionalArguments);
reportErrors(settingErrors);
operation = settings['operation']; // some operations map to different operation with parameter
app.main(settings, ioInterface)
.catch(reason => {
ioInterface.out(settings.statusFormatter('error', settings) + '\n');
ioInterface.out(settings.errorFormatter(reason, settings) + '\n');
process.exit(2);
}).then(result => {
ioInterface.out(settings.statusFormatter('success', settings) + '\n');
if(result) {
ioInterface.out(settings.responseFormatter(result, settings) + '\n');
}
process.exit(0);
});