Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add specs list command #238

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading