node-op
Version:
Interactive 1Password CLI and installer
156 lines (124 loc) • 5.05 kB
JavaScript
;
var require$$1 = require('child_process');
var os = require('os');
var util = require('util');
function spawnAndCheck(command, args, options) {
var _options$expectedExit, _options$appendOutput, _options$chooseReturn;
const expectedExitCodes = (_options$expectedExit = options === null || options === void 0 ? void 0 : options.expectedExitCodes) !== null && _options$expectedExit !== void 0 ? _options$expectedExit : [0];
const appendOutputToError = (_options$appendOutput = options === null || options === void 0 ? void 0 : options.appendOutputToError) !== null && _options$appendOutput !== void 0 ? _options$appendOutput : false;
if (options.verbosity > 0) {
console.log([command, ...(args !== null && args !== void 0 ? args : [])].join(' '));
}
const proc = require$$1.spawn(command, args !== null && args !== void 0 ? args : [], options !== null && options !== void 0 ? options : {});
if (options.created) {
options.created(proc);
}
const output = [];
if (proc.stderr) {
proc.stderr.setEncoding('utf8');
proc.stderr.on('data', chunk => {
output.push(chunk);
if (options.verbosity > 1) {
console.error(chunk);
}
});
}
if (proc.stdout) {
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', chunk => {
output.push(chunk);
if (options.verbosity > 1) {
console.log(chunk);
}
});
}
const chooseReturn = (_options$chooseReturn = options.chooseReturn) !== null && _options$chooseReturn !== void 0 ? _options$chooseReturn : (_code, _signal, out) => out;
return new Promise((res, rej) => {
const exitHandler = (code, signal) => {
if (typeof code !== 'number' || !expectedExitCodes.includes(code)) {
const additional = appendOutputToError ? `${os.EOL}${output.join('').trim()}` : '';
if (typeof code === 'number') {
rej(new Error(`process "${command}" quit with non-zero code: ${code}${additional}`));
} else {
rej(new Error(`process "${command}" was terminated with ${String(signal)}${additional}`));
}
} else {
res(chooseReturn(code, signal, output.join('')));
}
};
proc.on('error', err => {
rej(err);
});
proc.on('exit', exitHandler);
});
}
async function listItems(props) {
var _props$verbosity;
const result = await spawnAndCheck('op', ['list', 'items', typeof (props === null || props === void 0 ? void 0 : props.includeTrash) === 'boolean' && '--include-trash', (props === null || props === void 0 ? void 0 : props.vault) && `--vault=${props.vault}`].filter(util.isString), {
env: process.env,
verbosity: (_props$verbosity = props === null || props === void 0 ? void 0 : props.verbosity) !== null && _props$verbosity !== void 0 ? _props$verbosity : 0,
stdio: ['inherit', 'pipe', 'pipe'],
appendOutputToError: true
});
return JSON.parse(result);
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
const innerErrorsToMessage = (pad, ...errors) => {
return errors.reduce((acc, val) => {
const valStr = // tslint:disable-next-line:strict-string-expressions
val instanceof AggregateError ? val.description.trim() : `${String(val)}`.trim();
return acc + `${pad}${valStr}`.replace('\n', `\n${pad}`) + '\n';
}, '');
};
class AggregateError extends Error {
constructor(message, ...innerErrors) {
const errors = [message, ...innerErrors].filter(util.isError);
const msg = [util.isError(message) ? 'Multiple errors occured' : message, `\n${innerErrorsToMessage(' ', ...errors)}`].filter(util.isString).join('').trim();
super(msg);
_defineProperty(this, "description", void 0);
_defineProperty(this, "innerErrors", void 0);
this.name = 'AggregateError';
this.innerErrors = errors;
this.description = ['AggregateError', msg].filter(Boolean).join(': ');
}
}
function ensureError(err) {
// tslint:disable-next-line:strict-boolean-expressions
if (!err) {
// tslint:disable-next-line: strict-string-expressions
return new Error(`Invalid error '${String(err)}' thrown`);
}
return typeof err === 'object' && err instanceof Error ? err : // tslint:disable-next-line: strict-string-expressions
new Error(`Invalid error '${String(err)}' thrown`);
}
exports.AggregateError = AggregateError;
exports._extends = _extends;
exports.ensureError = ensureError;
exports.listItems = listItems;
exports.spawnAndCheck = spawnAndCheck;