import spawn , { ProcessError , Mode } from "./spawn" ; import { FinalConfig , FinalTask } from "./config" ; import path from "path" ; let DOCKERFILES = path . resolve ( __dirname , "dockerfiles" ) ; let TESTER_DOCKERFILE = path . join ( DOCKERFILES , "tester.Dockerfile" ) ; export enum Target { Builder = "builder" , Tester = "tester" } export function findTask ( config , taskName ) { let found = config . tasks . find ( task => task . taskName === taskName ) ; if ( ! found ) { throw new Error ( `Could not find a task named "${ taskName }"` ) ; } return found ; } export async function buildTask ( task , mode , cwd ) { await spawn ( "docker" , [ "build" , task . buildContext , `--build-arg=cwd=${ cwd }` , ... task . buildArgs . map ( buildArg => { return `--build-arg=${ buildArg }` ; } ) , `--tag=${ getTag ( task , Target . Builder ) }` , ] , { interactive : false , mode , } ) ; } export async function buildTaskTester ( task , mode , cwd ) { await buildTask ( task , mode , cwd ) ; await spawn ( "docker" , [ "build" , task . buildContext , `--file=${ TESTER_DOCKERFILE }` , `--build-arg=baseImage=${ getTag ( task , Target . Builder ) }` , `--build-arg=testFiles=${ getTestFilesGlob ( task ) }` , `--tag=${ getTag ( task , Target . Tester ) }` , ] , { interactive : false , mode , } ) ; } export async function runTask ( task , target , cwd ) { await spawn ( "docker" , [ "run" , `--name=${ task . taskName }` , "--rm" , "--network=host" , ... ( target === Target . Builder ? [ "--interactive" ] : [ ] ) , "--tty" , ... ( target === Target . Builder ? [ `--volume=${ cwd }:/workdir` ] : [ ] ) , getTag ( task , target ) , ... ( target === Target . Builder ? task . args : [ ] ) , ] , { interactive : true , mode : Mode . Loud , } ) ; } export async function shellTask ( task , target , cwd ) { try { await spawn ( "docker" , [ "run" , `--name=${ task . taskName }` , "--rm" , "--interactive" , "--tty" , "--network=host" , ... ( target === Target . Builder ? [ `--volume=${ cwd }:/workdir` ] : [ ] ) , "--entrypoint=/bin/bash" , getTag ( task , target ) , ] , { interactive : true , mode : Mode . Loud , } ) ; } catch ( err ) { if ( err instanceof ProcessError && err . code === 130 ) { return ; } else { throw err ; } } } function getTag ( task , target ) { return `naw-${ target }-${ task . taskName }:latest` ; } function getTestFilesGlob ( task ) { return path . join ( task . buildContext , "*.bats" ) ; }