import * as bedrock from ".." ; import { BedrockFile , Rings , HelmConfig } from "../../../types" ; import * as path from "path" ; import * as fs from "fs" ; import { logger } from "../../../logger" ; import yaml from "js-yaml" ; export interface LegacyBedrockService { displayName ? : string ; disableRouteScaffold ? : boolean ; helm : HelmConfig ; k8sBackend ? : string ; k8sBackendPort : number ; middlewares ? : string [ ] ; pathPrefix ? : string ; pathPrefixMajorVersion ? : string ; } /**
 * Bedrock config file
 * Used to capture service meta-information regarding how to deploy
 */ export interface LegacyBedrockFile { rings : Rings ; services : { [ path ] : LegacyBedrockService ; } ; variableGroups ? : string [ ] ; version : string ; } param legacy export const convertToNewSchema = ( legacy ) : BedrockFile => { const services = Object . entries ( legacy . services ) . map ( ( [ svcPath , config ] ) => ( { ... config , path : svcPath , } ) ) ; return { ... legacy , services , } ; } ; param b export function isLegacySchema ( b ) { return ! Array . isArray ( b . services ) ; } /**
 * Run the migration to new Bedrock schema.
 *
 * - Attempts to load the bedrock.yaml file and check the version it legacy
 * - If it is legacy, update it and overwrite
 */ export const migrate = ( b , bedrockDir ) : BedrockFile => { if ( isLegacySchema ( b ) ) { logger . info ( "Legacy bedrock.yaml schema found -- Migrating to new schema..." ) ; const bedrockPath = path . resolve ( path . join ( bedrockDir , bedrock . YAML_NAME ) ) ; fs . unlinkSync ( path . join ( bedrockDir , bedrock . YAML_NAME ) ) ; logger . info ( `Writing updated bedrock.yaml file to ${ bedrockPath }` ) ; const migrated = convertToNewSchema ( b ) ; bedrock . create ( bedrockDir , migrated ) ; logger . info ( `Successfully updated Bedrock config` ) ; return yaml . safeLoad ( fs . readFileSync ( bedrockPath , "utf8" ) ) ; } else { return b ; } } ;