From feeff5fa20417fd8a3f41c1cbbc6445a7f9106f4 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Mon, 3 Feb 2020 09:41:42 +0100 Subject: [PATCH] feat(dockest): support function as commands for injecting the containerId into the command. (#133) * feat(dockest): support function as commands for injecting the containerId into the command. This makes it much easier to run commands inside your container, e.g. a database dump import. * test: add example for using a command with the injected container id. --- packages/dockest/src/@types.ts | 2 +- .../dockest/src/run/waitForServices/runRunnerCommands.ts | 5 ++++- packages/examples/multiple-resources/dockest.ts | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/dockest/src/@types.ts b/packages/dockest/src/@types.ts index eb75051e..3334ae33 100644 --- a/packages/dockest/src/@types.ts +++ b/packages/dockest/src/@types.ts @@ -63,7 +63,7 @@ export interface DockerComposeFileServicePostgres extends DockerComposeFileServi } } -export type Commands = string[] +export type Commands = (string | ((containerId: string) => string))[] export interface DockestService { serviceName: ServiceName diff --git a/packages/dockest/src/run/waitForServices/runRunnerCommands.ts b/packages/dockest/src/run/waitForServices/runRunnerCommands.ts index b22045e5..ad1bb350 100644 --- a/packages/dockest/src/run/waitForServices/runRunnerCommands.ts +++ b/packages/dockest/src/run/waitForServices/runRunnerCommands.ts @@ -4,7 +4,10 @@ import { Runner } from '../../@types' const logPrefix = '[Dockest Service Commands]' export const runRunnerCommands = async ({ runner, runner: { commands } }: { runner: Runner }) => { - for (const command of commands) { + for (let command of commands) { + if (typeof command === 'function') { + command = command(runner.containerId) + } await execaWrapper(command, { runner, logPrefix, logStdout: true }) } } diff --git a/packages/examples/multiple-resources/dockest.ts b/packages/examples/multiple-resources/dockest.ts index 14b88114..365c7b41 100644 --- a/packages/examples/multiple-resources/dockest.ts +++ b/packages/examples/multiple-resources/dockest.ts @@ -18,6 +18,7 @@ dockest.run([ 'sequelize db:migrate', 'sequelize db:seed:undo:all', 'sequelize db:seed --seed 20190101001337-demo-user', + containerId => `echo "The container id is ${containerId}"`, ], healthcheck: ({ defaultHealthchecks: { postgres } }) => postgres(), },