{"version":3,"file":"readTranscendYaml-bL1qFPqv.mjs","names":[],"sources":["../src/lib/readTranscendYaml.ts"],"sourcesContent":["import { readFileSync, writeFileSync } from 'node:fs';\n\nimport { decodeCodec, ObjByString } from '@transcend-io/type-utils';\nimport yaml from 'js-yaml';\n\nimport { TranscendInput } from '../codecs.js';\n\nexport const VARIABLE_PARAMETERS_REGEXP = /<<parameters\\.(.+?)>>/;\nexport const VARIABLE_PARAMETERS_NAME = 'parameters';\n\n/**\n * Function that replaces variables in a text file.\n * Throws error if there are variables that have not been replaced\n *\n * @param input - Input text\n * @param variables - Variables to replace\n * @param extraErrorMessage - Additional error message text\n * @returns Output text\n */\nexport function replaceVariablesInYaml(\n  input: string,\n  variables: ObjByString,\n  extraErrorMessage = '',\n): string {\n  let contents = input;\n  // Replace variables\n  Object.entries(variables).forEach(([name, value]) => {\n    contents = contents.split(`<<${VARIABLE_PARAMETERS_NAME}.${name}>>`).join(value);\n  });\n\n  // Throw error if unfilled variables\n  if (VARIABLE_PARAMETERS_REGEXP.test(contents)) {\n    const [, name] = VARIABLE_PARAMETERS_REGEXP.exec(contents) || [];\n    throw new Error(\n      `Found variable that was not set: ${name}.\nMake sure you are passing all parameters through the --${VARIABLE_PARAMETERS_NAME}=${name}:value-for-param flag.\n${extraErrorMessage}`,\n    );\n  }\n\n  return contents;\n}\n\n/**\n * Read in the contents of a yaml file and validate that the shape\n * of the yaml file matches the codec API\n *\n * @param filePath - Path to yaml file\n * @param variables - Variables to fill in\n * @returns The contents of the yaml file, type-checked\n */\nexport function readTranscendYaml(filePath: string, variables: ObjByString = {}): TranscendInput {\n  // Read in contents\n  const fileContents = readFileSync(filePath, 'utf-8');\n\n  // Replace variables\n  const replacedVariables = replaceVariablesInYaml(\n    fileContents,\n    variables,\n    `Also check that there are no extra variables defined in your yaml: ${filePath}`,\n  );\n\n  // Validate shape\n  return decodeCodec(TranscendInput, yaml.load(replacedVariables));\n}\n\n/**\n * Write a Transcend configuration to disk\n *\n * @param filePath - Path to yaml file\n * @param input - The input to write out\n */\nexport function writeTranscendYaml(filePath: string, input: TranscendInput): void {\n  writeFileSync(filePath, yaml.dump(decodeCodec(TranscendInput, input)));\n}\n"],"mappings":"mLAOA,MAAa,EAA6B,wBAC7B,EAA2B,aAWxC,SAAgB,EACd,EACA,EACA,EAAoB,GACZ,CACR,IAAI,EAAW,EAOf,GALA,OAAO,QAAQ,EAAU,CAAC,SAAS,CAAC,EAAM,KAAW,CACnD,EAAW,EAAS,MAAM,KAAK,EAAyB,GAAG,EAAK,IAAI,CAAC,KAAK,EAAM,EAChF,CAGE,EAA2B,KAAK,EAAS,CAAE,CAC7C,GAAM,EAAG,GAAQ,EAA2B,KAAK,EAAS,EAAI,EAAE,CAChE,MAAU,MACR,oCAAoC,EAAK;yDACU,EAAyB,GAAG,EAAK;EACxF,IACG,CAGH,OAAO,EAWT,SAAgB,EAAkB,EAAkB,EAAyB,EAAE,CAAkB,CAK/F,IAAM,EAAoB,EAHL,EAAa,EAAU,QAI9B,CACZ,EACA,sEAAsE,IACvE,CAGD,OAAO,EAAY,EAAgB,EAAK,KAAK,EAAkB,CAAC,CASlE,SAAgB,EAAmB,EAAkB,EAA6B,CAChF,EAAc,EAAU,EAAK,KAAK,EAAY,EAAgB,EAAM,CAAC,CAAC"}