UNPKG

intlayer-editor

Version:

Integrate the Intlayer visual editor into your Intlayer projects, enabling CMS-like content management with multilingual support.

92 lines (89 loc) 3.79 kB
import { configurationRouter } from "./routes/config.routes.mjs"; import { dictionaryRouter } from "./routes/dictionary.routes.mjs"; import { checkPortAvailability } from "./utils/checkPortAvailability.mjs"; import { getConfiguration } from "@intlayer/config/node"; import { dirname, join, resolve } from "node:path"; import { intlayer } from "fastify-intlayer"; import { existsSync, lstatSync, readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import fastifyCompress from "@fastify/compress"; import fastifyCookie from "@fastify/cookie"; import fastifyCors from "@fastify/cors"; import fastifyFormbody from "@fastify/formbody"; import fastifyHelmet from "@fastify/helmet"; import fastifyStatic from "@fastify/static"; import * as ANSIColors from "@intlayer/config/colors"; import { getEnvFilePath } from "@intlayer/config/env"; import { colorize, colorizePath, getAppLogger } from "@intlayer/config/logger"; import Fastify from "fastify"; import mime from "mime"; //#region src/index.ts const __dirname = dirname(fileURLToPath(import.meta.url)); const envFileOptions = { env: "development", envFile: process.env.ENV_FILE }; const FALLBACK_PORT = 8e3; const config = getConfiguration(envFileOptions); const appLogger = getAppLogger(config); const port = config.editor.port ?? FALLBACK_PORT; if (!config.editor.enabled) { appLogger(`Editor is not enabled. Add ${colorize("editor.enabled", ANSIColors.BLUE)} to ${colorizePath("intlayer.config.ts")} file to enable it.`, { level: "error" }); process.exit(0); } const packageJson = JSON.parse(readFileSync(resolve(__dirname, "../../package.json"), "utf8")); const app = Fastify({ disableRequestLogging: true }); app.register(intlayer); const clientDistPath = resolve(__dirname, "../../client/dist"); const corsOptions = { origin: "*", credentials: true }; const startServer = async (app) => { if (!await checkPortAvailability(port)) { appLogger(`Error: Port ${port} is already in use.`, { level: "error" }); process.exit(255); } await app.register(fastifyHelmet, { contentSecurityPolicy: false, global: true }); await app.register(fastifyCors, corsOptions); await app.register(fastifyCompress); await app.register(fastifyCookie); await app.register(fastifyFormbody); await app.register(dictionaryRouter, { prefix: "/api/dictionary" }); await app.register(configurationRouter, { prefix: "/api/config" }); await app.register(fastifyStatic, { root: clientDistPath, wildcard: false }); app.setNotFoundHandler((req, reply) => { const requestedPath = join(clientDistPath, req.raw.url || "/"); if (existsSync(requestedPath) && lstatSync(requestedPath).isFile()) { const mimeType = mime.getType(requestedPath) ?? "application/octet-stream"; reply.header("Content-Type", mimeType); return reply.sendFile(req.raw.url?.split("/").pop() || "index.html"); } else return reply.sendFile("index.html"); }); try { await app.listen({ port, host: "0.0.0.0" }); const dotEnvFilePath = getEnvFilePath(envFileOptions.env, envFileOptions.envFile); console.log(` ${colorize(colorize("INTLAYER", ANSIColors.BOLD), ANSIColors.GREY_DARK)} ${colorize(`v${packageJson.version}`, ANSIColors.GREY_DARK)} Editor running at: ${colorizePath(`http://localhost:${port}`)} ${colorize("➜", ANSIColors.GREY_DARK)} Watching application at: ${config.editor.applicationURL === "" ? "-" : colorizePath(config.editor.applicationURL)} ${colorize("➜", ANSIColors.GREY_DARK)} Access key: ${config.editor.clientId ?? "-"} ${colorize("➜", ANSIColors.GREY_DARK)} Environment: ${dotEnvFilePath ?? "-"} `); } catch (err) { app.log.error(err); process.exit(1); } }; startServer(app); //#endregion //# sourceMappingURL=index.mjs.map