From ba58fc5706fcc62ce7ec7f420f70c00f8e04ef17 Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Mon, 27 Nov 2023 10:18:51 -0800 Subject: [PATCH] fix: shell inference when ENV:SHELL isn't set & add back option for user to supply shell (#93) Signed-off-by: Chapman Pendery --- src/commands/root.ts | 8 ++++++-- src/index.ts | 3 ++- src/utils/shell.ts | 4 +++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/commands/root.ts b/src/commands/root.ts index 8024416..e506a1e 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -9,10 +9,14 @@ import { Command } from "commander"; export const supportedShells = shells.join(", "); -export const action = (program: Command) => async () => { +type RootCommandOptions = { + shell: Shell | undefined; +}; + +export const action = (program: Command) => async (options: RootCommandOptions) => { await loadConfig(program); - const shell = (await inferShell()) as unknown as Shell | undefined; + const shell = options.shell ?? ((await inferShell()) as unknown as Shell | undefined); if (shell == null) { program.error(`Unable to identify shell, use the -s/--shell option to provide your shell`, { exitCode: 1 }); } diff --git a/src/index.ts b/src/index.ts index 20f4fb7..06e6404 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { Command } from "commander"; import bind from "./commands/bind.js"; import uninstall from "./commands/uninstall.js"; -import { action } from "./commands/root.js"; +import { action, supportedShells } from "./commands/root.js"; import { getVersion } from "./utils/version.js"; const program = new Command(); @@ -19,6 +19,7 @@ program .description("IDE style command line auto complete") .version(await getVersion(), "-v, --version", "output the current version") .action(action(program)) + .option("-s, --shell ", `shell to use for command execution, supported shells: ${supportedShells}`) .showHelpAfterError("(add --help for additional information)"); program.addCommand(bind); diff --git a/src/utils/shell.ts b/src/utils/shell.ts index ad68926..17e8294 100644 --- a/src/utils/shell.ts +++ b/src/utils/shell.ts @@ -10,7 +10,9 @@ import fs from "node:fs"; export const inferShell = async () => { try { - return path.parse(process.env.SHELL ?? "").name; + const name = path.parse(process.env.SHELL ?? "").name; + const shellName = supportedShells.find((shell) => name.includes(shell)); + if (shellName) return shellName; } catch { /* empty */ }