Skip to content

Commit 0574654

Browse files
authored
feat: add specs list command (#238)
Signed-off-by: Chapman Pendery <cpendery@vt.edu>
1 parent 375a6f9 commit 0574654

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

src/commands/specs/list.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { Command } from "commander";
5+
import { loadConfig } from "../../utils/config.js";
6+
import { getSpecNames, loadLocalSpecsSet } from "../../runtime/runtime.js";
7+
import { getAliasNames, loadAliases } from "../../runtime/alias.js";
8+
import { aliasSupportedShells, Shell } from "../../utils/shell.js";
9+
10+
const supportedShells = aliasSupportedShells.join(", ");
11+
12+
type ListCommandOptions = {
13+
shell: Shell | undefined;
14+
};
15+
16+
const action = (program: Command) => async (options: ListCommandOptions) => {
17+
await loadConfig(program);
18+
await loadLocalSpecsSet();
19+
20+
const { shell } = options;
21+
if (shell && !aliasSupportedShells.map((s) => s.valueOf()).includes(shell)) {
22+
program.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`, { exitCode: 1 });
23+
}
24+
if (shell) {
25+
await loadAliases(shell);
26+
}
27+
process.stdout.write(JSON.stringify([...getAliasNames(), ...getSpecNames()]));
28+
process.exit(0);
29+
};
30+
31+
const cmd = new Command("list");
32+
cmd.description(`list the names of all available specs`);
33+
cmd.option("-s, --shell <shell>", `shell to use alias specs, supported shells: ${supportedShells}`);
34+
cmd.action(action(cmd));
35+
36+
export default cmd;

src/commands/specs/root.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { Command } from "commander";
5+
import list from "./list.js";
6+
7+
const cmd = new Command("specs");
8+
cmd.description(`manage specs`);
9+
cmd.addCommand(list);
10+
11+
export default cmd;

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Command, Option } from "commander";
1010
import complete from "./commands/complete.js";
1111
import uninstall from "./commands/uninstall.js";
1212
import init from "./commands/init.js";
13+
import specs from "./commands/specs/root.js";
1314
import { action, supportedShells } from "./commands/root.js";
1415
import { getVersion } from "./utils/version.js";
1516

@@ -31,10 +32,12 @@ program
3132
.option("-c, --check", `check if shell is in an inshellisense session`)
3233
.addOption(hiddenOption("-T, --test", "used to make e2e tests reproducible across machines"))
3334
.option("-V, --verbose", `enable verbose logging`)
34-
.showHelpAfterError("(add --help for additional information)");
35+
.showHelpAfterError("(add --help for additional information)")
36+
.passThroughOptions();
3537

3638
program.addCommand(complete);
3739
program.addCommand(uninstall);
3840
program.addCommand(init);
41+
program.addCommand(specs);
3942

4043
program.parse();

src/runtime/alias.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export const loadAliases = async (shell: Shell) => {
5656
return [];
5757
};
5858

59+
export const getAliasNames = () => Object.keys(loadedAliases);
60+
5961
export const aliasExpand = (command: CommandToken[]): CommandToken[] => {
6062
if (!command.at(0)?.complete) return command;
6163

src/runtime/runtime.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export const getSuggestions = async (cmd: string, cwd: string, shell: Shell): Pr
120120
return { ...result, charactersToDrop };
121121
};
122122

123+
export const getSpecNames = (): string[] => {
124+
return Object.keys(specSet).filter((spec) => !spec.startsWith("@") && spec != "-");
125+
};
126+
123127
const getPersistentOptions = (persistentOptions: Fig.Option[], options?: Fig.Option[]) => {
124128
const persistentOptionNames = new Set(persistentOptions.map((o) => (typeof o.name === "string" ? [o.name] : o.name)).flat());
125129
return persistentOptions.concat(

src/utils/shell.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const supportedShells = [
3434
].filter((shell) => shell != null) as Shell[];
3535

3636
export const initSupportedShells = supportedShells.filter((shell) => shell != Shell.Cmd);
37+
export const aliasSupportedShells = [Shell.Bash, Shell.Zsh];
3738

3839
export const userZdotdir = process.env?.ZDOTDIR ?? os.homedir() ?? `~`;
3940
export const zdotdir = path.join(os.tmpdir(), `is-zsh`);

0 commit comments

Comments
 (0)