Skip to content

Commit

Permalink
feat: add specs list command (#238)
Browse files Browse the repository at this point in the history
Signed-off-by: Chapman Pendery <[email protected]>
  • Loading branch information
cpendery authored Apr 15, 2024
1 parent 375a6f9 commit 0574654
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/commands/specs/list.ts
Original file line number Diff line number Diff line change
@@ -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>", `shell to use alias specs, supported shells: ${supportedShells}`);
cmd.action(action(cmd));

export default cmd;
11 changes: 11 additions & 0 deletions src/commands/specs/root.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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();
2 changes: 2 additions & 0 deletions src/runtime/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/utils/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down

0 comments on commit 0574654

Please sign in to comment.