UNPKG

@lucidcms/node-adapter

Version:

The official Node adapter for Lucid CMS

182 lines (167 loc) 5.24 kB
import { getVitePaths, stripAdapterExportPlugin, stripImportsPlugin } from "@lucidcms/core/helpers"; import { readFileSync } from "node:fs"; import { relative } from "node:path"; import { serveStatic } from "@hono/node-server/serve-static"; import { serve } from "@hono/node-server"; import lucid from "@lucidcms/core"; import { build } from "rolldown"; import { writeFile } from "node:fs/promises"; import nodeExternals from "rollup-plugin-node-externals"; //#region src/define-config.ts const defineConfig = (factory) => { return (env) => { const lucidConfig = factory(env); return { ...lucidConfig, hono: { extensions: [...lucidConfig.hono?.extensions || [], async (app, config) => { const paths = getVitePaths(config); app.use("/*", serveStatic({ rewriteRequestPath: (path) => { const relativeClientDist = relative(process.cwd(), paths.publicDist); return `${relativeClientDist}${path}`; } })); app.get("/admin", (c) => { const html = readFileSync(paths.clientDistHtml, "utf-8"); return c.html(html); }); app.get("/admin/*", (c) => { const html = readFileSync(paths.clientDistHtml, "utf-8"); return c.html(html); }); }] } }; }; }; var define_config_default = defineConfig; //#endregion //#region src/constants.ts const ADAPTER_KEY = "node"; const LUCID_VERSION = "0.x.x"; var constants_default = { CONFIG_FILE: "lucid.config.js", ENTRY_FILE: "server.js" }; //#endregion //#region src/adapter.ts const nodeAdapter = (options) => { return { key: ADAPTER_KEY, lucid: LUCID_VERSION, getEnvVars: async () => { try { const { config } = await import("dotenv"); config(); } catch {} return process.env; }, cli: { serve: async (config, logger) => { const startTime = process.hrtime(); logger.serverStarting("Node"); const app = await lucid.createApp({ config }); const server = serve({ fetch: app.fetch, port: options?.server?.port ?? 6543, hostname: options?.server?.hostname }); server.on("listening", () => { const address = server.address(); logger.serverStarted(address, startTime); }); return async () => { return new Promise((resolve, reject) => { server.close((error) => { if (error) reject(error); else resolve(); }); }); }; }, build: async (_, options$1, logger) => { const startTime = logger.appBuildStart("Node"); const configOutput = `${options$1.outputPath}/${constants_default.CONFIG_FILE}`; const entryOutput = `${options$1.outputPath}/${constants_default.ENTRY_FILE}`; try { await build({ input: options$1.configPath, output: { file: configOutput, format: "esm", minify: true, inlineDynamicImports: true }, plugins: [ nodeExternals(), stripAdapterExportPlugin("nodeAdapter"), stripImportsPlugin("node-adapter", ["rolldown"]) ], treeshake: true, platform: "node" }); const entry = ` import 'dotenv/config' import config from "./${constants_default.CONFIG_FILE}"; import lucid from "@lucidcms/core"; import { processConfig } from "@lucidcms/core/helpers"; import { serve } from '@hono/node-server'; import cron from 'node-cron'; const startServer = async () => { try { const resolved = await processConfig(config(process.env)); const app = await lucid.createApp({ config: resolved, }); const cronJobs = lucid.setupCronJobs({ config: resolved, }); const port = Number.parseInt(process.env.PORT || '6543', 10); const hostname = process.env.HOST || process.env.HOSTNAME; const server = serve({ fetch: app.fetch, port, hostname, }); if (cronJobs.schedule) { cron.schedule(cronJobs.schedule, async () => { await cronJobs.register(); }); } server.on("listening", () => { const address = server.address(); if(typeof address === 'string') console.log(address); else { if(address.address === '::') console.log('http://localhost:' + address.port); else console.log('http://' + address.address + ':' + address.port); } }); const gracefulShutdown = (signal) => { server.close((error) => { if (error) { console.error(error); process.exit(1); } else { process.exit(0); } }); }; process.on("SIGINT", () => gracefulShutdown("SIGINT")); process.on("SIGTERM", () => gracefulShutdown("SIGTERM")); } catch (error) { console.error(error); process.exit(1); } }; startServer();`; await writeFile(entryOutput, entry); logger.appBuildComplete(startTime); } catch (error) { logger.buildFailed(error); throw error; } } } }; }; var adapter_default = nodeAdapter; //#endregion export { define_config_default as defineConfig, adapter_default as nodeAdapter }; //# sourceMappingURL=index.js.map