{"version":3,"sources":["../../src/bin/generateTypes.ts"],"sourcesContent":["import type { AcceptedLanguages } from '@payloadcms/translations'\n\nimport { initI18n } from '@payloadcms/translations'\nimport fs from 'fs/promises'\nimport { compile } from 'json-schema-to-typescript'\n\nimport type { SanitizedConfig } from '../config/types.js'\n\nimport { addSelectGenericsToGeneratedTypes } from '../utilities/addSelectGenericsToGeneretedTypes.js'\nimport { configToJSONSchema } from '../utilities/configToJSONSchema.js'\nimport { getLogger } from '../utilities/logger.js'\n\nexport async function generateTypes(\n  config: SanitizedConfig,\n  options?: { log: boolean },\n): Promise<void> {\n  const logger = getLogger('payload', 'sync')\n  const outputFile = process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile\n\n  const shouldLog = options?.log ?? true\n\n  if (shouldLog) {\n    logger.info('Compiling TS types for Collections and Globals...')\n  }\n\n  const languages = Object.keys(config.i18n.supportedLanguages) as AcceptedLanguages[]\n\n  const language = languages.includes('en') ? 'en' : config.i18n.fallbackLanguage\n\n  const i18n = await initI18n({ config: config.i18n, context: 'api', language })\n\n  const jsonSchema = configToJSONSchema(config, config.db.defaultIDType, i18n)\n\n  const declare = `declare module 'payload' {\\n  export interface GeneratedTypes extends Config {}\\n}`\n  const declareWithTSIgnoreError = `declare module 'payload' {\\n  // @ts-ignore \\n  export interface GeneratedTypes extends Config {}\\n}`\n\n  let compiled = await compile(jsonSchema, 'Config', {\n    bannerComment:\n      '/* tslint:disable */\\n/* eslint-disable */\\n/**\\n* This file was automatically generated by Payload.\\n* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,\\n* and re-run `payload generate:types` to regenerate this file.\\n*/',\n    style: {\n      singleQuote: true,\n    },\n    // Generates code for $defs that aren't referenced by the schema. Reason:\n    // If a field defines an interfaceName, it should be included in the generated types\n    // even if it's not used by another type. Reason: the user might want to use it in their own code.\n    unreachableDefinitions: true,\n    // Allow resolving external file references in $ref pointers\n    cwd: process.cwd(),\n  })\n\n  compiled = addSelectGenericsToGeneratedTypes({ compiledGeneratedTypes: compiled })\n\n  if (config.typescript.postProcess?.length) {\n    for (const fn of config.typescript.postProcess) {\n      compiled = fn({ compiledTypes: compiled, config })\n    }\n  }\n\n  if (config.typescript.declare !== false) {\n    if (config.typescript.declare?.ignoreTSError) {\n      compiled += `\\n\\n${declareWithTSIgnoreError}`\n    } else {\n      compiled += `\\n\\n${declare}`\n    }\n  }\n\n  // Diff the compiled types against the existing types file\n  try {\n    const existingTypes = await fs.readFile(outputFile, 'utf-8')\n\n    if (compiled === existingTypes) {\n      return\n    }\n  } catch (_) {\n    // swallow err\n  }\n\n  await fs.writeFile(outputFile, compiled)\n  if (shouldLog) {\n    logger.info(`Types written to ${outputFile}`)\n  }\n}\n"],"names":["initI18n","fs","compile","addSelectGenericsToGeneratedTypes","configToJSONSchema","getLogger","generateTypes","config","options","logger","outputFile","process","env","PAYLOAD_TS_OUTPUT_PATH","typescript","shouldLog","log","info","languages","Object","keys","i18n","supportedLanguages","language","includes","fallbackLanguage","context","jsonSchema","db","defaultIDType","declare","declareWithTSIgnoreError","compiled","bannerComment","style","singleQuote","unreachableDefinitions","cwd","compiledGeneratedTypes","postProcess","length","fn","compiledTypes","ignoreTSError","existingTypes","readFile","_","writeFile"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,2BAA0B;AACnD,OAAOC,QAAQ,cAAa;AAC5B,SAASC,OAAO,QAAQ,4BAA2B;AAInD,SAASC,iCAAiC,QAAQ,oDAAmD;AACrG,SAASC,kBAAkB,QAAQ,qCAAoC;AACvE,SAASC,SAAS,QAAQ,yBAAwB;AAElD,OAAO,eAAeC,cACpBC,MAAuB,EACvBC,OAA0B;IAE1B,MAAMC,SAASJ,UAAU,WAAW;IACpC,MAAMK,aAAaC,QAAQC,GAAG,CAACC,sBAAsB,IAAIN,OAAOO,UAAU,CAACJ,UAAU;IAErF,MAAMK,YAAYP,SAASQ,OAAO;IAElC,IAAID,WAAW;QACbN,OAAOQ,IAAI,CAAC;IACd;IAEA,MAAMC,YA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