@travetto/cli 3.0.0-rc.1 → 3.0.0-rc.4

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/src/util.ts DELETED
@@ -1,146 +0,0 @@
1
- import * as timers from 'timers/promises';
2
- import * as readline from 'readline';
3
- import { Writable } from 'stream';
4
-
5
- import { CompletionConfig } from './types';
6
-
7
- /**
8
- * Common CLI Utilities
9
- */
10
- export class CliUtil {
11
-
12
- static #waitState = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'.split('');
13
-
14
- static isBoolean(x: string): boolean {
15
- return /^(1|0|yes|no|on|off|auto|true|false)$/i.test(x);
16
- }
17
-
18
- static toBool(x: string | boolean, def: boolean): boolean;
19
- static toBool(x?: string | boolean, def?: boolean): boolean | undefined;
20
- static toBool(x?: string | boolean, def?: boolean): boolean | undefined {
21
- return x === undefined ? def :
22
- (typeof x === 'boolean' ? x :
23
- (this.isBoolean(x) ? /^(1|yes|on|true)$/i.test(x) :
24
- def));
25
- }
26
-
27
- static toInt(l: number | undefined, u: number | undefined, v: string, d: number): number {
28
- let n = +v;
29
- if (l === undefined && u === undefined) {
30
- return n;
31
- }
32
- if (l !== undefined && n < l) {
33
- n = d;
34
- }
35
- if (u !== undefined && n > u) {
36
- n = d;
37
- }
38
- return n;
39
- }
40
-
41
- /**
42
- * Get code completion values
43
- */
44
- static async getCompletion(cfg: CompletionConfig, args: string[]): Promise<string[]> {
45
- args = args.slice(0); // Copy as we mutate
46
-
47
- const cmd = args.shift()!;
48
-
49
- let last = cmd;
50
- let opts: string[] = [];
51
-
52
- // List all commands
53
- if (!cfg.task[cmd]) {
54
- opts = cfg.all;
55
- } else {
56
- // Look available sub commands
57
- last = args.pop() ?? '';
58
- const second = args.pop() ?? '';
59
- let flag = '';
60
-
61
- if (last in cfg.task[cmd]) {
62
- flag = last;
63
- last = '';
64
- } else if (second in cfg.task[cmd]) {
65
- // Look for available flags
66
- if (cfg.task[cmd][second].includes(last)) {
67
- flag = '';
68
- last = '';
69
- } else {
70
- flag = second;
71
- }
72
- }
73
- opts = cfg.task[cmd][flag];
74
- }
75
-
76
- return last ? opts.filter(x => x.startsWith(last)) : opts.filter(x => !x.startsWith('-'));
77
- }
78
-
79
- /**
80
- * Rewrite a single line in the stream
81
- * @param stream The stream to write
82
- * @param text Text, if desired
83
- * @param clear Should the entire line be cleared?
84
- */
85
- static async rewriteLine(stream: Writable, text?: string, clear = false): Promise<void> {
86
- await new Promise<void>(r => readline.cursorTo(stream, 0, undefined, () => {
87
- if (clear) {
88
- readline.clearLine(stream, 0);
89
- }
90
- if (text) {
91
- stream.write(text);
92
- readline.moveCursor(stream, 1, 0);
93
- }
94
- r();
95
- }));
96
- }
97
-
98
- /**
99
- * Waiting message with a callback to end
100
- *
101
- * @param message Message to share
102
- * @param delay Delay duration
103
- */
104
- static async waiting<T>(message: string, work: Promise<T> | (() => Promise<T>),
105
- config: { completion?: string, delay?: number, stream?: Writable } = {}
106
- ): Promise<T> {
107
- const { stream, delay, completion } = { delay: 1000, stream: process.stderr, ...config };
108
-
109
- const writeLine = this.rewriteLine.bind(this, stream);
110
-
111
- if (!('then' in work)) {
112
- work = work();
113
- }
114
-
115
- if (!process.stdout.isTTY) {
116
- return work; // Dip early
117
- }
118
-
119
- let i = -1;
120
- let done = false;
121
- let value: T | undefined;
122
- let capturedError: Error | undefined;
123
- const final = work
124
- .then(res => value = res)
125
- .catch(err => capturedError = err)
126
- .finally(() => done = true);
127
-
128
- if (delay) {
129
- await Promise.race([timers.setTimeout(delay), final]);
130
- }
131
-
132
- while (!done) {
133
- await writeLine(`${this.#waitState[i = (i + 1) % this.#waitState.length]} ${message}`);
134
- await timers.setTimeout(50);
135
- }
136
-
137
- if (i >= 0) {
138
- await writeLine(completion ? `${message} ${completion}\n` : '', true);
139
- }
140
- if (capturedError) {
141
- throw capturedError;
142
- } else {
143
- return value!;
144
- }
145
- }
146
- }