UNPKG

intlayer-editor

Version:

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

1 lines 6.78 kB
{"version":3,"file":"index.cjs","names":["__dirname","ANSIColors","intlayer","checkPortAvailability","fastifyHelmet","fastifyCors","fastifyCompress","fastifyCookie","fastifyFormbody","dictionaryRouter","configurationRouter","fastifyStatic"],"sources":["../src/index.ts"],"sourcesContent":["import { existsSync, lstatSync, readFileSync } from 'node:fs';\nimport { dirname, join, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport fastifyCompress from '@fastify/compress';\nimport fastifyCookie from '@fastify/cookie';\nimport fastifyCors, { type FastifyCorsOptions } from '@fastify/cors';\nimport fastifyFormbody from '@fastify/formbody';\nimport fastifyHelmet from '@fastify/helmet';\nimport fastifyStatic from '@fastify/static';\nimport * as ANSIColors from '@intlayer/config/colors';\nimport { getEnvFilePath } from '@intlayer/config/env';\nimport { colorize, colorizePath, getAppLogger } from '@intlayer/config/logger';\nimport { getConfiguration } from '@intlayer/config/node';\nimport { configurationRouter } from '@routes/config.routes';\nimport { dictionaryRouter } from '@routes/dictionary.routes';\nimport { checkPortAvailability } from '@utils/checkPortAvailability';\nimport Fastify, { type FastifyInstance } from 'fastify';\nimport { intlayer } from 'fastify-intlayer';\nimport mime from 'mime';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\nconst envFileOptions = {\n env: process.env.NODE_ENV,\n envFile: process.env.ENV_FILE,\n};\n\nconst FALLBACK_PORT = 8000;\nconst config = getConfiguration(envFileOptions);\n\nconst appLogger = getAppLogger(config);\nconst port = config.editor.port ?? FALLBACK_PORT;\n\nif (!config.editor.enabled) {\n appLogger(\n `Editor is not enabled. Add ${colorize('editor.enabled', ANSIColors.BLUE)} to ${colorizePath('intlayer.config.ts')} file to enable it.`,\n {\n level: 'error',\n }\n );\n process.exit(0);\n}\n\n// Load package.json\nconst packageJson = JSON.parse(\n readFileSync(resolve(__dirname, '../../package.json'), 'utf8')\n);\n\nconst app: FastifyInstance = Fastify({\n disableRequestLogging: true, // Optional: Keep logs clean like the original\n});\n\n// Load internationalization plugin\n// Assuming fastify-intlayer is the Fastify equivalent of express-intlayer\napp.register(intlayer);\n\nconst clientDistPath = resolve(__dirname, '../../client/dist');\n\nconst corsOptions: FastifyCorsOptions = {\n origin: '*',\n credentials: true,\n};\n\nconst startServer = async (app: FastifyInstance) => {\n const isPortAvailable = await checkPortAvailability(port);\n\n if (!isPortAvailable) {\n appLogger(`Error: Port ${port} is already in use.`, {\n level: 'error',\n });\n process.exit(255);\n }\n\n // Security Headers\n await app.register(fastifyHelmet, {\n contentSecurityPolicy: false,\n global: true,\n });\n\n // CORS\n await app.register(fastifyCors, corsOptions);\n\n // Compression\n await app.register(fastifyCompress);\n\n // Cookie Parser\n await app.register(fastifyCookie);\n\n // Parse application/x-www-form-urlencoded\n await app.register(fastifyFormbody);\n\n // Register Routes\n await app.register(dictionaryRouter, { prefix: '/api/dictionary' });\n await app.register(configurationRouter, { prefix: '/api/config' });\n\n // Serve Static Files\n await app.register(fastifyStatic, {\n root: clientDistPath,\n wildcard: false, // We handle the fallback manually to match SPA logic\n });\n\n // For single-page applications, redirect all unmatched routes to index.html\n app.setNotFoundHandler((req, reply) => {\n const requestedPath = join(clientDistPath, req.raw.url || '/');\n\n if (existsSync(requestedPath) && lstatSync(requestedPath).isFile()) {\n const mimeType =\n mime.getType(requestedPath) ?? 'application/octet-stream';\n reply.header('Content-Type', mimeType);\n return reply.sendFile(req.raw.url?.split('/').pop() || 'index.html');\n } else {\n return reply.sendFile('index.html');\n }\n });\n\n try {\n await app.listen({ port, host: '0.0.0.0' });\n\n const dotEnvFilePath = getEnvFilePath(\n envFileOptions.env,\n envFileOptions.envFile\n );\n\n console.log(`\n ${colorize(colorize('INTLAYER', ANSIColors.BOLD), ANSIColors.GREY_DARK)} ${colorize(`v${packageJson.version}`, ANSIColors.GREY_DARK)}\n\n Editor running at: ${colorizePath(`http://localhost:${port}`)}\n ${colorize('➜', ANSIColors.GREY_DARK)} Watching application at: ${config.editor.applicationURL === '' ? '-' : colorizePath(config.editor.applicationURL)}\n ${colorize('➜', ANSIColors.GREY_DARK)} Access key: ${config.editor.clientId ?? '-'}\n ${colorize('➜', ANSIColors.GREY_DARK)} Environment: ${dotEnvFilePath ?? '-'}\n `);\n } catch (err) {\n app.log.error(err);\n process.exit(1);\n }\n};\n\n// Start it up!\nstartServer(app);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAMA,8GAAiD,CAAC;AAExD,MAAM,iBAAiB;CACrB,KAAK,QAAQ,IAAI;CACjB,SAAS,QAAQ,IAAI;AACvB;AAEA,MAAM,gBAAgB;AACtB,MAAM,qDAA0B,cAAc;AAE9C,MAAM,sDAAyB,MAAM;AACrC,MAAM,OAAO,OAAO,OAAO,QAAQ;AAEnC,IAAI,CAAC,OAAO,OAAO,SAAS;CAC1B,UACE,oEAAuC,kBAAkBC,wBAAW,IAAI,EAAE,gDAAmB,oBAAoB,EAAE,sBACnH,EACE,OAAO,QACT,CACF;CACA,QAAQ,KAAK,CAAC;AAChB;AAGA,MAAM,cAAc,KAAK,uDACFD,aAAW,oBAAoB,GAAG,MAAM,CAC/D;AAEA,MAAM,2BAA+B,EACnC,uBAAuB,KACzB,CAAC;AAID,IAAI,SAASE,yBAAQ;AAErB,MAAM,wCAAyBF,aAAW,mBAAmB;AAE7D,MAAM,cAAkC;CACtC,QAAQ;CACR,aAAa;AACf;AAEA,MAAM,cAAc,OAAO,QAAyB;CAGlD,IAAI,CAAC,MAFyBG,0DAAsB,IAAI,GAElC;EACpB,UAAU,eAAe,KAAK,sBAAsB,EAClD,OAAO,QACT,CAAC;EACD,QAAQ,KAAK,GAAG;CAClB;CAGA,MAAM,IAAI,SAASC,yBAAe;EAChC,uBAAuB;EACvB,QAAQ;CACV,CAAC;CAGD,MAAM,IAAI,SAASC,uBAAa,WAAW;CAG3C,MAAM,IAAI,SAASC,yBAAe;CAGlC,MAAM,IAA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