Skip to content

Commit

Permalink
feat(generic): add env var support to generic module type
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jul 2, 2018
1 parent b54fd67 commit a5096ee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/plugins/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as childProcess from "child-process-promise"
import {
Module,
ModuleConfig,
ModuleSpec,
} from "../types/module"
import { LogSymbolType } from "../logger/types"
import {
Expand Down Expand Up @@ -46,6 +47,7 @@ import {
genericModuleSpecSchema,
GenericModuleSpec,
GenericTestSpec,
genericTestSchema,
} from "./generic"

export interface ServiceEndpointSpec {
Expand Down Expand Up @@ -176,15 +178,16 @@ const serviceSchema = baseServiceSchema
.description("List of volumes that should be mounted when deploying the container."),
})

export interface ContainerModuleSpec extends GenericModuleSpec {
export interface ContainerModuleSpec extends ModuleSpec {
buildArgs: PrimitiveMap,
image?: string,
services: ContainerServiceSpec[],
tests: GenericTestSpec[],
}

export type ContainerModuleConfig = ModuleConfig<ContainerModuleSpec>

export const containerModuleSpecSchema = genericModuleSpecSchema
export const containerModuleSpecSchema = Joi.object()
.keys({
buildArgs: Joi.object()
.pattern(/.+/, joiPrimitive())
Expand All @@ -200,6 +203,8 @@ export const containerModuleSpecSchema = genericModuleSpecSchema
services: joiArray(serviceSchema)
.unique("name")
.description("List of services to deploy from this container module."),
tests: joiArray(genericTestSchema)
.description("A list of tests to run in the module."),
})
.description("Configuration for a container module.")

Expand Down
17 changes: 14 additions & 3 deletions src/plugins/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { exec } from "child-process-promise"
import * as Joi from "joi"
import {
joiArray,
joiEnvVars,
PrimitiveMap,
validate,
} from "../types/common"
import {
Expand Down Expand Up @@ -55,11 +57,13 @@ export const genericTestSchema = baseTestSpecSchema
.description("The test specification of a generic module.")

export interface GenericModuleSpec extends ModuleSpec {
env: PrimitiveMap,
tests: GenericTestSpec[],
}

export const genericModuleSpecSchema = Joi.object()
.keys({
env: joiEnvVars(),
tests: joiArray(genericTestSchema)
.description("A list of tests to run in the module."),
})
Expand All @@ -86,7 +90,7 @@ export async function parseGenericModule(
}
}

export async function buildGenericModule({ module }: BuildModuleParams): Promise<BuildResult> {
export async function buildGenericModule({ module }: BuildModuleParams<GenericModule>): Promise<BuildResult> {
// By default we run the specified build command in the module root, if any.
// TODO: Keep track of which version has been built (needs local data store/cache).
const config: ModuleConfig = module.config
Expand All @@ -95,7 +99,7 @@ export async function buildGenericModule({ module }: BuildModuleParams): Promise
const buildPath = await module.getBuildPath()
const result = await exec(config.build.command, {
cwd: buildPath,
env: { ...process.env },
env: { ...process.env, ...module.spec.env },
})

return {
Expand All @@ -110,8 +114,15 @@ export async function buildGenericModule({ module }: BuildModuleParams): Promise
export async function testGenericModule({ module, testConfig }: TestModuleParams<GenericModule>): Promise<TestResult> {
const startedAt = new Date()
const command = testConfig.spec.command

const result = await spawn(
command[0], command.slice(1), { cwd: module.path, ignoreError: true },
command[0],
command.slice(1),
{
cwd: module.path,
env: { ...process.env, ...module.spec.env },
ignoreError: true,
},
)

return {
Expand Down
6 changes: 4 additions & 2 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import highlight from "cli-highlight"
import chalk from "chalk"
import hasAnsi = require("has-ansi")
import { safeDump } from "js-yaml"
import { PrimitiveMap } from "../types/common"

// shim to allow async generator functions
if (typeof (Symbol as any).asyncIterator === "undefined") {
Expand Down Expand Up @@ -148,6 +149,7 @@ export interface SpawnParams {
cwd?: string
data?: Buffer
ignoreError?: boolean
env?: object
}

export interface SpawnPtyParams extends SpawnParams {
Expand All @@ -166,9 +168,9 @@ export interface SpawnOutput {

export function spawn(
cmd: string, args: string[],
{ timeout = 0, cwd, data, ignoreError = false }: SpawnParams = {},
{ timeout = 0, cwd, data, ignoreError = false, env }: SpawnParams = {},
) {
const proc = _spawn(cmd, args, { cwd })
const proc = _spawn(cmd, args, { cwd, env })

const result: SpawnOutput = {
code: 0,
Expand Down

0 comments on commit a5096ee

Please sign in to comment.