UNPKG

detect-shells

Version:

Detect shells installed on a system

61 lines (60 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isGitOnPath = exports.findGitOnPath = void 0; const child_process_1 = require("child_process"); const Path = require("path"); const log_1 = require("./log"); function captureCommandOutput(command, args, options = {}) { return new Promise((resolve, reject) => { const cp = (0, child_process_1.spawn)(command, args, options); cp.on('error', (error) => { log_1.default.warn(`Unable to spawn ${command}`, error); resolve(undefined); }); const chunks = new Array(); let total = 0; cp.stdout.on('data', (chunk) => { chunks.push(chunk); total += chunk.length; }); cp.on('close', function (code) { if (code !== 0) { resolve(undefined); } else { resolve(Buffer.concat(chunks, total) .toString() .replace(/\r?\n[^]*/m, '')); } }); }); } function findGitOnPath() { // adapted from http://stackoverflow.com/a/34953561/1363815 if (process.platform === 'win32') { const windowsRoot = process.env.SystemRoot || 'C:\\Windows'; const wherePath = Path.join(windowsRoot, 'System32', 'where.exe'); // `where` will list _all_ PATH components where the executable // is found, one per line, and return 0, or print an error and // return 1 if it cannot be found log_1.default.info(`calling captureCommandOutput(where git)`); return captureCommandOutput(wherePath, ['git'], { cwd: windowsRoot }); } if (process.platform === 'darwin' || process.platform === 'linux') { // `which` will print the path and return 0 when the executable // is found under PATH, or return 1 if it cannot be found return captureCommandOutput('which', ['git']); } return Promise.resolve(undefined); } exports.findGitOnPath = findGitOnPath; async function isGitOnPath() { // Modern versions of macOS ship with a Git shim that guides you through // the process of setting everything up. We trust this is available, so // don't worry about looking for it here. if (process.platform === 'darwin') { return Promise.resolve(true); } return (await findGitOnPath()) !== undefined; } exports.isGitOnPath = isGitOnPath;