UNPKG

firmament-yargs

Version:

Typescript classes for building CLI node applications

191 lines 7.91 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; const inversify_1 = require("inversify"); const child_process_1 = require('child_process'); const force_error_impl_1 = require("./force-error-impl"); const readlineSync = require('readline-sync'); const inpathSync = require('inpath').sync; const pidof = require('pidof'); const psTree = require('ps-tree'); let SpawnImpl = class SpawnImpl extends force_error_impl_1.ForceErrorImpl { constructor(_commandUtil) { super(); this.commandUtil = _commandUtil; } spawnShellCommandPipelineAsync(cmdArray, options, cbStatusOrFinal, cbFinal) { let lclCmdArray = cmdArray.slice(0); let cmd = lclCmdArray.pop().slice(0); let childProcess = this.spawnShellCommandAsync(cmd, options, cbStatusOrFinal, cbFinal); let proc = childProcess; while (cmd = lclCmdArray.pop()) { cmd = cmd.slice(0); let prevStdin = proc.stdin; proc = this.spawnShellCommandAsync(cmd); proc.stdout.pipe(prevStdin); } return childProcess; } sudoSpawnPipelineAsync(cmdArray, options, cbStatusOrFinal, cbFinal) { return undefined; } spawnShellCommandAsync(cmd, options = null, cbStatusOrFinal = null, cbFinal = null) { let me = this; let childProcess; if (typeof cbFinal !== 'function') { if (typeof cbStatusOrFinal !== 'function') { cbFinal = cbStatusOrFinal = null; } else { cbFinal = cbStatusOrFinal; cbStatusOrFinal = null; } } else { if (typeof cbStatusOrFinal !== 'function') { cbStatusOrFinal = null; } } if (me.checkForceError('spawnShellCommandAsync', cbFinal)) { return; } try { cmd = cmd || []; cmd = cmd.slice(0); options = options || {}; options.preSpawnMessage = options.preSpawnMessage || ''; options.postSpawnMessage = options.postSpawnMessage || ''; options.showDiagnostics = options.showDiagnostics || false; options.suppressOutput = options.suppressOutput || false; options.stdio = options.stdio || 'pipe'; options.cwd = options.cwd || process.cwd(); if (options.showDiagnostics) { me.commandUtil.log(`Running '${cmd}' @ '${options.cwd}'`); } if (options.preSpawnMessage && cbStatusOrFinal) { cbStatusOrFinal(null, options.preSpawnMessage); } let command = cmd.shift(); let result = ''; childProcess = child_process_1.spawn(command, cmd, options); if (cbFinal) { childProcess.stdout.on('data', function (data) { result += data.toString(); if (cbStatusOrFinal && !options.suppressOutput) { cbStatusOrFinal(null, data.toString()); } }); childProcess.on('exit', function (code, signal) { if (options.postSpawnMessage && cbStatusOrFinal) { cbStatusOrFinal(null, options.postSpawnMessage); cbStatusOrFinal = null; } cbFinal = me.childCloseOrExit(code, signal, result, cbFinal); }); childProcess.on('close', function (code, signal) { if (options.postSpawnMessage && cbStatusOrFinal) { cbStatusOrFinal(null, options.postSpawnMessage); cbStatusOrFinal = null; } cbFinal = me.childCloseOrExit(code, signal, result, cbFinal); }); childProcess.on('error', function (code) { if (cbFinal) { cbFinal(new Error(`spawn error: exit code ${code.code}`), null); cbFinal = null; } }); } } catch (err) { cbFinal(err, null); } return childProcess; } childCloseOrExit(code, signal, result, cbFinal) { if (cbFinal) { if (code !== null) { if (code !== 0) { cbFinal(new Error(`spawn error: exit code ${code}`), result); } else { cbFinal(null, result); } } else { cbFinal(new Error(`child process received signal: ${signal}`), result); } cbFinal = null; } return cbFinal; } sudoSpawnAsync(cmd, options = null, cbStatusOrFinal = null, cbFinal = null) { let me = this; let prompt = '#node-sudo-passwd#'; let prompts = 0; let args = ['-S', '-p', prompt]; cmd = cmd || []; cmd = cmd.slice(0); [].push.apply(args, cmd); let path = process.env['PATH'].split(':'); let sudoBin = inpathSync('sudo', path); args.unshift(sudoBin); let child = me.spawnShellCommandAsync(args, options, cbStatusOrFinal, cbFinal); if (!child) { return; } function waitForStartup(err, children) { if (err) { throw new Error(`Error spawning process`); } if (children && children.length) { child.stderr.removeAllListeners(); } else { setTimeout(function () { psTree(child.pid, waitForStartup); }, 100); } } psTree(child.pid, waitForStartup); child.stderr.on('data', function (data) { let lines = data.toString().trim().split('\n'); lines.forEach(function (line) { if (line === prompt) { if (++prompts > 1) { me.cachedPassword = null; } let username = require('username').sync(); let loginMessage = (prompts > 1) ? `Sorry, try again.\n[sudo] password for ${username}: ` : `[sudo] password for ${username}: `; if (me.cachedPassword) { child.stdin.write(me.cachedPassword + '\n'); } else { me.cachedPassword = readlineSync.question(loginMessage, { hideEchoBack: true }); child.stdin.write(me.cachedPassword + '\n'); } } }); }); return child; } }; SpawnImpl = __decorate([ inversify_1.injectable(), __param(0, inversify_1.inject('CommandUtil')), __metadata('design:paramtypes', [Object]) ], SpawnImpl); exports.SpawnImpl = SpawnImpl; //# sourceMappingURL=spawn-impl.js.map