Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
read.js88 linesDownload Raw Back to esm
1import Mute from 'mute-stream';
2import { createInterface } from 'readline';
3export async function read({ default: def, input = process.stdin, output = process.stdout, completer, prompt = '', silent, timeout, edit, terminal, replace, history, }) {
4    if (typeof def !== 'undefined' &&
5        typeof def !== 'string' &&
6        typeof def !== 'number') {
7        throw new Error('default value must be string or number');
8    }
9    let editDef = false;
10    const defString = def?.toString();
11    prompt = prompt.trim() + ' ';
12    terminal = !!(terminal || output.isTTY);
13    if (defString) {
14        if (silent) {
15            prompt += '(<default hidden>) ';
16            // TODO: add tests for edit
17            /* c8 ignore start */
18        }
19        else if (edit) {
20            editDef = true;
21            /* c8 ignore stop */
22        }
23        else {
24            prompt += '(' + defString + ') ';
25        }
26    }
27    const m = new Mute({ replace, prompt });
28    m.pipe(output, { end: false });
29    output = m;
30    return new Promise((resolve, reject) => {
31        const rl = createInterface({ input, output, terminal, completer, history });
32        // TODO: add tests for timeout
33        /* c8 ignore start */
34        const timer = timeout && setTimeout(() => onError(new Error('timed out')), timeout);
35        /* c8 ignore stop */
36        m.unmute();
37        rl.setPrompt(prompt);
38        rl.prompt();
39        if (silent) {
40            m.mute();
41            // TODO: add tests for edit + default
42            /* c8 ignore start */
43        }
44        else if (editDef && defString) {
45            const rlEdit = rl;
46            rlEdit.line = defString;
47            rlEdit.cursor = defString.length;
48            rlEdit._refreshLine();
49        }
50        /* c8 ignore stop */
51        const done = () => {
52            rl.close();
53            clearTimeout(timer);
54            m.mute();
55            m.end();
56        };
57        // TODO: add tests for rejecting
58        /* c8 ignore start */
59        const onError = (er) => {
60            done();
61            reject(er);
62        };
63        /* c8 ignore stop */
64        rl.on('error', onError);
65        rl.on('line', line => {
66            // TODO: add tests for silent
67            /* c8 ignore start */
68            if (silent && terminal) {
69                m.unmute();
70            }
71            /* c8 ignore stop */
72            done();
73            // TODO: add tests for default
74            /* c8 ignore start */
75            // truncate the \n at the end.
76            return resolve(line.replace(/\r?\n?$/, '') || defString || '');
77            /* c8 ignore stop */
78        });
79        // TODO: add tests for sigint
80        /* c8 ignore start */
81        rl.on('SIGINT', () => {
82            rl.close();
83            onError(new Error('canceled'));
84        });
85        /* c8 ignore stop */
86    });
87}
88//# sourceMappingURL=read.js.map
codekingpro/portable-devtools · Team Ai