Skip to content

Commit

Permalink
feat: allow to pass docker compose configuration as string
Browse files Browse the repository at this point in the history
  • Loading branch information
furstenheim authored and AlexZeitler committed Jan 6, 2021
1 parent 4983b2b commit e8c14d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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[])[];
Expand Down Expand Up @@ -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;

const configArgs = isConfigProvidedAsString ? [ '-f', '-' ] : configToArgs(options.config);

composeArgs = composeArgs.concat(configToArgs(options.config).concat([ command ].concat(composeOptionsToArgs(commandOptions), args)));
composeArgs = composeArgs.concat(configArgs.concat([ command ].concat(composeOptionsToArgs(commandOptions), args)));

const cwd = options.cwd;
const env = options.env || undefined;
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 22 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit e8c14d3

Please sign in to comment.