@miso.ai/server-commons 0.6.0-beta.0 → 0.6.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/index.js +1 -0
- package/src/rate-limiting-queue.js +3 -7
- package/src/resolution.js +1 -1
- package/src/stream/output.js +2 -1
- package/src/yargs.js +81 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export * as sink from './sink/index.js';
|
|
|
9
9
|
export * as buffer from './buffer/index.js';
|
|
10
10
|
export * as stream from './stream/index.js';
|
|
11
11
|
export * as log from './log/index.js';
|
|
12
|
+
export * as yargs from './yargs.js';
|
|
12
13
|
|
|
13
14
|
export { default as Resolution } from './resolution.js';
|
|
14
15
|
export { default as TaskQueue } from './task-queue.js';
|
package/src/resolution.js
CHANGED
package/src/stream/output.js
CHANGED
|
@@ -17,6 +17,7 @@ export default class OutputStream extends Writable {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
_write(record, _, next) {
|
|
20
|
+
//console.error(record);
|
|
20
21
|
this._out.write(this._format(record) + '\n');
|
|
21
22
|
next();
|
|
22
23
|
}
|
|
@@ -24,7 +25,7 @@ export default class OutputStream extends Writable {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
function defaultObjectModeFormat(v) {
|
|
27
|
-
typeof v === 'object' ? JSON.stringify(v) : `${v}`;
|
|
28
|
+
return typeof v === 'object' ? JSON.stringify(v) : `${v}`;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
function defaultNonObjectModeFormat(v) {
|
package/src/yargs.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
import _yargs from 'yargs/yargs';
|
|
3
|
+
import { hideBin } from 'yargs/helpers';
|
|
4
|
+
|
|
5
|
+
function hasDefaultCommand(args) {
|
|
6
|
+
let args0 = args[0];
|
|
7
|
+
const command = typeof args0 === 'object' ? args0.command : typeof args0 === 'string' ? args0 : undefined;
|
|
8
|
+
if (!command) {
|
|
9
|
+
return false; // this should not happen?
|
|
10
|
+
}
|
|
11
|
+
for (const c of Array.isArray(command) ? command : [command]) {
|
|
12
|
+
if (c.startsWith('*') || c.startsWith('$0')) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class Tracker {
|
|
20
|
+
|
|
21
|
+
constructor() {}
|
|
22
|
+
|
|
23
|
+
get hasDefaultCommand() {
|
|
24
|
+
return !!this._hasDefaultCommand;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
shim(yargs) {
|
|
28
|
+
const _command = yargs.command;
|
|
29
|
+
yargs.command = (...args) => {
|
|
30
|
+
if (hasDefaultCommand(args)) {
|
|
31
|
+
this._hasDefaultCommand = true;
|
|
32
|
+
}
|
|
33
|
+
return _command.apply(yargs, args);
|
|
34
|
+
};
|
|
35
|
+
return yargs;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function build(fn) {
|
|
41
|
+
handleEpipe();
|
|
42
|
+
const yargs = _yargs(hideBin(process.argv));
|
|
43
|
+
|
|
44
|
+
const tracker = new Tracker();
|
|
45
|
+
tracker.shim(yargs);
|
|
46
|
+
|
|
47
|
+
fn(yargs);
|
|
48
|
+
|
|
49
|
+
if (!tracker.hasDefaultCommand) {
|
|
50
|
+
yargs.command('*', '', () => {}, () => yargs.showHelp())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return yargs
|
|
54
|
+
.help()
|
|
55
|
+
.fail(handleFail)
|
|
56
|
+
.parse();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let _epipeHandled = false;
|
|
60
|
+
|
|
61
|
+
export function handleEpipe() {
|
|
62
|
+
if (_epipeHandled) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
_epipeHandled = true;
|
|
66
|
+
process.stdout.on('error', err => err.code == 'EPIPE' && process.exit(0));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function handleFail(msg, err) {
|
|
70
|
+
if (err) {
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
console.error(msg);
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function coerceToArray(arg) {
|
|
78
|
+
return Array.isArray(arg) ? arg :
|
|
79
|
+
typeof arg === 'string' ? arg.split(',') :
|
|
80
|
+
arg === undefined || arg === null ? [] : [arg];
|
|
81
|
+
}
|