Formatting
This commit is contained in:
@@ -19,7 +19,12 @@
|
||||
* xo-cli completions fish --install
|
||||
*/
|
||||
|
||||
import { existsSync, readFileSync, appendFileSync, writeFileSync } from "node:fs";
|
||||
import {
|
||||
existsSync,
|
||||
readFileSync,
|
||||
appendFileSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { homedir } from "node:os";
|
||||
@@ -40,7 +45,16 @@ const MNEMONIC_SUBS = ["create", "import", "list", "expose"];
|
||||
/** Subcommands for the template command */
|
||||
const TEMPLATE_SUBS = ["import", "list", "inspect", "set-default"];
|
||||
/** Subcommands for the invitation command */
|
||||
const INVITATION_SUBS = ["create", "append", "sign", "broadcast", "requirements", "import", "inspect", "list"];
|
||||
const INVITATION_SUBS = [
|
||||
"create",
|
||||
"append",
|
||||
"sign",
|
||||
"broadcast",
|
||||
"requirements",
|
||||
"import",
|
||||
"inspect",
|
||||
"list",
|
||||
];
|
||||
/** Subcommands for the resource command */
|
||||
const RESOURCE_SUBS = ["list", "unreserve", "unreserve-all"];
|
||||
/** Subcommands for the completions command */
|
||||
@@ -57,7 +71,16 @@ export const COMMAND_TREE = {
|
||||
} as const;
|
||||
|
||||
/** Global option flags available on every command. */
|
||||
const GLOBAL_OPTIONS = ["-h", "--help", "-v", "--verbose", "-m", "--mnemonic-file", "-o", "--output"];
|
||||
const GLOBAL_OPTIONS = [
|
||||
"-h",
|
||||
"--help",
|
||||
"-v",
|
||||
"--verbose",
|
||||
"-m",
|
||||
"--mnemonic-file",
|
||||
"-o",
|
||||
"--output",
|
||||
];
|
||||
|
||||
/**
|
||||
* Gets the path to the scripts directory containing shell templates.
|
||||
@@ -92,13 +115,22 @@ function loadAndProcessTemplate(templateName: string, binName: string): string {
|
||||
content = content.replace(/\{\{OPTIONS\}\}/g, options);
|
||||
content = content.replace(/\{\{MNEMONIC_SUBS\}\}/g, MNEMONIC_SUBS.join(" "));
|
||||
content = content.replace(/\{\{TEMPLATE_SUBS\}\}/g, TEMPLATE_SUBS.join(" "));
|
||||
content = content.replace(/\{\{INVITATION_SUBS\}\}/g, INVITATION_SUBS.join(" "));
|
||||
content = content.replace(
|
||||
/\{\{INVITATION_SUBS\}\}/g,
|
||||
INVITATION_SUBS.join(" "),
|
||||
);
|
||||
content = content.replace(/\{\{RESOURCE_SUBS\}\}/g, RESOURCE_SUBS.join(" "));
|
||||
|
||||
// Fish-specific placeholders
|
||||
if (templateName.endsWith(".fish")) {
|
||||
content = content.replace(/\{\{TOP_LEVEL_COMMANDS\}\}/g, generateFishTopLevelCommands(binName));
|
||||
content = content.replace(/\{\{STATIC_SUBCOMMANDS\}\}/g, generateFishStaticSubcommands(binName));
|
||||
content = content.replace(
|
||||
/\{\{TOP_LEVEL_COMMANDS\}\}/g,
|
||||
generateFishTopLevelCommands(binName),
|
||||
);
|
||||
content = content.replace(
|
||||
/\{\{STATIC_SUBCOMMANDS\}\}/g,
|
||||
generateFishStaticSubcommands(binName),
|
||||
);
|
||||
}
|
||||
|
||||
return content;
|
||||
@@ -110,7 +142,9 @@ function loadAndProcessTemplate(templateName: string, binName: string): string {
|
||||
function generateFishTopLevelCommands(binName: string): string {
|
||||
const lines: string[] = [];
|
||||
for (const cmd of Object.keys(COMMAND_TREE)) {
|
||||
lines.push(`complete -c ${binName} -n "__fish_use_subcommand" -a "${cmd}" -d "${cmd} command"`);
|
||||
lines.push(
|
||||
`complete -c ${binName} -n "__fish_use_subcommand" -a "${cmd}" -d "${cmd} command"`,
|
||||
);
|
||||
}
|
||||
return lines.join("\n");
|
||||
}
|
||||
@@ -122,7 +156,9 @@ function generateFishStaticSubcommands(binName: string): string {
|
||||
const lines: string[] = [];
|
||||
for (const [cmd, subs] of Object.entries(COMMAND_TREE)) {
|
||||
for (const sub of subs) {
|
||||
lines.push(`complete -c ${binName} -n "__fish_seen_subcommand_from ${cmd}; and not __fish_seen_subcommand_from ${subs.join(" ")}" -a "${sub}" -d "${cmd} ${sub}"`);
|
||||
lines.push(
|
||||
`complete -c ${binName} -n "__fish_seen_subcommand_from ${cmd}; and not __fish_seen_subcommand_from ${subs.join(" ")}" -a "${sub}" -d "${cmd} ${sub}"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return lines.join("\n");
|
||||
@@ -163,7 +199,10 @@ const generators: Record<ShellType, (binName: string) => string> = {
|
||||
/**
|
||||
* Shell config file paths and eval commands for each shell type.
|
||||
*/
|
||||
const shellConfigs: Record<ShellType, { configFile: string; evalCommand: (binName: string) => string }> = {
|
||||
const shellConfigs: Record<
|
||||
ShellType,
|
||||
{ configFile: string; evalCommand: (binName: string) => string }
|
||||
> = {
|
||||
bash: {
|
||||
configFile: join(homedir(), ".bashrc"),
|
||||
evalCommand: (binName) => `eval "$(${binName} completions bash)"`,
|
||||
@@ -199,7 +238,8 @@ function installCompletions(shell: ShellType, binName: string): boolean {
|
||||
}
|
||||
|
||||
// Append the completion line
|
||||
const newLine = existingContent.endsWith("\n") || existingContent === "" ? "" : "\n";
|
||||
const newLine =
|
||||
existingContent.endsWith("\n") || existingContent === "" ? "" : "\n";
|
||||
const completionBlock = `${newLine}\n# ${binName} shell completions\n${evalCommand}\n`;
|
||||
|
||||
appendFileSync(config.configFile, completionBlock);
|
||||
@@ -227,14 +267,26 @@ export function handleCompletionsCommand(
|
||||
console.error(`Usage: ${binName} completions <${supported}> [--install]`);
|
||||
console.error("");
|
||||
console.error("Examples:");
|
||||
console.error(` eval "$(${binName} completions bash)" # Output to stdout (add to ~/.bashrc)`);
|
||||
console.error(` eval "$(${binName} completions zsh)" # Output to stdout (add to ~/.zshrc)`);
|
||||
console.error(` ${binName} completions fish | source # Output to stdout (add to fish config)`);
|
||||
console.error(
|
||||
` eval "$(${binName} completions bash)" # Output to stdout (add to ~/.bashrc)`,
|
||||
);
|
||||
console.error(
|
||||
` eval "$(${binName} completions zsh)" # Output to stdout (add to ~/.zshrc)`,
|
||||
);
|
||||
console.error(
|
||||
` ${binName} completions fish | source # Output to stdout (add to fish config)`,
|
||||
);
|
||||
console.error("");
|
||||
console.error("Install directly to shell config:");
|
||||
console.error(` ${binName} completions bash --install # Appends to ~/.bashrc`);
|
||||
console.error(` ${binName} completions zsh --install # Appends to ~/.zshrc`);
|
||||
console.error(` ${binName} completions fish --install # Appends to ~/.config/fish/config.fish`);
|
||||
console.error(
|
||||
` ${binName} completions bash --install # Appends to ~/.bashrc`,
|
||||
);
|
||||
console.error(
|
||||
` ${binName} completions zsh --install # Appends to ~/.zshrc`,
|
||||
);
|
||||
console.error(
|
||||
` ${binName} completions fish --install # Appends to ~/.config/fish/config.fish`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user