Skip to content

Commit 6e5bfd9

Browse files
committed
1 parent 7f8e237 commit 6e5bfd9

File tree

23 files changed

+1079
-19
lines changed

23 files changed

+1079
-19
lines changed

node_modules/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
!/imurmurhash
8181
!/ini
8282
!/init-package-json
83+
!/init-package-json/node_modules/
84+
/init-package-json/node_modules/*
85+
!/init-package-json/node_modules/mute-stream
86+
!/init-package-json/node_modules/read
8387
!/ip-address
8488
!/ip-regex
8589
!/is-cidr
@@ -135,6 +139,10 @@
135139
!/promise-call-limit
136140
!/promise-retry
137141
!/promzard
142+
!/promzard/node_modules/
143+
/promzard/node_modules/*
144+
!/promzard/node_modules/mute-stream
145+
!/promzard/node_modules/read
138146
!/qrcode-terminal
139147
!/read-cmd-shim
140148
!/read
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The ISC License
2+
3+
Copyright (c) Isaac Z. Schlueter and Contributors
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15+
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
const Stream = require('stream')
2+
3+
class MuteStream extends Stream {
4+
#isTTY = null
5+
6+
constructor (opts = {}) {
7+
super(opts)
8+
this.writable = this.readable = true
9+
this.muted = false
10+
this.on('pipe', this._onpipe)
11+
this.replace = opts.replace
12+
13+
// For readline-type situations
14+
// This much at the start of a line being redrawn after a ctrl char
15+
// is seen (such as backspace) won't be redrawn as the replacement
16+
this._prompt = opts.prompt || null
17+
this._hadControl = false
18+
}
19+
20+
#destSrc (key, def) {
21+
if (this._dest) {
22+
return this._dest[key]
23+
}
24+
if (this._src) {
25+
return this._src[key]
26+
}
27+
return def
28+
}
29+
30+
#proxy (method, ...args) {
31+
if (typeof this._dest?.[method] === 'function') {
32+
this._dest[method](...args)
33+
}
34+
if (typeof this._src?.[method] === 'function') {
35+
this._src[method](...args)
36+
}
37+
}
38+
39+
get isTTY () {
40+
if (this.#isTTY !== null) {
41+
return this.#isTTY
42+
}
43+
return this.#destSrc('isTTY', false)
44+
}
45+
46+
// basically just get replace the getter/setter with a regular value
47+
set isTTY (val) {
48+
this.#isTTY = val
49+
}
50+
51+
get rows () {
52+
return this.#destSrc('rows')
53+
}
54+
55+
get columns () {
56+
return this.#destSrc('columns')
57+
}
58+
59+
mute () {
60+
this.muted = true
61+
}
62+
63+
unmute () {
64+
this.muted = false
65+
}
66+
67+
_onpipe (src) {
68+
this._src = src
69+
}
70+
71+
pipe (dest, options) {
72+
this._dest = dest
73+
return super.pipe(dest, options)
74+
}
75+
76+
pause () {
77+
if (this._src) {
78+
return this._src.pause()
79+
}
80+
}
81+
82+
resume () {
83+
if (this._src) {
84+
return this._src.resume()
85+
}
86+
}
87+
88+
write (c) {
89+
if (this.muted) {
90+
if (!this.replace) {
91+
return true
92+
}
93+
// eslint-disable-next-line no-control-regex
94+
if (c.match(/^\u001b/)) {
95+
if (c.indexOf(this._prompt) === 0) {
96+
c = c.slice(this._prompt.length)
97+
c = c.replace(/./g, this.replace)
98+
c = this._prompt + c
99+
}
100+
this._hadControl = true
101+
return this.emit('data', c)
102+
} else {
103+
if (this._prompt && this._hadControl &&
104+
c.indexOf(this._prompt) === 0) {
105+
this._hadControl = false
106+
this.emit('data', this._prompt)
107+
c = c.slice(this._prompt.length)
108+
}
109+
c = c.toString().replace(/./g, this.replace)
110+
}
111+
}
112+
this.emit('data', c)
113+
}
114+
115+
end (c) {
116+
if (this.muted) {
117+
if (c && this.replace) {
118+
c = c.toString().replace(/./g, this.replace)
119+
} else {
120+
c = null
121+
}
122+
}
123+
if (c) {
124+
this.emit('data', c)
125+
}
126+
this.emit('end')
127+
}
128+
129+
destroy (...args) {
130+
return this.#proxy('destroy', ...args)
131+
}
132+
133+
destroySoon (...args) {
134+
return this.#proxy('destroySoon', ...args)
135+
}
136+
137+
close (...args) {
138+
return this.#proxy('close', ...args)
139+
}
140+
}
141+
142+
module.exports = MuteStream
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "mute-stream",
3+
"version": "3.0.0",
4+
"main": "lib/index.js",
5+
"devDependencies": {
6+
"@npmcli/eslint-config": "^5.0.0",
7+
"@npmcli/template-oss": "4.27.1",
8+
"tap": "^16.3.0"
9+
},
10+
"scripts": {
11+
"test": "tap",
12+
"lint": "npm run eslint",
13+
"postlint": "template-oss-check",
14+
"template-oss-apply": "template-oss-apply --force",
15+
"lintfix": "npm run eslint -- --fix",
16+
"snap": "tap",
17+
"posttest": "npm run lint",
18+
"eslint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\""
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/npm/mute-stream.git"
23+
},
24+
"keywords": [
25+
"mute",
26+
"stream",
27+
"pipe"
28+
],
29+
"author": "GitHub Inc.",
30+
"license": "ISC",
31+
"description": "Bytes go in, but they don't come out (when muted).",
32+
"files": [
33+
"bin/",
34+
"lib/"
35+
],
36+
"tap": {
37+
"statements": 70,
38+
"branches": 60,
39+
"functions": 81,
40+
"lines": 70,
41+
"nyc-arg": [
42+
"--exclude",
43+
"tap-snapshots/**"
44+
]
45+
},
46+
"engines": {
47+
"node": "^20.17.0 || >=22.9.0"
48+
},
49+
"templateOSS": {
50+
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
51+
"version": "4.27.1",
52+
"publish": true
53+
}
54+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The ISC License
2+
3+
Copyright (c) Isaac Z. Schlueter and Contributors
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15+
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.read = read;
7+
const mute_stream_1 = __importDefault(require("mute-stream"));
8+
const readline_1 = require("readline");
9+
async 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
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

0 commit comments

Comments
 (0)