@jsse/quick-maths
Version:
73 lines (64 loc) • 1.85 kB
text/typescript
import process from "node:process";
import { version } from "../package.json";
import { quickmaths } from "./quick-maths.js";
function isDebug() {
const _debugEnvStr = (process.env["DEBUG"] ?? "false").toLowerCase();
// is not debug
/* v8 ignore start */
const isNotDebug =
_debugEnvStr === "false" || _debugEnvStr === "f" || _debugEnvStr === "0";
/* v8 ignore stop */
return (
!isNotDebug || !process.argv.some((arg) => arg.toLowerCase() === "--debug")
);
}
function isTest() {
return process.env["NODE_ENV"] === "test";
}
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-explicit-any
function noop(..._args: any[]) {}
/* v8 ignore start */
const debug =
isDebug() && !isTest()
? // eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-explicit-any
(...args: any[]) => console.log(...args)
: noop;
const echo = isTest() ? noop : console.log;
/* v8 ignore stop */
export async function cli(args?: string[]) {
const argv = args || [];
if (debug) {
debug("Debugging enabled");
}
if (argv.includes("--help") || argv.includes("-h")) {
echo("Usage: quick-maths");
return;
}
if (argv.includes("--version") || argv.includes("-v")) {
echo(`quick-maths v${version}`);
return;
}
// perform quick maths
const ti = Date.now();
const result = quickmaths();
const tf = Date.now();
echo(
`2 plus 2 that's 4 minus 1 that's ${result} quick maths! (${tf - ti}ms)`,
);
}
/* v8 ignore start */
async function main(args?: string[]) {
const argv = args || process.argv.slice(2);
await cli(argv);
}
if (process.env["NODE_ENV"] !== "test") {
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(() => {
process.exit(0);
});
}
/* v8 ignore stop */