UNPKG

@magicdawn/x-args

Version:

play with cli commands like a composer

115 lines (111 loc) 4.13 kB
#!/usr/bin/env node import { a as getFilenameTokens, c as BaseCommand, n as TxtCommand, o as printFilenameTokens, s as renderFilenameTokens } from "./txt-__ezvIi7.js"; import { createRequire } from "node:module"; import { Builtins, Cli, Command, Option } from "clipanion"; import chalk from "chalk"; import { execSync } from "node:child_process"; import { delay } from "es-toolkit"; import boxen from "boxen"; import fg from "fast-glob"; import { PathFinder } from "mac-helper"; import { ExecaSyncError, execaSync } from "execa"; //#region src/commands/default.ts var DefaultCommand = class extends BaseCommand { static paths = [Command.Default]; static usage = { description: "xargs" }; command = Option.String("-c,--command", "", { description: "the command to execute" }); execute() { return this.run(); } async run() { const { files, command } = this; console.log(""); console.log(`${chalk.green("[x-args]")}: received`); console.log(` ${chalk.cyan("files")}: ${chalk.yellow(files)}`); console.log(`${chalk.cyan("command")}: ${chalk.yellow(command)}`); console.log(""); let resolvedFiles = []; if (files === "$PF") { resolvedFiles = await PathFinder.allSelected(); if (!resolvedFiles.length) { console.error("$PF has no selected files"); process.exit(1); } } else { resolvedFiles = fg.sync(files, { caseSensitiveMatch: !this.ignoreCase }); console.log(`${chalk.green("[globby]")}: docs ${chalk.blue("https://github.com/mrmlnc/fast-glob#pattern-syntax")}`); } console.log(`${chalk.green("[files]")}: mapping ${chalk.yellow(files)} to ${chalk.yellow(resolvedFiles.length)} files ->`); resolvedFiles.forEach((f) => { console.log(` ${chalk.cyan(f)}`); }); for (const item of resolvedFiles) { const tokens = getFilenameTokens(item); const usingCommand = renderFilenameTokens(this.command, tokens); console.log(""); console.log(`${chalk.green("[exec]")} for ${chalk.yellow(item)}`); this.command && console.log(` ${chalk.green("command")}: ${chalk.yellow(usingCommand)}`); if (this.showTokens || !this.command) printFilenameTokens(tokens); if (this.yes) execSync(usingCommand, { stdio: "inherit" }); } if (!this.yes) { console.log(""); console.log("-".repeat(80)); console.log(` current ${chalk.yellow("previewing")} commands. After comfirmed, append ${chalk.green("-y or --yes")} flag to execute`); console.log("-".repeat(80)); console.log(""); } } }; //#endregion //#region src/commands/run-in-platypus.ts /** * what's this: https://github.com/sveinbjornt/Platypus/issues/274 */ var RunInPlatypusCommand = class extends Command { static paths = [["run-in-platypus"], ["platypus"]]; static usage = { description: "run command in platypus" }; args = Option.Proxy({ name: "the command you want to proxy" }); async execute() { if (this.args[0] === "--") this.args.shift(); console.log("[RunInPlatypusCommand] args = %O", this.args); let err; try { execaSync({ stdio: "inherit" })`${this.args}`; } catch (e) { err = e; } if (err instanceof ExecaSyncError && err.isTerminated) { console.error("[RunInPlatypusCommand] terminated: signal => %s, signalDescription => %s", err.signal, err.signalDescription); throw err; } if (err) { console.log(); console.log(boxen("↓↓↓ [RunInPlatypusCommand] error happens", { padding: 1 })); console.error(err.stack || err); console.log(); console.log(); console.log(`ALERT:Error|${(err.stack || err.message || "").split("\n")[0]}`); console.log(); while (true) await delay(2147483647); } } }; //#endregion //#region src/bin.ts const { version, name, bin } = createRequire(import.meta.url)("../package"); const [node, app, ...args] = process.argv; const cli = new Cli({ binaryLabel: name, binaryName: Object.keys(bin)[0], binaryVersion: version }); cli.register(Builtins.HelpCommand); cli.register(Builtins.VersionCommand); cli.register(Builtins.DefinitionsCommand); cli.register(TxtCommand); cli.register(DefaultCommand); cli.register(RunInPlatypusCommand); cli.runExit(args, Cli.defaultContext); //#endregion export { };