diff --git a/src/commands/specs/list.ts b/src/commands/specs/list.ts new file mode 100644 index 00000000..67600950 --- /dev/null +++ b/src/commands/specs/list.ts @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { Command } from "commander"; +import { loadConfig } from "../../utils/config.js"; +import { getSpecNames, loadLocalSpecsSet } from "../../runtime/runtime.js"; +import { getAliasNames, loadAliases } from "../../runtime/alias.js"; +import { aliasSupportedShells, Shell } from "../../utils/shell.js"; + +const supportedShells = aliasSupportedShells.join(", "); + +type ListCommandOptions = { + shell: Shell | undefined; +}; + +const action = (program: Command) => async (options: ListCommandOptions) => { + await loadConfig(program); + await loadLocalSpecsSet(); + + const { shell } = options; + if (shell && !aliasSupportedShells.map((s) => s.valueOf()).includes(shell)) { + program.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`, { exitCode: 1 }); + } + if (shell) { + await loadAliases(shell); + } + process.stdout.write(JSON.stringify([...getAliasNames(), ...getSpecNames()])); + process.exit(0); +}; + +const cmd = new Command("list"); +cmd.description(`list the names of all available specs`); +cmd.option("-s, --shell ", `shell to use alias specs, supported shells: ${supportedShells}`); +cmd.action(action(cmd)); + +export default cmd; diff --git a/src/commands/specs/root.ts b/src/commands/specs/root.ts new file mode 100644 index 00000000..f5d3649c --- /dev/null +++ b/src/commands/specs/root.ts @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { Command } from "commander"; +import list from "./list.js"; + +const cmd = new Command("specs"); +cmd.description(`manage specs`); +cmd.addCommand(list); + +export default cmd; diff --git a/src/index.ts b/src/index.ts index 7ed815b7..05234c81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { Command, Option } from "commander"; import complete from "./commands/complete.js"; import uninstall from "./commands/uninstall.js"; import init from "./commands/init.js"; +import specs from "./commands/specs/root.js"; import { action, supportedShells } from "./commands/root.js"; import { getVersion } from "./utils/version.js"; @@ -31,10 +32,12 @@ program .option("-c, --check", `check if shell is in an inshellisense session`) .addOption(hiddenOption("-T, --test", "used to make e2e tests reproducible across machines")) .option("-V, --verbose", `enable verbose logging`) - .showHelpAfterError("(add --help for additional information)"); + .showHelpAfterError("(add --help for additional information)") + .passThroughOptions(); program.addCommand(complete); program.addCommand(uninstall); program.addCommand(init); +program.addCommand(specs); program.parse(); diff --git a/src/runtime/alias.ts b/src/runtime/alias.ts index 4621b6cd..6491ae6d 100644 --- a/src/runtime/alias.ts +++ b/src/runtime/alias.ts @@ -56,6 +56,8 @@ export const loadAliases = async (shell: Shell) => { return []; }; +export const getAliasNames = () => Object.keys(loadedAliases); + export const aliasExpand = (command: CommandToken[]): CommandToken[] => { if (!command.at(0)?.complete) return command; diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index 98cabb10..b77d0f02 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -120,6 +120,10 @@ export const getSuggestions = async (cmd: string, cwd: string, shell: Shell): Pr return { ...result, charactersToDrop }; }; +export const getSpecNames = (): string[] => { + return Object.keys(specSet).filter((spec) => !spec.startsWith("@") && spec != "-"); +}; + const getPersistentOptions = (persistentOptions: Fig.Option[], options?: Fig.Option[]) => { const persistentOptionNames = new Set(persistentOptions.map((o) => (typeof o.name === "string" ? [o.name] : o.name)).flat()); return persistentOptions.concat( diff --git a/src/utils/shell.ts b/src/utils/shell.ts index 396c5e00..7d2d4daf 100644 --- a/src/utils/shell.ts +++ b/src/utils/shell.ts @@ -34,6 +34,7 @@ export const supportedShells = [ ].filter((shell) => shell != null) as Shell[]; export const initSupportedShells = supportedShells.filter((shell) => shell != Shell.Cmd); +export const aliasSupportedShells = [Shell.Bash, Shell.Zsh]; export const userZdotdir = process.env?.ZDOTDIR ?? os.homedir() ?? `~`; export const zdotdir = path.join(os.tmpdir(), `is-zsh`);