detect-shells
Version:
Detect shells installed on a system
147 lines (146 loc) • 5.68 kB
JavaScript
;
// automatically generated by scripts/fetch-shells.mjs
// do not edit this file directly.
Object.defineProperty(exports, "__esModule", { value: true });
exports.launch = exports.getAvailableShells = exports.parse = exports.Default = exports.Shell = void 0;
const child_process_1 = require("child_process");
const fatal_error_1 = require("../fatal-error");
const app_path_1 = require("app-path");
const enum_1 = require("../enum");
var Shell;
(function (Shell) {
Shell["Terminal"] = "Terminal";
Shell["Hyper"] = "Hyper";
Shell["iTerm2"] = "iTerm2";
Shell["PowerShellCore"] = "PowerShell Core";
Shell["Kitty"] = "Kitty";
Shell["Alacritty"] = "Alacritty";
Shell["Tabby"] = "Tabby";
Shell["WezTerm"] = "WezTerm";
Shell["Warp"] = "Warp";
})(Shell = exports.Shell || (exports.Shell = {}));
exports.Default = Shell.Terminal;
function parse(label) {
var _a;
return (_a = (0, enum_1.parseEnumValue)(Shell, label)) !== null && _a !== void 0 ? _a : exports.Default;
}
exports.parse = parse;
function getBundleID(shell) {
switch (shell) {
case Shell.Terminal:
return 'com.apple.Terminal';
case Shell.iTerm2:
return 'com.googlecode.iterm2';
case Shell.Hyper:
return 'co.zeit.hyper';
case Shell.PowerShellCore:
return 'com.microsoft.powershell';
case Shell.Kitty:
return 'net.kovidgoyal.kitty';
case Shell.Alacritty:
return 'io.alacritty';
case Shell.Tabby:
return 'org.tabby';
case Shell.WezTerm:
return 'com.github.wez.wezterm';
case Shell.Warp:
return 'dev.warp.Warp-Stable';
default:
return (0, fatal_error_1.assertNever)(shell, `Unknown shell: ${shell}`);
}
}
async function getShellPath(shell) {
const bundleId = getBundleID(shell);
try {
return await (0, app_path_1.default)(bundleId);
}
catch (e) {
// `appPath` will raise an error if it cannot find the program.
return null;
}
}
async function getAvailableShells() {
const [terminalPath, hyperPath, iTermPath, powerShellCorePath, kittyPath, alacrittyPath, tabbyPath, wezTermPath, warpPath,] = await Promise.all([
getShellPath(Shell.Terminal),
getShellPath(Shell.Hyper),
getShellPath(Shell.iTerm2),
getShellPath(Shell.PowerShellCore),
getShellPath(Shell.Kitty),
getShellPath(Shell.Alacritty),
getShellPath(Shell.Tabby),
getShellPath(Shell.WezTerm),
getShellPath(Shell.Warp),
]);
const shells = [];
if (terminalPath) {
shells.push({ shell: Shell.Terminal, path: terminalPath });
}
if (hyperPath) {
shells.push({ shell: Shell.Hyper, path: hyperPath });
}
if (iTermPath) {
shells.push({ shell: Shell.iTerm2, path: iTermPath });
}
if (powerShellCorePath) {
shells.push({ shell: Shell.PowerShellCore, path: powerShellCorePath });
}
if (kittyPath) {
const kittyExecutable = `${kittyPath}/Contents/MacOS/kitty`;
shells.push({ shell: Shell.Kitty, path: kittyExecutable });
}
if (alacrittyPath) {
const alacrittyExecutable = `${alacrittyPath}/Contents/MacOS/alacritty`;
shells.push({ shell: Shell.Alacritty, path: alacrittyExecutable });
}
if (tabbyPath) {
const tabbyExecutable = `${tabbyPath}/Contents/MacOS/Tabby`;
shells.push({ shell: Shell.Tabby, path: tabbyExecutable });
}
if (wezTermPath) {
const wezTermExecutable = `${wezTermPath}/Contents/MacOS/wezterm`;
shells.push({ shell: Shell.WezTerm, path: wezTermExecutable });
}
if (warpPath) {
const warpExecutable = `${warpPath}/Contents/MacOS/stable`;
shells.push({ shell: Shell.Warp, path: warpExecutable });
}
return shells;
}
exports.getAvailableShells = getAvailableShells;
function launch(foundShell, path) {
if (foundShell.shell === Shell.Kitty) {
// kitty does not handle arguments as expected when using `open` with
// an existing session but closed window (it reverts to the previous
// directory rather than using the new directory directory).
//
// This workaround launches the internal `kitty` executable which
// will open a new window to the desired path.
return (0, child_process_1.spawn)(foundShell.path, ['--single-instance', '--directory', path]);
}
else if (foundShell.shell === Shell.Alacritty) {
// Alacritty cannot open files in the folder format.
//
// It uses --working-directory command to start the shell
// in the specified working directory.
return (0, child_process_1.spawn)(foundShell.path, ['--working-directory', path]);
}
else if (foundShell.shell === Shell.Tabby) {
// Tabby cannot open files in the folder format.
//
// It uses open command to start the shell
// in the specified working directory.
return (0, child_process_1.spawn)(foundShell.path, ['open', path]);
}
else if (foundShell.shell === Shell.WezTerm) {
// WezTerm, like Alacritty, "cannot open files in the 'folder' format."
//
// It uses the subcommand `start`, followed by the option `--cwd` to set
// the working directory, followed by the path.
return (0, child_process_1.spawn)(foundShell.path, ['start', '--cwd', path]);
}
else {
const bundleID = getBundleID(foundShell.shell);
return (0, child_process_1.spawn)('open', ['-b', bundleID, path]);
}
}
exports.launch = launch;