|
| 1 | +import shelljs from "shelljs"; |
| 2 | +export const { |
| 3 | + ls, find, dirs, |
| 4 | + cd, pwd, pushd, popd, |
| 5 | + cp, rm, mv, mkdir, ln, touch, tempdir, |
| 6 | + chmod, |
| 7 | + test, error, |
| 8 | + cat, sed, grep, sort, head, tail, uniq, |
| 9 | + which, |
| 10 | + exit, env, set, |
| 11 | + exec, |
| 12 | + config |
| 13 | +}= shelljs; |
| 14 | + |
| 15 | +import chalk from "chalk"; |
| 16 | +export { chalk, chalk as s }; |
| 17 | + |
| 18 | +import fetch from 'node-fetch'; |
| 19 | +export { fetch }; |
| 20 | + |
| 21 | +import { log } from "node:console"; |
| 22 | +export function echo(...messages){ |
| 23 | + log(...messages.map(v=> |
| 24 | + v instanceof Error && config.verbose ? v : |
| 25 | + ( String(v)==="[object Object]" ? v : String(v).replaceAll("\t", " ") ))); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Returns object representation of given arguments. Script name is under `_name` key, arguments without `-`/`--` are under `_` key. |
| 30 | + * @param {NodeJS.Process.argv} argv |
| 31 | + * @param {Record<string, any>} initial Initial values |
| 32 | + * @returns {{ _name: string, _: string[] } & Record<string, any>} |
| 33 | + * @example |
| 34 | + * script arg1 --arg2=val -arg3 val --arg4 |
| 35 | + * => { _name: "script", _: [ "arg1" ], arg2: "val", arg3: "val", arg4: true } |
| 36 | + * */ |
| 37 | +export function parseArgsMinimal(initial= {}, argv= process.argv){ |
| 38 | + const out= Object.create(initial); |
| 39 | + out._name= argv[1].slice(argv[1].lastIndexOf("/")+1); |
| 40 | + out._= []; |
| 41 | + const args= argv.slice(2); |
| 42 | + for(let i= 0, { length }= args; i<length; i+= 1){ |
| 43 | + const item= args[i]; |
| 44 | + if(0===item.indexOf("--")){ |
| 45 | + const [ name, value= true ]= item.slice(2).split("="); |
| 46 | + Reflect.set(out, name, value); |
| 47 | + continue; |
| 48 | + } |
| 49 | + if(0!==item.indexOf("-")){ |
| 50 | + out._.push(item); |
| 51 | + continue; |
| 52 | + } |
| 53 | + let next= args[i+1]; |
| 54 | + if(!next || !next.indexOf("-")) |
| 55 | + next= true; |
| 56 | + Reflect.set(out, item.slice(1), next); |
| 57 | + if(next!==true) |
| 58 | + i+= 1; |
| 59 | + } |
| 60 | + if(!out._.length && initial._ && initial._.length) |
| 61 | + out._= initial._; |
| 62 | + return out; |
| 63 | +} |
| 64 | + |
| 65 | +import { createInterface } from 'node:readline'; |
| 66 | +/** |
| 67 | + * Promt user for answer. |
| 68 | + * @param {string} query |
| 69 | + * @param {{ completions: string[] }} options |
| 70 | + * */ |
| 71 | +export function question(query= "", options= undefined){ |
| 72 | + query= String(query); |
| 73 | + if(!/\s$/.test(query)) query+= "\n"+chalk.greenBright.bold('❯ '); |
| 74 | + |
| 75 | + const rl= createInterface({ |
| 76 | + input: process.stdin, |
| 77 | + output: process.stdout, |
| 78 | + terminal: true, |
| 79 | + completer: !options || !Array.isArray(options.completions) ? undefined : |
| 80 | + function completer(line){ |
| 81 | + const completions= options.completions; |
| 82 | + const hits= completions.filter((c) => c.startsWith(line)); |
| 83 | + return [hits.length ? hits : completions, line]; |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + return new Promise(resolve=> |
| 88 | + rl.question(query, |
| 89 | + answer=> { rl.close(); resolve(answer); })); |
| 90 | +} |
| 91 | + |
| 92 | +export async function stdin(){ |
| 93 | + let buf = ''; |
| 94 | + process.stdin.setEncoding('utf8'); |
| 95 | + for await (const chunk of process.stdin) |
| 96 | + buf += chunk; |
| 97 | + return buf; |
| 98 | +} |
0 commit comments