UNPKG

ssh2-exec

Version:

Transparent usage between `child_process.exec` and `ssh2.prototype.exec`

197 lines (196 loc) 6.05 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default, exec: () => exec2, local: () => local, remote: () => remote }); module.exports = __toCommonJS(src_exports); var childProcess = __toESM(require("child_process"), 1); var stream = __toESM(require("stream"), 1); var local = function(options, callback) { options.env ??= process.env; if (callback) { return childProcess.exec( options.command, options, function(err, stdout, stderr) { if (err) { let debug; if (stderr.trim().length) { debug = stderr.trim().split(/\r\n|[\n\r\u0085\u2028\u2029]/g)[0]; } const { code, signal } = err; err = Error( [ "Child process exited unexpectedly: ", `code ${JSON.stringify(err.code)}, `, err.signal ? `signal ${JSON.stringify(err.signal)}` : "no signal", debug ? `, got ${JSON.stringify(debug)}` : void 0 ].join("") ); err.code = code; err.signal = signal; } callback(err, stdout, stderr, (err != null ? err.code : void 0) || 0); } ); } else { return childProcess.spawn( options.command, [], Object.assign(options, { shell: options.shell || true }) ); } }; var remote = function(options, callback) { const child = new childProcess.ChildProcess(); child.stdout = new stream.Readable(); child.stdout._read = function() { }; child.stderr = new stream.Readable(); child.stderr._read = function() { }; child.kill = function(signal) { console.warn(`Not implemented, called kill ${signal}`); return true; }; let stdout = ""; let stderr = ""; if (options.cwd) { options.command = `cd ${options.cwd}; ${options.command}`; } options.ssh.exec(options.command, options, function(errArg, stream2) { if (errArg && callback) { return callback(errArg, "", "", NaN); } if (errArg) { return child.emit("error", errArg); } let err = null; stream2.stderr.on("data", function(data) { child.stderr?.push(data); stderr += data; }); stream2.on("data", function(data) { child.stdout?.push(data); stdout += data; }); let code = 0; let signal = null; let exitCalled = false; let stdoutCalled = false; let stderrCalled = false; const exit = function() { if (!(exitCalled && stdoutCalled && stderrCalled)) { return; } child.stdout?.push(null); child.stderr?.push(null); child.emit("close", code, signal); child.emit("exit", code, signal); if (code !== 0) { let debug; if (stderr.trim().length) { debug = stderr.trim().split(/\r\n|[\n\r\u0085\u2028\u2029]/g)[0]; } err = new Error( [ "Child process exited unexpectedly: ", `code ${JSON.stringify(code)}`, signal ? `, signal ${JSON.stringify(signal)}` : ", no signal", debug ? `, got ${JSON.stringify(debug)}` : void 0 ].join("") ); err.code = code; } if (options.end) { options.ssh.end(); options.ssh.on("error", function(err2) { callback(err2, "", "", NaN); }); options.ssh.on("close", function() { if (callback) { callback(err || null, stdout, stderr, code); } }); } else { if (callback) { callback(err || null, stdout, stderr, code); } } }; stream2.on("error", function(code2) { console.error("error", code2); }); stream2.on("exit", function(codeArg, signalArg) { exitCalled = true; code = codeArg; signal = signalArg; exit(); }); stream2.on("end", function() { stdoutCalled = true; exit(); }); stream2.stderr.on("end", function() { stderrCalled = true; exit(); }); }); return child; }; function exec2(arg1, arg2, arg3, arg4) { const options = arg3 !== void 0 ? ( // ssh and cmd separated typeof arg3 === "function" ? { ssh: arg1, command: arg2 } : arg3 ) : arg1; const callback = arg3 !== void 0 ? ( // ssh and cmd separated typeof arg3 === "function" ? arg3 : arg4 ) : arg2; if (options.ssh) { return remote(options, callback); } else { return local(options, callback); } } var src_default = exec2; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { exec, local, remote }); //# sourceMappingURL=index.cjs.map