35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import parseArgs from 'minimist'
|
|
export const argv = parseArgs(process.argv.slice(2))
|
|
export function sum(xs: number[]): number {
|
|
return xs.reduce((x, y) => x+y, 0)
|
|
}
|
|
|
|
let writtenHeader = false
|
|
export function csvOut(stuff: Record<string, number | string>) {
|
|
function escape(x: number | string): string {
|
|
if (typeof x === 'number') return "" + x
|
|
return x.indexOf(",") !== -1 ? `"${x}"` : x
|
|
}
|
|
if (!writtenHeader) console.log(Object.keys(stuff).join(','))
|
|
writtenHeader = true
|
|
console.log(Object.values(stuff).map(escape).join(','))
|
|
}
|
|
|
|
export function jsonOut(stuff: any) {
|
|
if (process.stdout.isTTY) console.dir(stuff, { depth: 10 })
|
|
else console.log(JSON.stringify(stuff))
|
|
}
|
|
|
|
|
|
export function runCommands(commands: Record<string, (...args: string[]) => (Promise<void> | void)>) {
|
|
if (argv._.length < 1 || !commands[argv._[0]]) {
|
|
console.log("Available commands:")
|
|
for (const fn of Object.values(commands)) {
|
|
const src = fn.toString().split('{')[0]
|
|
console.log("-", src)
|
|
}
|
|
} else if (commands[argv._[0]]) {
|
|
commands[argv._[0]](...argv._.slice(1))
|
|
}
|
|
}
|