{"version":3,"file":"writeYamlFile.cjs","names":["detectFormatCommand"],"sources":["../../../src/writeContentDeclaration/writeYamlFile.ts"],"sourcesContent":["import { execSync } from 'node:child_process';\nimport { mkdir, rename, rm, writeFile } from 'node:fs/promises';\nimport { basename, dirname, join } from 'node:path';\nimport { stringifyYaml } from '@intlayer/core/utils';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport { detectFormatCommand } from '../detectFormatCommand';\n\n// Fields that are auto-generated or runtime-only, not persisted in the file\nconst EXCLUDED_YAML_KEYS = new Set<string>(['$schema', 'id', 'filePath']);\n\nexport const writeYamlFile = async (\n  absoluteFilePath: string,\n  dictionary: Dictionary,\n  configuration: IntlayerConfig\n): Promise<void> => {\n  const filtered = Object.fromEntries(\n    Object.entries(dictionary).filter(\n      ([k, v]) => !EXCLUDED_YAML_KEYS.has(k) && v !== undefined\n    )\n  );\n\n  const fileContent = stringifyYaml(filtered);\n\n  const dir = dirname(absoluteFilePath);\n  await mkdir(dir, { recursive: true });\n\n  const tempDir = configuration.system?.tempDir;\n  if (tempDir) await mkdir(tempDir, { recursive: true });\n\n  const tempFileName = `${basename(absoluteFilePath)}.${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`;\n  const tempPath = tempDir\n    ? join(tempDir, tempFileName)\n    : `${absoluteFilePath}.${tempFileName}`;\n\n  try {\n    await writeFile(tempPath, fileContent, 'utf-8');\n    await rename(tempPath, absoluteFilePath);\n  } catch (error) {\n    try {\n      await rm(tempPath, { force: true });\n    } catch {\n      // ignore\n    }\n    throw error;\n  }\n\n  const formatCommand = detectFormatCommand(configuration);\n  if (formatCommand) {\n    try {\n      execSync(formatCommand.replace('{{file}}', absoluteFilePath), {\n        stdio: 'inherit',\n        cwd: configuration.system.baseDir,\n      });\n    } catch (error) {\n      console.error(error);\n    }\n  }\n};\n"],"mappings":";;;;;;;;;AASA,MAAM,qBAAqB,IAAI,IAAY;CAAC;CAAW;CAAM;AAAU,CAAC;AAExE,MAAa,gBAAgB,OAC3B,kBACA,YACA,kBACkB;CAOlB,MAAM,sDANW,OAAO,YACtB,OAAO,QAAQ,UAAU,EAAE,QACxB,CAAC,GAAG,OAAO,CAAC,mBAAmB,IAAI,CAAC,KAAK,MAAM,MAClD,CAGuC,CAAC;CAG1C,yDADoB,gBACN,GAAG,EAAE,WAAW,KAAK,CAAC;CAEpC,MAAM,UAAU,cAAc,QAAQ;CACtC,IAAI,SAAS,kCAAY,SAAS,EAAE,WAAW,KAAK,CAAC;CAErD,MAAM,eAAe,2BAAY,gBAAgB,EAAE,GAAG,KAAK,IAAI,EAAE,GAAG,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC,EAAE;CACxG,MAAM,WAAW,8BACR,SAAS,YAAY,IAC1B,GAAG,iBAAiB,GAAG;CAE3B,IAAI;EACF,sCAAgB,UAAU,aAAa,OAAO;EAC9C,mCAAa,UAAU,gBAAgB;CACzC,SAAS,OAAO;EACd,IAAI;GACF,+BAAS,UAAU,EAAE,OAAO,KAAK,CAAC;EACpC,QAAQ,CAER;EACA,MAAM;CACR;CAEA,MAAM,gBAAgBA,gDAAoB,aAAa;CACvD,IAAI,eACF,IAAI;EACF,iCAAS,cAAc,QAAQ,YAAY,gBAAgB,GAAG;GAC5D,OAAO;GACP,KAAK,cAAc,OAAO;EAC5B,CAAC;CACH,SAAS,OAAO;EACd,QAAQ,MAAM,KAAK;CACrB;AAEJ"}