Team Ai
Datasetpublic

codekingpro/portable-devtools

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