UNPKG

intlayer-editor

Version:

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

1 lines 5.43 kB
{"version":3,"file":"index.mjs","names":["app: Express","corsOptions: CorsOptions"],"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 {\n ANSIColors,\n colorize,\n colorizePath,\n getConfiguration,\n getEnvFilePath,\n} from '@intlayer/config';\nimport { configurationRouter } from '@routes/config.routes';\nimport { dictionaryRouter } from '@routes/dictionary.routes';\nimport { checkPortAvailability } from '@utils/checkPortAvailability';\nimport compression from 'compression';\nimport cookieParser from 'cookie-parser';\nimport cors, { type CorsOptions } from 'cors';\nimport express, { type Express } from 'express';\nimport { intlayer } from 'express-intlayer';\nimport helmet from 'helmet';\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\n// Load package.json\nconst packageJson = JSON.parse(\n readFileSync(resolve(__dirname, '../../package.json'), 'utf8')\n);\n\nconst app: Express = express();\n\n// Load internationalization request handler\napp.use(intlayer());\n\nconst FALLBACK_PORT = 8000;\nconst config = getConfiguration(envFileOptions);\nconst port = config.editor.port ?? FALLBACK_PORT;\n\nconst clientDistPath = resolve(__dirname, '../../client/dist');\n\nconst corsOptions: CorsOptions = {\n origin: '*',\n credentials: true,\n};\n\nconst startServer = async (app: Express) => {\n const isPortAvailable = await checkPortAvailability(port);\n\n if (!isPortAvailable) {\n console.error(`\\x1b[1;31mError: Port ${port} is already in use.\\x1b[0m`);\n process.exit(255);\n }\n\n app.disable('x-powered-by'); // Disabled to prevent attackers from knowing that the app is running Express\n app.use(\n helmet({\n contentSecurityPolicy: false,\n })\n );\n\n app.use(cors(corsOptions));\n\n // Compress all HTTP responses\n app.use(compression());\n\n app.use(express.json());\n\n app.use(cookieParser());\n\n // Parse incoming requests with urlencoded payloads\n app.use(express.urlencoded({ extended: true }));\n\n app.use('/api/dictionary', dictionaryRouter);\n app.use('/api/config', configurationRouter);\n\n app.use(express.static(clientDistPath));\n\n // For single-page applications, redirect all unmatched routes to index.html\n app.get(/(.*)/, (req, res) => {\n const requestedPath = join(clientDistPath, req.url); // Full path of the requested file\n\n if (existsSync(requestedPath) && lstatSync(requestedPath).isFile()) {\n // If the requested file exists, determine its MIME type and serve it\n const mimeType =\n mime.getType(requestedPath) ?? 'application/octet-stream';\n res.setHeader('Content-Type', mimeType);\n res.sendFile(requestedPath);\n } else {\n // Otherwise, serve the index.html for React Router fallback\n res.sendFile(resolve(clientDistPath, 'index.html'));\n }\n });\n\n app.listen(port, () => {\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 });\n};\n\n// Start it up!\nstartServer(app);\n"],"mappings":";;;;;;;;;;;;;;;;AAqBA,MAAM,YAAY,QAAQ,cAAc,OAAO,KAAK,IAAI,CAAC;AAEzD,MAAM,iBAAiB;CACrB;CACA,SAAS,QAAQ,IAAI;CACtB;AAGD,MAAM,cAAc,KAAK,MACvB,aAAa,QAAQ,WAAW,qBAAqB,EAAE,OAAO,CAC/D;AAED,MAAMA,MAAe,SAAS;AAG9B,IAAI,IAAI,UAAU,CAAC;AAEnB,MAAM,gBAAgB;AACtB,MAAM,SAAS,iBAAiB,eAAe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