init
This commit is contained in:
14
src/cli.ts
Normal file
14
src/cli.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import {exec, execSync} from 'child_process';
|
||||
|
||||
export function $(str: TemplateStringsArray, ...args: string[]): Promise<string> {
|
||||
let cmd = str.reduce((acc, part, i) => acc + part + (args[i] || ''), '');
|
||||
return new Promise((res, rej) => exec(cmd, (err, stdout, stderr) => {
|
||||
if(err || stderr) return rej(err || stderr);
|
||||
return res(stdout);
|
||||
}))
|
||||
}
|
||||
|
||||
export function $Sync(str: TemplateStringsArray, ...args: string[]): string {
|
||||
let cmd = str.reduce((acc, part, i) => acc + part + (args[i] || ''), '');
|
||||
return execSync(cmd, { encoding: 'utf-8' });
|
||||
}
|
2
src/index.ts
Normal file
2
src/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './cli';
|
||||
export * from './input';
|
49
src/input.ts
Normal file
49
src/input.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import readline from 'node:readline';
|
||||
|
||||
/**
|
||||
* Retrieve input form the CLI
|
||||
*
|
||||
* @param {string} prompt Prepend a prompt before the cursor carrot
|
||||
* @param {boolean} hide Hide user input (replacing with stars)
|
||||
* @return {Promise<string>} User input
|
||||
*/
|
||||
export function ask(prompt: string, hide = false): Promise<string> {
|
||||
const rl: any = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
terminal: true
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
if (!hide) {
|
||||
rl.question(prompt, (answer: string) => {
|
||||
rl.close();
|
||||
resolve(answer);
|
||||
});
|
||||
} else {
|
||||
rl.output.write(prompt);
|
||||
let input = '';
|
||||
|
||||
// Listen for 'keypress' to handle masking
|
||||
rl.input.on('keypress', (char: string, key: any) => {
|
||||
if (key && key.name === 'return') {
|
||||
rl.output.write('\n'); // Submit on new line
|
||||
rl.close();
|
||||
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 {
|
||||
input += char;
|
||||
rl.output.write('\b*'); // Mask the input with '*'
|
||||
}
|
||||
});
|
||||
|
||||
// Restore settings
|
||||
rl.input.setRawMode(true);
|
||||
rl.input.resume();
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user