2019-03-27 15:56:00 +08:00

97 lines
4.1 KiB
JavaScript

"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var isUndefined = require("is-undefined"),
EventEmitter = require("events").EventEmitter,
spawn = require("spawno"),
isWin = require("is-win");
module.exports = function (_EventEmitter) {
_inherits(PowerShell, _EventEmitter);
/**
* PowerShell
*
* @name PowerShell
* @function
* @param {String} input The command or PowerShell script ro execute.
* @param {Object} opt An object containing the following fields:
*
* - `debug` (Boolean): Turn on/off the debug messages (default: `false`).
* - `noprofile` (Boolean): Turn on/off noprofile parameter (default: `true`).
* - `executionpolicy` (Enum): Run powershell with specified executionpolicy (default: System default). Valid enum values are `Restricted`, `AllSigned`, `RemoteSigned`, `Unrestricted`, `Bypass`, `Undefined`.
* - `PSCore` (Boolean) : Turn on/off 'pwsh' the executable for PowerShell Core as opposed to Windowes PowerShell (default: 'false').
*
* @param {Function} cb The callback function (optional).
*/
function PowerShell(input, opt, cb) {
_classCallCheck(this, PowerShell);
var _this = _possibleConstructorReturn(this, (PowerShell.__proto__ || Object.getPrototypeOf(PowerShell)).call(this));
opt = opt || {};
opt.debug = isUndefined(opt.debug) ? false : opt.debug;
opt.noprofile = isUndefined(opt.noprofile) ? true : opt.noprofile;
opt.PSCore = isUndefined(opt.PSCore) ? false : opt.PSCore;
var EXE_NAME = "" + (opt.PSCore ? 'pwsh' : 'powershell') + (isWin() ? ".exe" : "");
var args = [];
if (opt.noprofile) {
args.push("-NoProfile");
}
if (!isUndefined(opt.executionpolicy)) {
args.push("-ExecutionPolicy", opt.executionpolicy);
}
args.push("-Command", "& {" + input + "}");
var _proc = _this.proc = spawn(EXE_NAME, args, { stdio: ["ignore", "pipe", "pipe"] }, cb);
_proc.stdout.setEncoding("utf8");
_proc.stderr.setEncoding("utf8");
_proc.on("error", function (err) {
return _this.emit("error", err);
});
if (opt.debug) {
console.log("<" + EXE_NAME + "> Starting " + _proc.pid + " on " + process.platform);
}
var chunks = [];
_proc.stdout.on("data", function (chunk) {
if (typeof chunk === "string") {
chunks.push(chunk);
} else {
chunks.push.apply(chunks, chunk);
}
});
var err_chunks = [];
_proc.stderr.on("data", function (err_chunk) {
if (typeof err_chunk === "string") {
err_chunks.push(err_chunk);
} else {
err_chunks.push.apply(err_chunks, err_chunk);
}
});
_proc.on("close", function (code) {
if (opt.debug) {
console.log("<" + EXE_NAME + "> Process " + _proc.pid + " exited with code " + code);
}
_this.emit("output", chunks.join(""));
_this.emit("error-output", err_chunks.join(""))
_this.emit("end", code);
});
return _this;
}
return PowerShell;
}(EventEmitter);