UNPKG

git-command-helper

Version:
128 lines (121 loc) 3.55 kB
// git-command-helper 2.1.0 by Dimas Lanjaka <dimaslanjaka@gmail.com> (https://www.webmanajemen.com) 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var Bluebird = require('bluebird'); var spawn$1 = require('cross-spawn'); var spawnAsync = require('./dependencies/@expo/spawn-async/build/spawnAsync.js'); var _ = require('lodash'); var cacheStream = require('./cache-stream.js'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var spawn__namespace = /*#__PURE__*/_interopNamespaceDefault(spawn$1); /** * spawn promise * @param command * @param args * @param options * @returns */ function promiseSpawn(command, args = [], options = {}) { if (!command) throw new TypeError("command is required!"); if (typeof args === "string") args = [args]; if (!Array.isArray(args)) { options = args; args = []; } return new Bluebird((resolve, reject) => { const task = spawn__namespace.spawn(command, args, options); const verbose = options.verbose || false; const { encoding = "utf8" } = options; const stdoutCache = new cacheStream(); const stderrCache = new cacheStream(); if (task.stdout) { const stdout = task.stdout.pipe(stdoutCache); if (verbose) stdout.pipe(process.stdout); } if (task.stderr) { const stderr = task.stderr.pipe(stderrCache); if (verbose) stderr.pipe(process.stderr); } task.on("close", code => { if (code) { const e = new Error(getCache(stderrCache, encoding).toString()); e["code"] = code; return reject(e); } resolve(getCache(stdoutCache, encoding).toString()); }); task.on("error", reject); // Listen to exit events if neither stdout and stderr exist (inherit stdio) if (!task.stdout && !task.stderr) { task.on("exit", code => { if (code) { const e = new Error("Spawn failed"); e["code"] = code; return reject(e); } resolve(); }); } }); } function getCache(stream, encoding) { const buf = stream.getCache(); stream.destroy(); if (!encoding) return buf; return buf.toString(encoding); } /** * spawn async */ const spawn = promiseSpawn; /** * spawn async suppress errors * @param command * @param args * @param options * @returns */ const spawnSilent = async function (command, args, options) { try { return await promiseSpawn(command, args, options); } catch (_err) { return _.noop(_err); } }; Object.defineProperty(exports, "crossSpawn", { enumerable: true, get: function () { return spawn$1.spawn; } }); Object.defineProperty(exports, "crossSpawnAsync", { enumerable: true, get: function () { return spawn$1.async; } }); Object.defineProperty(exports, "crossSpawnSync", { enumerable: true, get: function () { return spawn$1.sync; } }); Object.defineProperty(exports, "spawnSync", { enumerable: true, get: function () { return spawn$1.spawnSync; } }); exports.spawnAsync = spawnAsync; exports.default = promiseSpawn; exports.spawn = spawn; exports.spawnSilent = spawnSilent;