pooliot-client
Version:
157 lines (104 loc) • 5.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.runPipeAsUser = exports.runPipe = exports.runAsyncAsUser = exports.runAsync = exports.runSyncAsUser = exports.runSyncOk = exports.runSync = undefined;
var _child_process = require('child_process');
var _nightingale = require('nightingale');
var _nightingale2 = _interopRequireDefault(_nightingale);
var _argv = require('../argv');
var _flowRuntime = require('flow-runtime');
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const logger = new _nightingale2.default('app:exec');
const scriptDirname = `${__dirname}/../../scripts/`;
const StdioOptionValueType = _flowRuntime2.default.type('StdioOptionValueType', _flowRuntime2.default.union(_flowRuntime2.default.string('ignore'), _flowRuntime2.default.string('pipe'), _flowRuntime2.default.string('inherit')));
const StdioOptionType = _flowRuntime2.default.type('StdioOptionType', _flowRuntime2.default.union(StdioOptionValueType, _flowRuntime2.default.array(_flowRuntime2.default.union(StdioOptionValueType, _flowRuntime2.default.any()))));
const RunOptionsType = _flowRuntime2.default.type('RunOptionsType', _flowRuntime2.default.exactObject(_flowRuntime2.default.property('cwd', _flowRuntime2.default.nullable(_flowRuntime2.default.string())), _flowRuntime2.default.property('env', _flowRuntime2.default.nullable(_flowRuntime2.default.object())), _flowRuntime2.default.property('stdio', _flowRuntime2.default.nullable(StdioOptionType))));
const ArgsType = _flowRuntime2.default.type('ArgsType', _flowRuntime2.default.array(_flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.number())));
const escapeArgs = args => args.map(arg => typeof arg !== 'string' ? arg : `"${arg.replace('"', '\\"')}"`).join(' ');
const runSync = exports.runSync = (script, args = [], _arg = {}) => {
let _scriptType = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
let { cwd, env } = RunOptionsType.assert(_arg);
logger.debug('run', { script, args, cwd, env });
const result = (0, _child_process.spawnSync)(script, args, {
cwd: cwd || scriptDirname,
env: env && Object.assign({}, process.env, env)
});
if (result.error) {
logger.error(result.error.message);
}
if (result.stderr) {
const stderr = result.stderr.toString();
if (stderr) {
logger.error(stderr, { script, args, env });
}
}
if (result.stdout) {
const stdout = result.stdout.toString().trim();
logger.debug(stdout, { script, args });
return stdout;
}
};
const runSyncOk = exports.runSyncOk = (script, args = [], _arg2 = {}) => {
let _scriptType2 = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType2).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
let { cwd, env } = RunOptionsType.assert(_arg2);
const result = (0, _child_process.spawnSync)(script, args, { cwd: cwd || scriptDirname, env });
return !result.status;
};
const runSyncAsUser = exports.runSyncAsUser = (script, args, _arg3 = {}) => {
let _scriptType3 = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType3).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
let { cwd, env } = RunOptionsType.assert(_arg3);
if (env) throw new Error('env not supported in runSyncAsUser');
return runSync('su', ['-c', `${script} ${escapeArgs(args)}`, _argv.user], { cwd });
};
const runAsync = exports.runAsync = (script, args, _arg4 = {}) => {
let _scriptType4 = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType4).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
let { cwd, env } = RunOptionsType.assert(_arg4);
logger.info('exec', { script, args });
const childProcess = (0, _child_process.spawn)(script, args, { cwd: cwd || scriptDirname, env, stdio: 'pipe' });
childProcess.stdout.on('data', data => logger.debug(data.toString()));
childProcess.stderr.on('data', data => logger.error(data.toString()));
return childProcess;
};
const runAsyncAsUser = exports.runAsyncAsUser = (script, args) => {
let _scriptType5 = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType5).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
return runAsync('su', ['-c', `${script} ${escapeArgs(args)}`, _argv.user]);
};
const runPipe = exports.runPipe = (script, args, _arg5 = {}) => {
let _scriptType6 = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType6).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
let { cwd, env } = RunOptionsType.assert(_arg5);
logger.debug('pipe', { script, args, cwd, env });
const result = (0, _child_process.spawnSync)(script, args, { cwd: cwd || scriptDirname, env });
if (result.error) {
logger.error(result.error.message);
return null;
}
if (result.stderr) {
const stderr = result.stderr.toString();
if (stderr) {
logger.error(stderr, { script, args });
return null;
}
}
return result.stdout;
};
const runPipeAsUser = exports.runPipeAsUser = (script, args) => {
let _scriptType7 = _flowRuntime2.default.string();
_flowRuntime2.default.param('script', _scriptType7).assert(script);
_flowRuntime2.default.param('args', ArgsType).assert(args);
return runPipe('su', ['-c', `${script} ${escapeArgs(args)}`, _argv.user]);
};
//# sourceMappingURL=exec.js.map