detect-shell
Version:
detect shells available on the system
133 lines • 5.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import os from 'os';
import { readFile, stat, lstat } from './pfs';
import { normalize, basename } from 'path';
import { enumeratePowerShellInstallations } from './powershell';
const platform = {
isWindows: process.platform === 'win32',
isMacintosh: process.platform === 'darwin',
isLinux: process.platform === 'linux'
};
export function getWindowsBuildNumber() {
const osVersion = /(\d+)\.(\d+)\.(\d+)/g.exec(os.release());
let buildNumber = 0;
if (osVersion && osVersion.length === 4) {
buildNumber = parseInt(osVersion[3]);
}
return buildNumber;
}
export function detectAvailableShells() {
return platform.isWindows ? detectAvailableWindowsShells() : detectAvailableUnixShells();
}
function detectAvailableWindowsShells() {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
const is32ProcessOn64Windows = process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
const system32Path = `${process.env['windir']}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}`;
let useWSLexe = false;
if (getWindowsBuildNumber() >= 16299) {
useWSLexe = true;
}
const expectedLocations = {
'Command Prompt': [`${system32Path}\\cmd.exe`],
'WSL Bash': [`${system32Path}\\${useWSLexe ? 'wsl.exe' : 'bash.exe'}`],
'Git Bash': [
`${process.env['ProgramW6432']}\\Git\\bin\\bash.exe`,
`${process.env['ProgramW6432']}\\Git\\usr\\bin\\bash.exe`,
`${process.env['ProgramFiles']}\\Git\\bin\\bash.exe`,
`${process.env['ProgramFiles']}\\Git\\usr\\bin\\bash.exe`,
`${process.env['LocalAppData']}\\Programs\\Git\\bin\\bash.exe`
],
Cygwin: [
`${process.env['HOMEDRIVE']}\\cygwin64\\bin\\bash.exe`,
`${process.env['HOMEDRIVE']}\\cygwin\\bin\\bash.exe`
]
};
try {
// Add all of the different kinds of PowerShells
for (var _b = __asyncValues(enumeratePowerShellInstallations()), _c; _c = yield _b.next(), !_c.done;) {
const pwshExe = _c.value;
expectedLocations[pwshExe.displayName] = [pwshExe.exePath];
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
const promises = [];
Object.keys(expectedLocations).forEach((key) => promises.push(validateShellPaths(key, expectedLocations[key])));
const shells = yield Promise.all(promises);
return shells.filter((e) => !!e);
});
}
function detectAvailableUnixShells() {
return __awaiter(this, void 0, void 0, function* () {
const contents = yield readFile('/etc/shells', 'utf8');
const shells = contents
.split('\n')
.filter((e) => e.trim().indexOf('#') !== 0 && e.trim().length > 0);
return shells.map((e) => {
return {
label: basename(e),
path: e
};
});
});
}
function validateShellPaths(label, potentialPaths) {
return __awaiter(this, void 0, void 0, function* () {
if (potentialPaths.length === 0) {
return Promise.resolve(undefined);
}
const current = potentialPaths.shift();
if (current === '') {
return validateShellPaths(label, potentialPaths);
}
try {
const result = yield stat(normalize(current));
if (result.isFile() || result.isSymbolicLink()) {
return {
label,
path: current
};
}
}
catch (e) {
// Also try using lstat as some symbolic links on Windows
// throw 'permission denied' using 'stat' but don't throw
// using 'lstat'
try {
const result = yield lstat(normalize(current));
if (result.isFile() || result.isSymbolicLink()) {
return {
label,
path: current
};
}
}
catch (e) {
// noop
}
}
return validateShellPaths(label, potentialPaths);
});
}
//# sourceMappingURL=index.js.map