UNPKG

git-command-helper

Version:
68 lines (63 loc) 1.97 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 child_process = require('child_process'); var spawn$1 = require('./spawn.js'); class spawner { static spawn = spawn$1.default; /** * promises spawn * @param options * @param cmd * @param args * @returns * @example * spawner.promise({}, 'git', 'log', '-n', '1').then(console.log); * spawner.promise({stdio:'pipe'}, 'git', 'submodule', 'status').then(console.log); */ static promise(options = null, cmd, ...args) { return new Promise((resolve, reject) => { // default option inherit if (options === null) { options = { cwd: __dirname, stdio: "inherit" }; } const stdouts = []; const stderrs = []; const child = child_process.spawn(cmd, args, options); // use event hooks to provide a callback to execute when data are available: if (child.stdout !== null) { child.stdout.on("data", function (data) { stdouts.push(data.toString().trim()); }); } if (child.stderr !== null) { child.stderr.on("data", function (data) { stderrs.push(data.toString().trim()); }); } child.on("close", function (code) { // Should probably be 'exit', not 'close' // *** Process completed return resolve({ code: code, stdout: stdouts.length > 0 ? stdouts : child.stdout, stderr: stderrs.length > 0 ? stderrs : stdouts.length === 0 ? child.stderr : null }); }); child.on("error", function (err) { // *** Process creation failed return reject({ args: args, err: err }); }); }); } } const spawn = spawner.spawn; exports.default = spawner; exports.spawn = spawn; exports.spawner = spawner;