execli
Version:
Generate task-oriented CLIs declaratively
55 lines • 2.06 kB
JavaScript
import { isAbsolute, join } from "node:path";
import { argv, cwd } from "node:process";
import { pathToFileURL } from "node:url";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { runCli } from "./commands.js";
import { compile } from "./compile.js";
// `"true"` instead of `true` because of https://github.com/DefinitelyTyped/DefinitelyTyped/issues/37797.
const required = "true";
const compileCommand = {
builder: (args) => args
.positional("source", {
describe: "The path resolving to the file exporting the commands.",
normalize: true,
required,
type: "string",
})
.positional("target", {
describe: "The path where the compiled Node.js file will be written to.",
normalize: true,
required,
type: "string",
}),
command: "compile <source> <target>",
describe: "Compile the commands at the given path to a single executable Node.js file, together with all the dependencies.",
handler: compile,
};
const runCommand = {
builder: (args) => args.positional("path", {
describe: "The path resolving to the file exporting the commands.",
normalize: true,
required,
type: "string",
}),
command: "run <path>",
describe: "Run the commands at the given path, forwarding the command line arguments after `--`.",
async handler({ _: [_, ...commandArgv], path: commandsPath }) {
const commandsAbsolutePath = isAbsolute(commandsPath)
? commandsPath
: join(cwd(), commandsPath);
const commandsUrl = pathToFileURL(commandsAbsolutePath);
const commands = (await import(commandsUrl.href));
await runCli(commands, commandArgv.map(String));
},
};
const yargsInstance = yargs(hideBin(argv));
export const createCli = () => yargsInstance
.command(compileCommand)
.command(runCommand)
.demandCommand()
.strict()
.version(false)
.wrap(yargsInstance.terminalWidth());
//# sourceMappingURL=cli.js.map