UNPKG

@intlayer/chokidar

Version:

Scans and builds Intlayer declaration files into dictionaries based on Intlayer configuration.

1 lines 2.89 kB
{"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;CAAW,CAAC;AAEzE,MAAa,gBAAgB,OAC3B,kBACA,YACA,kBACkB;CAOlB,MAAM,sDANW,OAAO,YACtB,OAAO,QAAQ,WAAW,CAAC,QACxB,CAAC,GAAG,OAAO,CAAC,mBAAmB,IAAI,EAAE,IAAI,MAAM,OACjD,CAGuC,CAAC;AAG3C,0DADoB,iBACL,EAAE,EAAE,WAAW,MAAM,CAAC;CAErC,MAAM,UAAU,cAAc,QAAQ;AACtC,KAAI,QAAS,mCAAY,SAAS,EAAE,WAAW,MAAM,CAAC;CAEtD,MAAM,eAAe,2BAAY,iBAAiB,CAAC,GAAG,KAAK,KAAK,CAAC,GAAG,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE,CAAC;CACxG,MAAM,WAAW,8BACR,SAAS,aAAa,GAC3B,GAAG,iBAAiB,GAAG;AAE3B,KAAI;AACF,wCAAgB,UAAU,aAAa,QAAQ;AAC/C,qCAAa,UAAU,iBAAiB;UACjC,OAAO;AACd,MAAI;AACF,kCAAS,UAAU,EAAE,OAAO,MAAM,CAAC;UAC7B;AAGR,QAAM;;CAGR,MAAM,gBAAgBA,gDAAoB,cAAc;AACxD,KAAI,cACF,KAAI;AACF,mCAAS,cAAc,QAAQ,YAAY,iBAAiB,EAAE;GAC5D,OAAO;GACP,KAAK,cAAc,OAAO;GAC3B,CAAC;UACK,OAAO;AACd,UAAQ,MAAM,MAAM"}