import { BuildFile } from './build-file' ; import { dirname } from 'path' ; import { parseBuildFileReferences } from './parse-build-file-references' ; import { parseEnvs } from './parse-envs' ; import { readEnvFile } from './read-env-file' ; import { parseStringArray } from './parse-string-array' ; import { parseBuildFileTaskSource } from './parse-build-file-task-source' ; import { parseBuildFileCommand } from './parse-build-file-task-command' ; import { Environment } from '../executer/environment' ; const validTaskKeys = [ 'envs' , 'src' , 'deps' , 'generates' , 'description' , 'extend' , 'cmds' , 'watch' ] ; const validDockerTaskKeys = [ 'image' , 'mounts' , 'ports' , 'shell' , ... validTaskKeys ] ; export async function parseBuildFile ( fileName , files , input , context ) { const result = { fileName , includes : { } , tasks : { } , path : dirname ( fileName ) , references : { } , envs : { } , } ; files [ fileName ] = result ; if ( typeof input !== 'object' ) { throw new Error ( `${ fileName } build file needs to be an object` ) ; } if ( ! input ) { return result ; } result . envs = await readEnvFile ( dirname ( fileName ) , result . envs , context ) ; for ( const [ key , value ] of Object . entries ( input ) ) { if ( key === 'includes' ) { result . includes = await parseBuildFileReferences ( 'include' , fileName , files , value || { } , context ) ; } else if ( key === 'references' ) { result . references = await parseBuildFileReferences ( 'reference' , fileName , files , value || { } , context ) ; } else if ( key === 'envs' ) { result . envs = parseEnvs ( fileName , value || { } , result . envs ) ; } } const tasks = 'tasks' in input ? ( input as any ) [ 'tasks' ] : null ; if ( tasks && typeof tasks !== 'object' ) { throw new Error ( `${ fileName } tasks need to be an object` ) ; } for ( const key of Object . keys ( tasks || { } ) ) { const value = tasks [ key ] ; if ( typeof value !== 'object' ) { throw new Error ( `${ fileName } task ${ key } needs to be an object` ) ; } const validKeys = value . image ? validDockerTaskKeys : validTaskKeys ; result . tasks [ key ] = { envs : parseEnvs ( fileName , value . envs || { } , { } ) , mounts : parseStringArray ( fileName , key , 'mounts' , value . mounts ) , src : parseBuildFileTaskSource ( fileName , key , value . src , context ) , deps : parseStringArray ( fileName , key , 'deps' , value . deps ) , generates : parseStringArray ( fileName , key , 'generates' , value . generates ) , description : value . description ? value . description . trim ( ) : null , image : value . image || null , extend : value . extend || null , shell : value . shell || null , ports : parseStringArray ( fileName , key , 'ports' , value . ports ) , cmds : parseBuildFileCommand ( fileName , key , value . cmds ) , unknownProps : Object . keys ( value ) . filter ( ( k ) => validKeys . indexOf ( k ) === - 1 ) . reduce < { [ key ] : any ; } > ( ( map , k ) => { map [ k ] = value [ k ] ; return map ; } , { } ) , platform : null , continuous : null , } ; if ( Object . keys ( result . tasks [ key ] . unknownProps ) . length > 0 ) { context . console . warn ( `unknown props ${ Object . keys ( result . tasks [ key ] . unknownProps ) } for ${ key } in ${ fileName }` ) ; } } return result ; }