Files
openclaw/src/cli/command-format.ts

26 lines
971 B
TypeScript
Raw Normal View History

2026-01-27 11:27:41 +00:00
import { replaceCliName, resolveCliName } from "./cli-name.js";
import { normalizeProfileName } from "./profile-utils.js";
2026-01-20 07:42:21 +00:00
2026-01-30 03:15:10 +01:00
const CLI_PREFIX_RE = /^(?:pnpm|npm|bunx|npx)\s+openclaw\b|^openclaw\b/;
const PROFILE_FLAG_RE = /(?:^|\s)--profile(?:\s|=|$)/;
const DEV_FLAG_RE = /(?:^|\s)--dev(?:\s|$)/;
2026-01-20 07:42:21 +00:00
export function formatCliCommand(
command: string,
env: Record<string, string | undefined> = process.env as Record<string, string | undefined>,
): string {
2026-01-30 03:15:10 +01:00
const cliName = resolveCliName();
2026-01-27 11:27:41 +00:00
const normalizedCommand = replaceCliName(command, cliName);
2026-01-30 03:15:10 +01:00
const profile = normalizeProfileName(env.OPENCLAW_PROFILE);
if (!profile) {
return normalizedCommand;
}
if (!CLI_PREFIX_RE.test(normalizedCommand)) {
return normalizedCommand;
}
2026-01-27 11:27:41 +00:00
if (PROFILE_FLAG_RE.test(normalizedCommand) || DEV_FLAG_RE.test(normalizedCommand)) {
return normalizedCommand;
}
return normalizedCommand.replace(CLI_PREFIX_RE, (match) => `${match} --profile ${profile}`);
2026-01-20 07:42:21 +00:00
}