{"version":3,"sources":["jsdelivr-header.js","/npm/@lsk4/config@4.28.3/src/loadConfigSync.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA,iBCmGiE,EAC7D,QAAU,Q","file":"/npm/@lsk4/config@4.28.3/cjs/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@lsk4/config@4.28.3/cjs/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import path from 'node:path';\n\nimport { Err } from '@lsk4/err';\n// @ts-ignore it can't find types, but module has types\nimport JoyCon from 'joycon';\n\nimport { checkSchema } from './checkSchema';\nimport { lazyLog } from './log';\nimport { allowedExtensions, defaultExtensions } from './options';\nimport type { LoadConfigOptions } from './types.js';\nimport { bundleRequireSync } from './utils/bundleRequireSync';\nimport { loadJsoncSync } from './utils/loadJsoncSync';\n\n// NOTE: это копипаста из соседнего файла loadConfig.ts, но синхронная версия. Изменять нужно оба файла.\n\nexport function loadConfigSync<T>(\n  name: string,\n  options: LoadConfigOptions<T> & { throwError?: true },\n): { path: string; config: T };\nexport function loadConfigSync<T>(name: string): { path: string; config: T };\nexport function loadConfigSync<T>(\n  name: string,\n  options: LoadConfigOptions<T> & { throwError: false },\n): { path?: string; config?: T };\n\nexport function loadConfigSync<T>(\n  name: string = '.env',\n  {\n    cwd = process.cwd(),\n    files: initFiles = [],\n    exts = defaultExtensions,\n    stopDir,\n    throwError = true,\n    silent = false,\n    packageKey = '',\n    processEnvKey = '',\n    schema,\n  }: LoadConfigOptions<T> = {},\n): { path?: string; config?: T } {\n  try {\n    const configJoycon = new JoyCon();\n    const filteredExts = exts.filter((ext) => allowedExtensions.some((e) => ext.endsWith(e)));\n\n    let files = initFiles;\n    if (files.length === 0) {\n      files = filteredExts.map((ext) => name + ext);\n      if (packageKey) {\n        files.push('package.json');\n      }\n    }\n\n    const configPath = configJoycon.resolveSync({\n      files,\n      cwd,\n      stopDir: stopDir || path.parse(cwd).root,\n      packageKey,\n    });\n\n    const processEnvValue = process.env[processEnvKey];\n    const processEnvPath = `process.env.${processEnvKey}`;\n\n    const isProcess = typeof processEnvValue === 'string';\n    const isJson = configPath && configPath.endsWith('.json');\n    const isRequire = configPath && !isJson;\n\n    // js,.ts,.cjs,.mjs\n    if (isRequire) {\n      const { mod: config } = bundleRequireSync<any>({\n        filepath: configPath,\n      });\n      const raw = config; // config.mod\n      const res = {\n        path: configPath,\n        config: raw[packageKey] || raw.default || raw,\n      };\n      if (schema) return checkSchema<T>(res as any, { schema, throwError, silent });\n      return res;\n    }\n    // json, package.json\n    if (isJson) {\n      let data = loadJsoncSync(configPath);\n      if (packageKey && configPath.endsWith('package.json')) {\n        data = packageKey ? data[packageKey] : undefined;\n      }\n      if (data) {\n        const res = { path: configPath, config: data };\n        if (schema) return checkSchema<T>(res as any, { schema, throwError, silent });\n        return res;\n      }\n    }\n    if (isProcess) {\n      const res = {\n        path: processEnvPath,\n        config: JSON.parse(processEnvValue),\n      };\n      if (schema) return checkSchema<T>(res as any, { schema, throwError, silent });\n      return res;\n    }\n    const lookingPaths = [\n      ...files.map((file) =>\n        file === 'package.json' ? `${cwd}/${file}#${packageKey}` : `${cwd}/${file}`,\n      ),\n      processEnvPath,\n    ].filter(Boolean);\n    const lookinPathsMessage = lookingPaths.map((p) => `- ${p}`).join('\\n');\n    const errMessage = `Config not found (${name}) in paths: \\n${lookinPathsMessage}`;\n    throw new Err('configNotFound', errMessage, {\n      paths: lookingPaths,\n    });\n  } catch (err) {\n    if (throwError) throw err;\n    // TODO: replace to lsk4/log/log, but now it doesn't compile dts\n    if (!silent) lazyLog().warn('[loadConfig]', err);\n  }\n  return {};\n}\n"]}