@miso.ai/server-commons 0.6.2-beta.0 → 0.6.3-beta.0
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 +1 -1
- package/src/object.js +12 -0
- package/src/stream/misc.js +11 -2
- package/src/yargs.js +22 -25
package/package.json
CHANGED
package/src/object.js
CHANGED
|
@@ -43,6 +43,18 @@ export function asNumber(value) {
|
|
|
43
43
|
return isNaN(value) ? undefined : value;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Get value from a Map if available, compute and set it otherwise.
|
|
48
|
+
*/
|
|
49
|
+
export function computeIfAbsent(map, key, fn) {
|
|
50
|
+
if (map.has(key)) {
|
|
51
|
+
return map.get(key);
|
|
52
|
+
}
|
|
53
|
+
const value = fn(key);
|
|
54
|
+
map.set(key, value);
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
46
58
|
/**
|
|
47
59
|
* Assign values on target object with Object.defineProperties() from source object.
|
|
48
60
|
*/
|
package/src/stream/misc.js
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import { Transform, pipeline as _pipeline } from 'stream';
|
|
2
2
|
|
|
3
|
-
export function parse() {
|
|
3
|
+
export function parse({ lenient } = {}) {
|
|
4
|
+
const parsnFn = lenient ? parseJsonIfPossible : JSON.parse;
|
|
4
5
|
return new Transform({
|
|
5
6
|
transform(chunk, _, callback) {
|
|
6
|
-
callback(null,
|
|
7
|
+
callback(null, parsnFn(chunk));
|
|
7
8
|
},
|
|
8
9
|
readableObjectMode: true,
|
|
9
10
|
});
|
|
10
11
|
}
|
|
11
12
|
|
|
13
|
+
function parseJsonIfPossible(chunk) {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(chunk);
|
|
16
|
+
} catch(_) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
12
21
|
export function stringify() {
|
|
13
22
|
return new Transform({
|
|
14
23
|
transform(chunk, _, callback) {
|
package/src/yargs.js
CHANGED
|
@@ -2,7 +2,7 @@ import 'dotenv/config';
|
|
|
2
2
|
import _yargs from 'yargs/yargs';
|
|
3
3
|
import { hideBin } from 'yargs/helpers';
|
|
4
4
|
|
|
5
|
-
function
|
|
5
|
+
function isDefaultCommand(args) {
|
|
6
6
|
let args0 = args[0];
|
|
7
7
|
const command = typeof args0 === 'object' ? args0.command : typeof args0 === 'string' ? args0 : undefined;
|
|
8
8
|
if (!command) {
|
|
@@ -16,38 +16,35 @@ function hasDefaultCommand(args) {
|
|
|
16
16
|
return false;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return
|
|
19
|
+
function shim(yargs) {
|
|
20
|
+
let hasDefaultCommand = false;
|
|
21
|
+
// command(): track whether a default command is registered
|
|
22
|
+
const _command = yargs.command;
|
|
23
|
+
yargs.command = (...args) => {
|
|
24
|
+
if (isDefaultCommand(args)) {
|
|
25
|
+
hasDefaultCommand = true;
|
|
26
|
+
}
|
|
27
|
+
return _command.apply(yargs, args);
|
|
28
|
+
};
|
|
29
|
+
// get hasDefaultCommand
|
|
30
|
+
Object.defineProperty(yargs, 'hasDefaultCommand', {
|
|
31
|
+
get: () => hasDefaultCommand,
|
|
32
|
+
});
|
|
33
|
+
// showHelpOnZeroCommand()
|
|
34
|
+
yargs.showHelpOnZeroCommand = function() {
|
|
35
|
+
return this.command('*', false, () => {}, () => this.showHelp());
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
return yargs;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export function build(fn) {
|
|
41
41
|
handleEpipe();
|
|
42
|
-
const yargs = _yargs(hideBin(process.argv));
|
|
43
|
-
|
|
44
|
-
const tracker = new Tracker();
|
|
45
|
-
tracker.shim(yargs);
|
|
42
|
+
const yargs = shim(_yargs(hideBin(process.argv)));
|
|
46
43
|
|
|
47
44
|
fn(yargs);
|
|
48
45
|
|
|
49
|
-
if (!
|
|
50
|
-
yargs.
|
|
46
|
+
if (!yargs.hasDefaultCommand) {
|
|
47
|
+
yargs.showHelpOnZeroCommand();
|
|
51
48
|
}
|
|
52
49
|
|
|
53
50
|
return yargs
|