init
Some checks failed
Publish Library / Build NPM Project (push) Failing after 18s
Publish Library / Tag Version (push) Has been skipped
Publish Library / Publish CDN & Docs (push) Has been skipped

This commit is contained in:
2024-11-29 17:57:07 -05:00
commit 9f71f97833
34 changed files with 4023 additions and 0 deletions

14
src/cli.ts Normal file
View 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
View File

@ -0,0 +1,2 @@
export * from './cli';
export * from './input';

49
src/input.ts Normal file
View 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();
}
});
}