|  | 
|  | 1 | +import { existsSync, readFileSync } from 'node:fs'; | 
|  | 2 | +import { ServerlessIac, ServerlessIacRaw } from '../types'; | 
|  | 3 | +import { parseFunction } from './functionParser'; | 
|  | 4 | +import { parseEvent } from './eventParser'; | 
|  | 5 | +import { parseDatabase } from './databaseParser'; | 
|  | 6 | +import { parseTag } from './tagParser'; | 
|  | 7 | +import { parse } from 'yaml'; | 
|  | 8 | +import { validateYaml } from '../validator'; | 
|  | 9 | + | 
|  | 10 | +const validateExistence = (path: string) => { | 
|  | 11 | +  if (!existsSync(path)) { | 
|  | 12 | +    throw new Error(`File does not exist at path: ${path}`); | 
|  | 13 | +  } | 
|  | 14 | +}; | 
|  | 15 | + | 
|  | 16 | +const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => { | 
|  | 17 | +  return { | 
|  | 18 | +    service: iacJson.service, | 
|  | 19 | +    version: iacJson.version, | 
|  | 20 | +    provider: iacJson.provider, | 
|  | 21 | +    vars: iacJson.vars, | 
|  | 22 | +    stages: iacJson.stages, | 
|  | 23 | +    functions: parseFunction(iacJson.functions), | 
|  | 24 | +    events: parseEvent(iacJson.events), | 
|  | 25 | +    databases: parseDatabase(iacJson.databases), | 
|  | 26 | +    tags: parseTag(iacJson.tags), | 
|  | 27 | +  }; | 
|  | 28 | +}; | 
|  | 29 | + | 
|  | 30 | +export const parseYaml = (yamlPath: string): ServerlessIac => { | 
|  | 31 | +  validateExistence(yamlPath); | 
|  | 32 | + | 
|  | 33 | +  const yamlContent = readFileSync(yamlPath, 'utf8'); | 
|  | 34 | +  const iacJson = parse(yamlContent) as ServerlessIacRaw; | 
|  | 35 | + | 
|  | 36 | +  validateYaml(iacJson); | 
|  | 37 | + | 
|  | 38 | +  return transformYaml(iacJson); | 
|  | 39 | +}; | 
0 commit comments