import { bold } from "kleur/colors"; import fs from "node:fs"; import { fileURLToPath } from "node:url"; import { buildClientDirectiveEntrypoint } from "../core/client-directive/index.js"; import { mergeConfig } from "../core/config/index.js"; import { info } from "../core/logger/core.js"; import { isServerLikeOutput } from "../prerender/utils.js"; async function withTakingALongTimeMsg({ name, hookResult, timeoutMs = 3e3, logging }) { const timeout = setTimeout(() => { info(logging, "build", `Waiting for the ${bold(name)} integration...`); }, timeoutMs); const result = await hookResult; clearTimeout(timeout); return result; } async function runHookConfigSetup({ settings, command, logging, isRestart = false }) { var _a; if (settings.config.adapter) { settings.config.integrations.push(settings.config.adapter); } let updatedConfig = { ...settings.config }; let updatedSettings = { ...settings, config: updatedConfig }; let addedClientDirectives = /* @__PURE__ */ new Map(); for (const integration of settings.config.integrations) { if ((_a = integration.hooks) == null ? void 0 : _a["astro:config:setup"]) { let addPageExtension2 = function(...input) { const exts = input.flat(Infinity).map((ext) => `.${ext.replace(/^\./, "")}`); updatedSettings.pageExtensions.push(...exts); }, addContentEntryType2 = function(contentEntryType) { updatedSettings.contentEntryTypes.push(contentEntryType); }, addDataEntryType2 = function(dataEntryType) { updatedSettings.dataEntryTypes.push(dataEntryType); }; var addPageExtension = addPageExtension2, addContentEntryType = addContentEntryType2, addDataEntryType = addDataEntryType2; const hooks = { config: updatedConfig, command, isRestart, addRenderer(renderer) { if (!renderer.name) { throw new Error(`Integration ${bold(integration.name)} has an unnamed renderer.`); } if (!renderer.serverEntrypoint) { throw new Error(`Renderer ${bold(renderer.name)} does not provide a serverEntrypoint.`); } updatedSettings.renderers.push(renderer); }, injectScript: (stage, content) => { updatedSettings.scripts.push({ stage, content }); }, updateConfig: (newConfig) => { updatedConfig = mergeConfig(updatedConfig, newConfig); }, injectRoute: (injectRoute) => { updatedSettings.injectedRoutes.push(injectRoute); }, addWatchFile: (path) => { updatedSettings.watchFiles.push(path instanceof URL ? fileURLToPath(path) : path); }, addClientDirective: ({ name, entrypoint }) => { if (updatedSettings.clientDirectives.has(name) || addedClientDirectives.has(name)) { throw new Error( `The "${integration.name}" integration is trying to add the "${name}" client directive, but it already exists.` ); } addedClientDirectives.set(name, buildClientDirectiveEntrypoint(name, entrypoint)); } }; Object.defineProperty(hooks, "addPageExtension", { value: addPageExtension2, writable: false, enumerable: false }); Object.defineProperty(hooks, "addContentEntryType", { value: addContentEntryType2, writable: false, enumerable: false }); Object.defineProperty(hooks, "addDataEntryType", { value: addDataEntryType2, writable: false, enumerable: false }); await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:config:setup"](hooks), logging }); for (const [name, compiled] of addedClientDirectives) { updatedSettings.clientDirectives.set(name, await compiled); } } } updatedSettings.config = updatedConfig; return updatedSettings; } async function runHookConfigDone({ settings, logging }) { var _a; for (const integration of settings.config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:config:done"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:config:done"]({ config: settings.config, setAdapter(adapter) { if (settings.adapter && settings.adapter.name !== adapter.name) { throw new Error( `Integration "${integration.name}" conflicts with "${settings.adapter.name}". You can only configure one deployment integration.` ); } settings.adapter = adapter; } }), logging }); } } } async function runHookServerSetup({ config, server, logging }) { var _a; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:server:setup"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:server:setup"]({ server }), logging }); } } } async function runHookServerStart({ config, address, logging }) { var _a; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:server:start"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:server:start"]({ address }), logging }); } } } async function runHookServerDone({ config, logging }) { var _a; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:server:done"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:server:done"](), logging }); } } } async function runHookBuildStart({ config, logging }) { var _a; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:build:start"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:build:start"](), logging }); } } } async function runHookBuildSetup({ config, vite, pages, target, logging }) { var _a; let updatedConfig = vite; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:build:setup"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:build:setup"]({ vite, pages, target, updateConfig: (newConfig) => { updatedConfig = mergeConfig(updatedConfig, newConfig); } }), logging }); } } return updatedConfig; } async function runHookBuildSsr({ config, manifest, logging, entryPoints, middlewareEntryPoint }) { var _a; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:build:ssr"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:build:ssr"]({ manifest, entryPoints, middlewareEntryPoint }), logging }); } } } async function runHookBuildGenerated({ config, logging }) { var _a; const dir = isServerLikeOutput(config) ? config.build.client : config.outDir; for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:build:generated"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:build:generated"]({ dir }), logging }); } } } async function runHookBuildDone({ config, pages, routes, logging }) { var _a; const dir = isServerLikeOutput(config) ? config.build.client : config.outDir; await fs.promises.mkdir(dir, { recursive: true }); for (const integration of config.integrations) { if ((_a = integration == null ? void 0 : integration.hooks) == null ? void 0 : _a["astro:build:done"]) { await withTakingALongTimeMsg({ name: integration.name, hookResult: integration.hooks["astro:build:done"]({ pages: pages.map((p) => ({ pathname: p })), dir, routes }), logging }); } } } export { runHookBuildDone, runHookBuildGenerated, runHookBuildSetup, runHookBuildSsr, runHookBuildStart, runHookConfigDone, runHookConfigSetup, runHookServerDone, runHookServerSetup, runHookServerStart };