{"version":3,"file":"prepare-artifacts.mjs","names":[],"sources":["../../../src/libs/runtime/prepare-artifacts.ts"],"sourcesContent":["import type {\n\tDatabaseAdapterCreator,\n\tDatabaseAdapterFactory,\n} from \"../db/adapter-factory.js\";\nimport type { LucidPluginResponse } from \"../plugins/types.js\";\nimport type {\n\tDatabaseAdapterValue,\n\tEnvironmentVariables,\n\tLucidConfigDefinition,\n\tRuntimeArtifactCustom,\n\tRuntimeArtifactProvider,\n\tRuntimeBuildArtifact,\n\tRuntimePrepareArtifacts,\n} from \"./types.js\";\n\nconst isPromiseLike = (value: unknown): value is PromiseLike<unknown> =>\n\t(typeof value === \"object\" || typeof value === \"function\") &&\n\tvalue !== null &&\n\t\"then\" in value;\n\n/** Detects adapter factories so core can read runtime setup metadata before resolving the adapter. */\nconst isDatabaseAdapterFactory = (\n\tvalue: unknown,\n): value is DatabaseAdapterFactory => {\n\tif (!value || (typeof value !== \"object\" && typeof value !== \"function\")) {\n\t\treturn false;\n\t}\n\n\tconst factory = value as Partial<DatabaseAdapterFactory> &\n\t\tPartial<DatabaseAdapterCreator>;\n\n\tif (\n\t\ttypeof value === \"function\" &&\n\t\tfactory.__lucidDatabaseAdapterCreator !== true\n\t) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\ttypeof factory.adapter === \"string\" && typeof factory.resolve === \"function\"\n\t);\n};\n\n/** Creates the empty artifact payload passed into runtime prepare handlers. */\nexport const createRuntimePrepareArtifacts = (): RuntimePrepareArtifacts => ({\n\tcustom: [],\n});\n\n/** Resolves static or env-aware artifact providers used by database adapters. */\nconst resolvePrepareArtifacts = async (\n\tprovider: RuntimeArtifactProvider | undefined,\n\tenv: EnvironmentVariables,\n): Promise<Array<RuntimeArtifactCustom>> => {\n\tif (!provider) return [];\n\treturn typeof provider === \"function\" ? provider(env) : provider;\n};\n\nconst isCustomArtifact = (\n\tartifact: RuntimeBuildArtifact | RuntimeArtifactCustom,\n): artifact is RuntimeArtifactCustom => \"custom\" in artifact;\n\n/** Adds only custom artifacts that the active runtime explicitly supports. */\nconst addSupportedArtifacts = (props: {\n\ttarget: RuntimePrepareArtifacts;\n\tartifacts: Array<RuntimeBuildArtifact | RuntimeArtifactCustom>;\n\tcustomArtifactTypes?: string[];\n}) => {\n\tif (!props.customArtifactTypes?.length) {\n\t\treturn;\n\t}\n\n\tfor (const artifact of props.artifacts) {\n\t\tif (\n\t\t\tisCustomArtifact(artifact) &&\n\t\t\tprops.customArtifactTypes.includes(artifact.type)\n\t\t) {\n\t\t\tprops.target.custom.push(artifact);\n\t\t}\n\t}\n};\n\nconst collectDatabasePrepareArtifacts = async (props: {\n\tdb: DatabaseAdapterValue;\n\tenv: EnvironmentVariables;\n\ttarget: RuntimePrepareArtifacts;\n\tcustomArtifactTypes?: string[];\n}) => {\n\tconst resolvedValue = isPromiseLike(props.db) ? await props.db : props.db;\n\tif (!isDatabaseAdapterFactory(resolvedValue)) {\n\t\treturn;\n\t}\n\n\taddSupportedArtifacts({\n\t\ttarget: props.target,\n\t\tartifacts: await resolvePrepareArtifacts(\n\t\t\tresolvedValue.hooks?.runtime,\n\t\t\tprops.env,\n\t\t),\n\t\tcustomArtifactTypes: props.customArtifactTypes,\n\t});\n};\n\nconst collectPluginPrepareArtifacts = async (props: {\n\tplugins: Array<LucidPluginResponse>;\n\tenv: EnvironmentVariables;\n\tdefinition: LucidConfigDefinition;\n\tpaths?: {\n\t\tconfigPath?: string;\n\t\tprojectRoot?: string;\n\t};\n\ttarget: RuntimePrepareArtifacts;\n\tcustomArtifactTypes?: string[];\n}) => {\n\tfor (const plugin of props.plugins) {\n\t\tif (!plugin.hooks?.runtime) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst res = await plugin.hooks.runtime({\n\t\t\tphase: \"prepare\",\n\t\t\tenv: props.env,\n\t\t\tdefinition: props.definition,\n\t\t\tpaths: props.paths,\n\t\t});\n\t\tif (res.error) {\n\t\t\tthrow new Error(\n\t\t\t\t`Runtime prepare hook failed for the ${plugin.key} plugin.`,\n\t\t\t);\n\t\t}\n\n\t\taddSupportedArtifacts({\n\t\t\ttarget: props.target,\n\t\t\tartifacts: res.data?.artifacts ?? [],\n\t\t\tcustomArtifactTypes: props.customArtifactTypes,\n\t\t});\n\t}\n};\n\n/**\n * Collects setup artifacts from adapters and plugin prepare hooks.\n * Core keeps these opaque and filters by runtime-supported artifact type.\n */\nexport const collectRuntimePrepareArtifacts = async (props: {\n\tdb: DatabaseAdapterValue;\n\tplugins: Array<LucidPluginResponse>;\n\tenv: EnvironmentVariables;\n\tdefinition: LucidConfigDefinition;\n\tpaths?: {\n\t\tconfigPath?: string;\n\t\tprojectRoot?: string;\n\t};\n\tcustomArtifactTypes?: string[];\n}): Promise<RuntimePrepareArtifacts> => {\n\tconst result = createRuntimePrepareArtifacts();\n\n\tif (!props.customArtifactTypes?.length) {\n\t\treturn result;\n\t}\n\n\tawait collectDatabasePrepareArtifacts({\n\t\tdb: props.db,\n\t\tenv: props.env,\n\t\ttarget: result,\n\t\tcustomArtifactTypes: props.customArtifactTypes,\n\t});\n\tawait collectPluginPrepareArtifacts({\n\t\tplugins: props.plugins,\n\t\tenv: props.env,\n\t\tdefinition: props.definition,\n\t\tpaths: props.paths,\n\t\ttarget: result,\n\t\tcustomArtifactTypes: props.customArtifactTypes,\n\t});\n\n\treturn result;\n};\n"],"mappings":"AAeA,MAAM,EAAiB,IACrB,OAAO,GAAU,UAAY,OAAO,GAAU,aAC/C,IAAU,MACV,SAAU,EAGL,EACL,GACqC,CACrC,GAAI,CAAC,GAAU,OAAO,GAAU,UAAY,OAAO,GAAU,WAC5D,MAAO,GAGR,IAAM,EAAU,EAUhB,OANC,OAAO,GAAU,YACjB,EAAQ,gCAAkC,GAEnC,GAIP,OAAO,EAAQ,SAAY,UAAY,OAAO,EAAQ,SAAY,UAEpE,EAGa,OAAgE,CAC5E,OAAQ,CAAC,CACV,GAGM,EAA0B,MAC/B,EACA,IAEK,EACE,OAAO,GAAa,WAAa,EAAS,CAAG,EAAI,EADlC,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