{"version":3,"file":"index.mjs","names":[],"sources":["../src/generate.ts"],"sourcesContent":["import {\n  getWarningCount,\n  type GlobalOptions,\n  isString,\n  logError,\n  type OptionsExport,\n  resetWarnings,\n  setVerbose,\n} from '@orval/core';\n\nimport { generateSpec } from './generate-spec';\nimport { findConfigFile, loadConfigFile } from './utils/config';\nimport { normalizeOptions } from './utils/options';\nimport { startWatcher } from './utils/watcher';\n\nexport async function generate(\n  optionsExport?: string | OptionsExport,\n  workspace = process.cwd(),\n  options?: GlobalOptions,\n) {\n  setVerbose(!!options?.verbose);\n  resetWarnings();\n\n  if (!optionsExport || isString(optionsExport)) {\n    const configFilePath = findConfigFile(optionsExport);\n    const configFile = await loadConfigFile(configFilePath);\n\n    const configs = Object.entries(configFile);\n\n    let hasErrors = false;\n    for (const [projectName, config] of configs) {\n      const normalizedOptions = await normalizeOptions(\n        config,\n        workspace,\n        options,\n      );\n\n      try {\n        await generateSpec(workspace, normalizedOptions, projectName);\n      } catch (error) {\n        hasErrors = true;\n        logError(error, projectName);\n      }\n\n      if (options?.watch !== undefined) {\n        const fileToWatch = isString(normalizedOptions.input.target)\n          ? normalizedOptions.input.target\n          : undefined;\n\n        await startWatcher(\n          options.watch,\n          async () => {\n            resetWarnings();\n            try {\n              await generateSpec(workspace, normalizedOptions, projectName);\n            } catch (error) {\n              logError(error, projectName);\n            }\n            if (options.failOnWarnings && getWarningCount() > 0) {\n              throw new Error(\n                `Process failed with ${getWarningCount()} warning(s) due to failOnWarnings option`,\n              );\n            }\n          },\n          fileToWatch,\n        );\n      }\n    }\n\n    if (hasErrors)\n      logError('One or more project failed, see above for details');\n\n    if (options?.failOnWarnings && getWarningCount() > 0) {\n      throw new Error(\n        `Process failed with ${getWarningCount()} warning(s) due to failOnWarnings option`,\n      );\n    }\n\n    return;\n  }\n\n  const normalizedOptions = await normalizeOptions(\n    optionsExport,\n    workspace,\n    options,\n  );\n\n  try {\n    await generateSpec(workspace, normalizedOptions);\n  } catch (error) {\n    logError(error);\n  }\n\n  if (options?.watch) {\n    await startWatcher(\n      options.watch,\n      async () => {\n        resetWarnings();\n        try {\n          await generateSpec(workspace, normalizedOptions);\n        } catch (error) {\n          logError(error);\n        }\n        if (options.failOnWarnings && getWarningCount() > 0) {\n          throw new Error(\n            `Process failed with ${getWarningCount()} warning(s) due to failOnWarnings option`,\n          );\n        }\n      },\n      normalizedOptions.input.target as string,\n    );\n  }\n\n  if (options?.failOnWarnings && getWarningCount() > 0) {\n    throw new Error(\n      `Process failed with ${getWarningCount()} warning(s) due to failOnWarnings option`,\n    );\n  }\n}\n"],"mappings":";;;;AAeA,eAAsB,SACpB,eACA,YAAY,QAAQ,KAAK,EACzB,SACA;AACA,YAAW,CAAC,CAAC,SAAS,QAAQ;AAC9B,gBAAe;AAEf,KAAI,CAAC,iBAAiB,SAAS,cAAc,EAAE;EAE7C,MAAM,aAAa,MAAM,eADF,eAAe,cAAc,CACG;EAEvD,MAAM,UAAU,OAAO,QAAQ,WAAW;EAE1C,IAAI,YAAY;AAChB,OAAK,MAAM,CAAC,aAAa,WAAW,SAAS;GAC3C,MAAM,oBAAoB,MAAM,iBAC9B,QACA,WACA,QACD;AAED,OAAI;AACF,UAAM,aAAa,WAAW,mBAAmB,YAAY;YACtD,OAAO;AACd,gBAAY;AACZ,aAAS,OAAO,YAAY;;AAG9B,OAAI,SAAS,UAAU,KAAA,GAAW;IAChC,MAAM,cAAc,SAAS,kBAAkB,MAAM,OAAO,GACxD,kBAAkB,MAAM,SACxB,KAAA;AAEJ,UAAM,aACJ,QAAQ,OACR,YAAY;AACV,oBAAe;AACf,SAAI;AACF,YAAM,aAAa,WAAW,mBAAmB,YAAY;cACtD,OAAO;AACd,eAAS,OAAO,YAAY;;AAE9B,SAAI,QAAQ,kBAAkB,iBAAiB,GAAG,EAChD,OAAM,IAAI,MACR,uBAAuB,iBAAiB,CAAC,0CAC1C;OAGL,YACD;;;AAIL,MAAI,UACF,UAAS,oDAAoD;AAE/D,MAAI,SAAS,kBAAkB,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