UNPKG

detect-shells

Version:

Detect shells installed on a system

117 lines (116 loc) 4.1 kB
"use strict"; // automatically generated by scripts/fetch-shells.mjs // do not edit this file directly. Object.defineProperty(exports, "__esModule", { value: true }); exports.launchShell = exports.findShellOrDefault = exports.getAvailableShells = exports.parse = exports.Default = void 0; const log_1 = require("../log"); const Darwin = require("./darwin"); const Win32 = require("./win32"); const Linux = require("./linux"); const error_1 = require("./error"); const path_exists_1 = require("../path-exists"); /** The default shell. */ exports.Default = (function () { if (process.platform === 'darwin') { return Darwin.Default; } else if (process.platform === 'win32') { return Win32.Default; } else { return Linux.Default; } })(); let shellCache = null; /** Parse the label into the specified shell type. */ function parse(label) { if (process.platform === 'darwin') { return Darwin.parse(label); } else if (process.platform === 'win32') { return Win32.parse(label); } else if (process.platform === 'linux') { return Linux.parse(label); } throw new Error(`Platform not currently supported for resolving shells: ${process.platform}`); } exports.parse = parse; /** Get the shells available for the user. */ async function getAvailableShells() { if (shellCache) { return shellCache; } if (process.platform === 'darwin') { shellCache = await Darwin.getAvailableShells(); return shellCache; } else if (process.platform === 'win32') { shellCache = await Win32.getAvailableShells(); return shellCache; } else if (process.platform === 'linux') { shellCache = await Linux.getAvailableShells(); return shellCache; } return Promise.reject(`Platform not currently supported for resolving shells: ${process.platform}`); } exports.getAvailableShells = getAvailableShells; /** Find the given shell or the default if the given shell can't be found. */ async function findShellOrDefault(shell) { const available = await getAvailableShells(); const found = available.find(s => s.shell === shell); if (found) { return found; } else { return available.find(s => s.shell === exports.Default); } } exports.findShellOrDefault = findShellOrDefault; /** Launch the given shell at the path. */ async function launchShell(shell, path, onError) { // We have to manually cast the wider `Shell` type into the platform-specific // type. This is less than ideal, but maybe the best we can do without // platform-specific build targets. const exists = await (0, path_exists_1.pathExists)(shell.path); if (!exists) { const label = process.platform === 'darwin' ? 'Preferences' : 'Options'; throw new error_1.ShellError(`Could not find executable for '${shell.shell}' at path '${shell.path}'. Please open ${label} and select an available shell.`); } let cp = null; if (process.platform === 'darwin') { cp = Darwin.launch(shell, path); } else if (process.platform === 'win32') { cp = Win32.launch(shell, path); } else if (process.platform === 'linux') { cp = Linux.launch(shell, path); } if (cp != null) { addErrorTracing(shell.shell, cp, onError); return Promise.resolve(); } else { return Promise.reject(`Platform not currently supported for launching shells: ${process.platform}`); } } exports.launchShell = launchShell; function addErrorTracing(shell, cp, onError) { if (cp.stderr !== null) { cp.stderr.on('data', chunk => { const text = chunk instanceof Buffer ? chunk.toString() : chunk; log_1.default.debug(`[${shell}] stderr: '${text}'`); }); } cp.on('error', err => { log_1.default.debug(`[${shell}] an error was encountered`, err); onError(err); }); cp.on('exit', code => { if (code !== 0) { log_1.default.debug(`[${shell}] exit code: ${code}`); } }); }