mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-24 12:35:32 +00:00
Changed build function names, fixed execCompose which was not resolving its promise, fixed issue with run/exec with the spawn change
This commit is contained in:
parent
b20a9bdb6c
commit
dc38a6f52f
2 changed files with 65 additions and 31 deletions
86
index.js
86
index.js
|
|
@ -29,19 +29,31 @@ const configToArgs = config => {
|
||||||
* @param {?object} [options.env]
|
* @param {?object} [options.env]
|
||||||
*/
|
*/
|
||||||
const execCompose = (command, args, options) => new Promise((resolve, reject) => {
|
const execCompose = (command, args, options) => new Promise((resolve, reject) => {
|
||||||
const composeArgs = configToArgs(options.config);
|
const composeArgs = configToArgs(options.config).concat([ command ], args);
|
||||||
const cwd = options.cwd;
|
const cwd = options.cwd;
|
||||||
const env = options.env || null;
|
const env = options.env || null;
|
||||||
|
|
||||||
const childProc = childProcess.spawn('docker-compose', composeArgs.concat([ command ], args), { cwd, env }, (err, stdout, stderr) => {
|
const childProc = childProcess.spawn('docker-compose', composeArgs, { cwd, env });
|
||||||
if (err) {
|
|
||||||
|
childProc.on('error', err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
|
||||||
resolve({
|
|
||||||
err: stderr,
|
|
||||||
out: stdout
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
const result = {
|
||||||
|
err: '',
|
||||||
|
out: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
childProc.stdout.on('data', chunk => {
|
||||||
|
result.out += chunk.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
childProc.stderr.on('data', chunk => {
|
||||||
|
result.err += chunk.toString();
|
||||||
|
});
|
||||||
|
|
||||||
|
childProc.on('close', () => {
|
||||||
|
resolve(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (options.log) {
|
if (options.log) {
|
||||||
|
|
@ -118,7 +130,9 @@ const rm = function (options) {
|
||||||
* @return {object} std.out / std.err
|
* @return {object} std.out / std.err
|
||||||
*/
|
*/
|
||||||
const exec = function (container, command, options) {
|
const exec = function (container, command, options) {
|
||||||
return execCompose('exec', [ '-T', container, command ], options);
|
const args = command.split(/\s+/);
|
||||||
|
|
||||||
|
return execCompose('exec', [ '-T', container ].concat(args), options);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -134,33 +148,53 @@ const exec = function (container, command, options) {
|
||||||
* @return {object} std.out / std.err
|
* @return {object} std.out / std.err
|
||||||
*/
|
*/
|
||||||
const run = function (container, command, options) {
|
const run = function (container, command, options) {
|
||||||
return execCompose('run', [ '-T', container, command ], options);
|
const args = command.split(/\s+/);
|
||||||
|
|
||||||
|
return execCompose('run', [ '-T', container ].concat(args), options);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build command
|
* Build command
|
||||||
* @param {string|string[]|object} service service name
|
|
||||||
* @param {object} options
|
* @param {object} options
|
||||||
* @param {string} options.cwd
|
* @param {string} options.cwd
|
||||||
* @param {boolean} [options.log]
|
* @param {boolean} [options.log]
|
||||||
* @param {?(string|string[])} [options.config]
|
* @param {?(string|string[])} [options.config]
|
||||||
|
* @param {?object} [options.env]
|
||||||
*
|
*
|
||||||
* @return {object} std.out / std.err
|
* @return {object} std.out / std.err
|
||||||
*/
|
*/
|
||||||
const build = function (service, options) {
|
const buildAll = function (options) {
|
||||||
let services = [];
|
return execCompose('build', [], options);
|
||||||
|
|
||||||
if (Array.isArray(service)) {
|
|
||||||
services = service;
|
|
||||||
} else if (typeof service === 'object') {
|
|
||||||
options = service;
|
|
||||||
} else {
|
|
||||||
services = [ service || '' ];
|
|
||||||
}
|
|
||||||
|
|
||||||
services = services.join(' ').trim();
|
|
||||||
|
|
||||||
return execCompose(`build ${services}`, options);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { up, kill, down, stop, rm, exec, run, build };
|
/**
|
||||||
|
* Build command
|
||||||
|
* @param {string[]} services list of service names
|
||||||
|
* @param {object} options
|
||||||
|
* @param {string} options.cwd
|
||||||
|
* @param {boolean} [options.log]
|
||||||
|
* @param {?(string|string[])} [options.config]
|
||||||
|
* @param {?object} [options.env]
|
||||||
|
*
|
||||||
|
* @return {object} std.out / std.err
|
||||||
|
*/
|
||||||
|
const buildMany = function (services, options) {
|
||||||
|
return execCompose('build', services, options);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build command
|
||||||
|
* @param {string} service service name
|
||||||
|
* @param {object} options
|
||||||
|
* @param {string} options.cwd
|
||||||
|
* @param {boolean} [options.log]
|
||||||
|
* @param {?(string|string[])} [options.config]
|
||||||
|
* @param {?object} [options.env]
|
||||||
|
*
|
||||||
|
* @return {object} std.out / std.err
|
||||||
|
*/
|
||||||
|
const buildOne = function (service, options) {
|
||||||
|
return execCompose('build', [ service ], options);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { up, kill, down, stop, rm, exec, run, buildAll, buildMany, buildOne };
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ test('build single service', async assert => {
|
||||||
|
|
||||||
await removeImagesStartingWith('compose-test-build-image');
|
await removeImagesStartingWith('compose-test-build-image');
|
||||||
|
|
||||||
await compose.build('build_test_1', opts);
|
await compose.buildOne('build_test_1', opts);
|
||||||
|
|
||||||
assert.true(await imageExists('compose-test-build-image-1:test'));
|
assert.true(await imageExists('compose-test-build-image-1:test'));
|
||||||
assert.false(await imageExists('compose-test-build-image-2:test'));
|
assert.false(await imageExists('compose-test-build-image-2:test'));
|
||||||
|
|
@ -148,7 +148,7 @@ test('build multiple services', async assert => {
|
||||||
config: 'docker-compose-build.yml'
|
config: 'docker-compose-build.yml'
|
||||||
};
|
};
|
||||||
|
|
||||||
await compose.build([ 'build_test_2', 'build_test_3' ], opts);
|
await compose.buildMany([ 'build_test_2', 'build_test_3' ], opts);
|
||||||
|
|
||||||
assert.false(await imageExists('compose-test-build-image-1:test'));
|
assert.false(await imageExists('compose-test-build-image-1:test'));
|
||||||
assert.true(await imageExists('compose-test-build-image-2:test'));
|
assert.true(await imageExists('compose-test-build-image-2:test'));
|
||||||
|
|
@ -167,7 +167,7 @@ test('build all services', async assert => {
|
||||||
config: 'docker-compose-build.yml'
|
config: 'docker-compose-build.yml'
|
||||||
};
|
};
|
||||||
|
|
||||||
await compose.build(opts);
|
await compose.buildAll(opts);
|
||||||
|
|
||||||
assert.true(await imageExists('compose-test-build-image-1:test'));
|
assert.true(await imageExists('compose-test-build-image-1:test'));
|
||||||
assert.true(await imageExists('compose-test-build-image-2:test'));
|
assert.true(await imageExists('compose-test-build-image-2:test'));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue