feat: allow to pass docker compose configuration as string

This commit is contained in:
Gabriel 2021-01-03 15:49:36 +01:00 committed by Alexander Zeitler
parent 4983b2b8dd
commit e8c14d318c
2 changed files with 33 additions and 3 deletions

View file

@ -1,8 +1,9 @@
import childProcess from 'child_process';
import { Readable } from 'stream'
export interface IDockerComposeOptions {
cwd?: string;
config?: string | string[];
configAsString?: string;
log?: boolean;
composeOptions?: string[] | (string | string[])[];
commandOptions?: string[] | (string | string[])[];
@ -67,8 +68,11 @@ const execCompose = (command, args, options: IDockerComposeOptions = {}): Promis
const composeOptions = options.composeOptions || [];
const commandOptions = options.commandOptions || [];
let composeArgs = composeOptionsToArgs(composeOptions);
const isConfigProvidedAsString = !!options.configAsString;
composeArgs = composeArgs.concat(configToArgs(options.config).concat([ command ].concat(composeOptionsToArgs(commandOptions), args)));
const configArgs = isConfigProvidedAsString ? [ '-f', '-' ] : configToArgs(options.config);
composeArgs = composeArgs.concat(configArgs.concat([ command ].concat(composeOptionsToArgs(commandOptions), args)));
const cwd = options.cwd;
const env = options.env || undefined;
@ -102,6 +106,11 @@ const execCompose = (command, args, options: IDockerComposeOptions = {}): Promis
}
});
if (isConfigProvidedAsString) {
childProc.stdin.write(options.configAsString);
childProc.stdin.end();
}
if (options.log) {
childProc.stdout.pipe(process.stdout);
childProc.stderr.pipe(process.stderr);

View file

@ -1,7 +1,7 @@
import Docker from 'dockerode';
import * as compose from '../src/index';
import * as path from 'path';
import { readFile } from 'fs'
const docker = new Docker();
// Docker commands, especially builds, can take some time. This makes sure that they can take the time they need.
@ -302,6 +302,27 @@ test('ensure run and exec with command defined as array are working', async ():
await compose.down({ cwd: path.join(__dirname), log: logOutput });
});
test('build accepts config as string', async (): Promise<void> => {
const configuration = await new Promise(function (resolve, reject) {
readFile(path.join(__dirname, 'docker-compose-2.yml'), function (err, content) {
if (err) {
return reject(err);
}
return resolve(content.toString());
})
});
const config = {
configAsString: <string> configuration,
log: logOutput
};
await compose.upAll(config);
const port = await compose.port('db', 5432, config);
expect(port.out).toMatch(/.*:[0-9]{1,5}/);
await compose.down(config);
});
test('build single service', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),