* Fixed ask function
Some checks failed
Publish Library / Build NPM Project (push) Failing after 21s
Publish Library / Publish CDN & Docs (push) Has been skipped
Publish Library / Tag Version (push) Has been skipped

This commit is contained in:
Zakary Timson 2025-05-12 15:58:02 -04:00
parent 1117c1e3b2
commit 2d1c2034e4
3 changed files with 22 additions and 28 deletions

8
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@ztimson/css-utils", "name": "@ztimson/node-utils",
"version": "1.1.1", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@ztimson/css-utils", "name": "@ztimson/node-utils",
"version": "1.1.1", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/node": "^22.10.1", "@types/node": "^22.10.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/node-utils", "name": "@ztimson/node-utils",
"version": "1.0.0", "version": "1.0.1",
"description": "CSS Utility Classes", "description": "CSS Utility Classes",
"author": "ztimson", "author": "ztimson",
"license": "MIT", "license": "MIT",

View File

@ -1,49 +1,43 @@
import readline from 'node:readline'; import readline from 'node:readline';
/** /**
* Retrieve input form the CLI * Retrieve input from the CLI
* *
* @param {string} prompt Prepend a prompt before the cursor carrot * @param {string} prompt Prepend a prompt before the cursor carrot
* @param {boolean} hide Hide user input (replacing with stars) * @param {boolean} hide Hide user input (replacing with stars)
* @return {Promise<string>} User input * @return {Promise<string>} User input
*/ */
export function ask(prompt: string, hide = false): Promise<string> { export function ask(prompt: string, hide = false) {
const rl: any = readline.createInterface({ const rl: any = readline.createInterface({input: process.stdin, output: process.stdout, terminal: true});
input: process.stdin,
output: process.stdout,
terminal: true
});
return new Promise((resolve) => { return new Promise((resolve) => {
if (!hide) { if (!hide) {
rl.question(prompt, (answer: string) => { rl.question(prompt, (answer: string) => {
rl.close();
resolve(answer); resolve(answer);
rl.close();
}); });
} else { } else {
rl.output.write(prompt);
let input = ''; let input = '';
const onKeyPress = (char: string, key: any) => {
// Listen for 'keypress' to handle masking
rl.input.on('keypress', (char: string, key: any) => {
if (key && key.name === 'return') { if (key && key.name === 'return') {
rl.output.write('\n'); // Submit on new line rl.input.setRawMode(false);
rl.input.removeListener('keypress', onKeyPress);
rl.close(); rl.close();
resolve(input); resolve(input);
} else if (key && key.name === 'backspace') {
if (input.length > 0) {
input = input.slice(0, -1);
rl.output.write(`\r${prompt}${input.replaceAll(/./g, '*')} \b`);
}
} else { } else {
input += char; if (key && key.name === 'backspace') {
rl.output.write('\b*'); // Mask the input with '*' if (input.length > 0) input = input.slice(0, -1);
} else {
input += char;
}
rl.output.write(`\r${prompt}${'*'.repeat(input.length)} `);
} }
}); };
// Restore settings // Attach keypress event
rl.input.on('keypress', onKeyPress);
rl.input.setRawMode(true); rl.input.setRawMode(true);
rl.input.resume(); rl.input.resume();
rl.output.write(prompt);
} }
}); });
} }