wxt
Version:
⚡ Next-gen Web Extension Framework
62 lines (61 loc) • 2.41 kB
JavaScript
import { filterTruthy, toArray } from "../core/utils/arrays.mjs";
import { registerWxt } from "../core/wxt.mjs";
import { formatDuration } from "../core/utils/time.mjs";
import { printHeader } from "../core/utils/log/printHeader.mjs";
import "../core/utils/log/index.mjs";
import { ValidationError } from "../core/utils/validation.mjs";
import spawn from "nano-spawn";
import consola, { LogLevels } from "consola";
//#region src/cli/cli-utils.ts
/**
* Wrap an action handler to add a timer, error handling, and maybe enable debug
* mode.
*/
function wrapAction(cb, options) {
return async (...args) => {
const level = args.find((arg) => arg?.level)?.level;
if (level && Object.keys(LogLevels).includes(level)) consola.level = LogLevels[level];
if (!!args.find((arg) => arg?.debug)) consola.level = LogLevels.debug;
const startTime = Date.now();
try {
printHeader();
if (!(await cb(...args))?.isOngoing && !options?.disableFinishedLog) consola.success(`Finished in ${formatDuration(Date.now() - startTime)}`);
} catch (err) {
consola.fail(`Command failed after ${formatDuration(Date.now() - startTime)}`);
if (!(err instanceof ValidationError)) consola.error(err);
process.exit(1);
}
};
}
/**
* Array flags, when not passed, are either `undefined` or `[undefined]`. This
* function filters out the
*/
function getArrayFromFlags(flags, name) {
const result = filterTruthy(toArray(flags[name]));
return result.length ? result : void 0;
}
const aliasCommandNames = /* @__PURE__ */ new Set();
/**
* @param base Command to add this one to
* @param name The command name to add
* @param alias The CLI tool being aliased
* @param bin The CLI tool binary name. Usually the same as the alias
* @param docsUrl URL to the docs for the aliased CLI tool
*/
function createAliasedCommand(base, name, alias, bin, docsUrl) {
const aliasedCommand = base.command(`${name} [...args]`, `Alias for ${alias} (${docsUrl})`).allowUnknownOptions().action(async () => {
try {
await registerWxt("build");
await spawn(bin, process.argv.slice(process.argv.indexOf(aliasedCommand.name) + 1), { stdio: "inherit" });
} catch {
process.exit(1);
}
});
aliasCommandNames.add(aliasedCommand.name);
}
function isAliasedCommand(command) {
return !!command && aliasCommandNames.has(command.name);
}
//#endregion
export { createAliasedCommand, getArrayFromFlags, isAliasedCommand, wrapAction };