@tracker1/docker-cli
Version:
Docker cli interface
75 lines (63 loc) • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _child_process = require("child_process");
var _dockermachineCliJs = require("dockermachine-cli-js");
var _extractResult = _interopRequireDefault(require("./extract-result"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Run a docker command and return deserialized results
* @param {string} command the docker command to run
* @param {object} options Command options
* @param {string} options.machineName Use a configured docker-machine name
* @param {string} options.cwd Use a different working directory
* @param {bool} options.echo Echo the command output to stdio
*/
var _default = async (command, options = {}) => {
let machineconfig = '';
if (options.machineName) {
machineconfig = await new _dockermachineCliJs.DockerMachine().command(`config ${options.machineName}`).then(data => data.machine.config);
}
const execCommand = `docker ${machineconfig} ${command}`;
const execOptions = {
cwd: options.cwd,
env: {
DEBUG: '',
HOME: process.env.HOME,
PATH: process.env.PATH
},
maxBuffer: 200 * 1024 * 1024 // 200mb
};
const raw = await new Promise((resolve, reject) => {
const childProcess = (0, _child_process.exec)(execCommand, execOptions, (error, stdout, stderr) => {
if (error) {
return reject(Object.assign(new Error(`Error processing command`), { ...error,
stdout,
stderr,
innerError: error
}));
}
resolve(stdout);
});
if (options.echo) {
childProcess.stdout.on('data', chunk => {
process.stdout.write(chunk.toString());
});
childProcess.stderr.on('data', chunk => {
process.stderr.write(chunk.toString());
});
}
});
return (0, _extractResult.default)({
input: {
command,
options
},
command: execCommand,
raw
});
};
exports.default = _default;
module.exports = exports.default;