{"version":3,"file":"loadExternalFile.cjs","names":["JSON5","transpileTSToCJSSync","parseFileContent","transpileTSToCJS","colorizePath"],"sources":["../../../src/loadExternalFile/loadExternalFile.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { readFile } from 'node:fs/promises';\nimport { extname } from 'node:path';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport JSON5 from 'json5';\nimport { colorizePath, logger } from '../logger';\nimport {\n  parseFileContent,\n  type SandBoxContextOptions,\n} from './parseFileContent';\nimport {\n  type TranspileOptions,\n  transpileTSToCJS,\n  transpileTSToCJSSync,\n} from './transpileTSToCJS';\n\n// CJS MJS cross usage\nconst parseJSON5 = JSON5.parse || (JSON5 as any).default?.parse;\n\nexport type LoadExternalFileOptions = {\n  configuration?: IntlayerConfig;\n  buildOptions?: TranspileOptions;\n  logError?: boolean;\n} & SandBoxContextOptions;\n\n/**\n * Load the content declaration from the given path\n *\n * Accepts JSON, JS, MJS and TS files as configuration\n */\nexport const loadExternalFileSync = (\n  filePath: string,\n  options?: LoadExternalFileOptions\n): any | undefined => {\n  const fileExtension = extname(filePath) || '.json';\n\n  try {\n    if (\n      fileExtension === '.json' ||\n      fileExtension === '.json5' ||\n      fileExtension === '.jsonc'\n    ) {\n      // Assume JSON\n      return parseJSON5(readFileSync(filePath, 'utf-8'));\n    }\n\n    // Rest is JS, MJS or TS\n    const code = readFileSync(filePath, 'utf-8');\n\n    const moduleResultString: string | undefined = transpileTSToCJSSync(\n      code,\n      filePath,\n      options?.buildOptions\n    );\n\n    if (!moduleResultString) {\n      logger('File could not be loaded.', { level: 'error' });\n      return undefined;\n    }\n\n    const fileContent = parseFileContent(moduleResultString, {\n      projectRequire: options?.projectRequire,\n      envVarOptions: options?.envVarOptions,\n      additionalEnvVars: options?.additionalEnvVars,\n      mocks: options?.mocks,\n      aliases: options?.aliases,\n    });\n\n    if (typeof fileContent === 'undefined') {\n      logger(`File could not be loaded. Path : ${filePath}`);\n      return undefined;\n    }\n\n    return fileContent;\n  } catch (error) {\n    logger(\n      [\n        `Error: ${(error as Error).message} - `,\n        JSON.stringify((error as Error).stack, null, 2),\n      ],\n      {\n        level: 'error',\n      }\n    );\n  }\n};\n\n/**\n * Load the content declaration from the given path\n *\n * Accepts JSON, JS, MJS and TS files as configuration\n */\nexport const loadExternalFile = async (\n  filePath: string,\n  options?: LoadExternalFileOptions\n): Promise<any | undefined> => {\n  const fileExtension = extname(filePath);\n\n  try {\n    if (\n      fileExtension === '.json' ||\n      fileExtension === '.json5' ||\n      fileExtension === '.jsonc'\n    ) {\n      // Remove cache to force getting fresh content\n      const fileContent = await readFile(filePath, 'utf-8');\n      return parseJSON5(fileContent);\n    }\n\n    // Rest is JS, MJS or TS\n    const code = await readFile(filePath, 'utf-8');\n\n    const moduleResultString: string | undefined = await transpileTSToCJS(\n      code,\n      filePath,\n      options?.buildOptions\n    );\n\n    if (!moduleResultString) {\n      logger('File could not be loaded.', { level: 'error' });\n      return undefined;\n    }\n\n    const fileContent = parseFileContent(moduleResultString, {\n      projectRequire: options?.projectRequire,\n      envVarOptions: options?.envVarOptions,\n      additionalEnvVars: options?.additionalEnvVars,\n      mocks: options?.mocks,\n      aliases: options?.aliases,\n    });\n\n    if (typeof fileContent === 'undefined') {\n      logger(`File could not be loaded. Path : ${colorizePath(filePath)}`);\n      return undefined;\n    }\n\n    return fileContent;\n  } catch (error) {\n    if (options?.logError ?? true) {\n      logger(\n        [\n          `Error: ${(error as Error).message} - `,\n          JSON.stringify((error as Error).stack, null, 2),\n        ],\n        {\n          level: 'error',\n        }\n      );\n    }\n  }\n};\n"],"mappings":";;;;;;;;;;;;AAiBA,MAAM,aAAaA,cAAM,SAAUA,cAAc,SAAS;;;;;;AAa1D,MAAa,wBACX,UACA,YACoB;CACpB,MAAM,uCAAwB,SAAS,IAAI;AAE3C,KAAI;AACF,MACE,kBAAkB,WAClB,kBAAkB,YAClB,kBAAkB,SAGlB,QAAO,qCAAwB,UAAU,QAAQ,CAAC;EAMpD,MAAM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