allons-y
Version:
Yet another JS/TS tools
38 lines (37 loc) • 1.57 kB
JavaScript
import { fileURLToPath } from 'node:url';
import * as path from 'node:path';
import * as fs from 'node:fs';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import * as cmds from './index.js';
import { totalTimeReadable } from './commands/index.js';
import { log, chalk } from './logger/index.js';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const commands = cmds;
const cwd = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(fs.readFileSync(path.join(cwd, '../package.json'), 'utf8'));
const y = yargs(hideBin(process.argv))
.scriptName(process.env.npm_lifecycle_event === 'npx' ? 'npx allons-y' : 'ay')
.middleware((argv) => {
// eslint-disable-next-line no-param-reassign
argv.startDate = new Date();
})
.epilog(process.env.npm_lifecycle_event === 'npx'
? 'You can globally install allons-y with "npm i -g allons-y" and then execute only the global "ay" command instead of "npx allons-y"!'
: '');
Object.keys(commands).forEach((key) => {
const command = commands[key];
y.command(command.cmd, command.description, command.builder, async (args) => {
log([
chalk.cyan.bold('<'),
chalk.bgCyan.bold(` Allons-y ${pkg.version} > `),
chalk.bgCyanBright.bold(` ${command.title} `),
chalk.cyan.bold('>'),
'\n',
].join(''));
await command.command(args);
log(chalk.bgGray(`\n ${totalTimeReadable(args.startDate, true)} `));
});
});
y.demandCommand(1).strict().parse();