diff --git a/src/index.ts b/src/index.ts index 637c4d5c6..cc4fe1728 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; + + 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; @@ -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); diff --git a/test/index.test.ts b/test/index.test.ts index dc7158379..d088c18d0 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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 => { + 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: 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 => { const opts = { cwd: path.join(__dirname),