|
| 1 | +// Import Node.js Dependencies |
| 2 | +import fsAsync from "node:fs/promises"; |
| 3 | +import http from "node:http"; |
| 4 | +import path from "node:path"; |
| 5 | + |
| 6 | +// Import Third-party Dependencies |
| 7 | +import { getBuildConfiguration } from "@nodesecure/documentation-ui/node"; |
| 8 | +import * as i18n from "@nodesecure/i18n"; |
| 9 | +import chokidar from "chokidar"; |
| 10 | +import esbuild from "esbuild"; |
| 11 | +import open from "open"; |
| 12 | +import sirv from "sirv"; |
| 13 | + |
| 14 | +// Import Internal Dependencies |
| 15 | +import english from "./i18n/english.js"; |
| 16 | +import french from "./i18n/french.js"; |
| 17 | +import { context as als, type AsyncStoreContext } from "./workspaces/server/src/ALS.ts"; |
| 18 | +import { ViewBuilder } from "./workspaces/server/src/ViewBuilder.class.ts"; |
| 19 | +import { cache } from "./workspaces/server/src/cache.ts"; |
| 20 | +import { getApiRouter } from "./workspaces/server/src/endpoints/index.ts"; |
| 21 | +import { logger } from "./workspaces/server/src/logger.ts"; |
| 22 | +import { WebSocketServerInstanciator } from "./workspaces/server/src/websocket/index.ts"; |
| 23 | + |
| 24 | +// CONSTANTS |
| 25 | +const kPublicDir = path.join(import.meta.dirname, "public"); |
| 26 | +const kOutDir = path.join(import.meta.dirname, "dist"); |
| 27 | +const kImagesDir = path.join(kPublicDir, "img"); |
| 28 | +const kNodeModulesDir = path.join(import.meta.dirname, "node_modules"); |
| 29 | +const kComponentsDir = path.join(kPublicDir, "components"); |
| 30 | +const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json"); |
| 31 | +const kDevPort = Number(process.env.DEV_PORT ?? 8080); |
| 32 | + |
| 33 | +await Promise.all([ |
| 34 | + i18n.getLocalLang(), |
| 35 | + i18n.extendFromSystemPath(path.join(import.meta.dirname, "i18n")) |
| 36 | +]); |
| 37 | + |
| 38 | +const imagesFiles = await fsAsync.readdir(kImagesDir); |
| 39 | + |
| 40 | +await Promise.all([ |
| 41 | + ...imagesFiles |
| 42 | + .map((name) => fsAsync.copyFile(path.join(kImagesDir, name), path.join(kOutDir, name))), |
| 43 | + fsAsync.copyFile(path.join(kPublicDir, "favicon.ico"), path.join(kOutDir, "favicon.ico")) |
| 44 | +]); |
| 45 | + |
| 46 | +const buildContext = await esbuild.context({ |
| 47 | + entryPoints: [ |
| 48 | + path.join(kPublicDir, "main.js"), |
| 49 | + path.join(kPublicDir, "main.css"), |
| 50 | + path.join(kNodeModulesDir, "highlight.js", "styles", "github.css"), |
| 51 | + ...getBuildConfiguration().entryPoints |
| 52 | + ], |
| 53 | + |
| 54 | + loader: { |
| 55 | + ".jpg": "file", |
| 56 | + ".png": "file", |
| 57 | + ".woff": "file", |
| 58 | + ".woff2": "file", |
| 59 | + ".eot": "file", |
| 60 | + ".ttf": "file", |
| 61 | + ".svg": "file" |
| 62 | + }, |
| 63 | + platform: "browser", |
| 64 | + bundle: true, |
| 65 | + sourcemap: true, |
| 66 | + treeShaking: true, |
| 67 | + outdir: kOutDir |
| 68 | +}); |
| 69 | + |
| 70 | +await buildContext.watch(); |
| 71 | + |
| 72 | +const { hosts: esbuildHosts, port: esbuildPort } = await buildContext.serve({ |
| 73 | + servedir: kOutDir |
| 74 | +}); |
| 75 | + |
| 76 | +const dataFilePath = await fsAsync.access(kDefaultPayloadPath).then(() => kDefaultPayloadPath, () => undefined); |
| 77 | + |
| 78 | +if (dataFilePath === undefined) { |
| 79 | + cache.startFromZero = true; |
| 80 | +} |
| 81 | + |
| 82 | +const store: AsyncStoreContext = { |
| 83 | + i18n: { english: { ui: english.ui }, french: { ui: french.ui } }, |
| 84 | + viewBuilder: new ViewBuilder({ |
| 85 | + projectRootDir: import.meta.dirname, |
| 86 | + componentsDir: kComponentsDir |
| 87 | + }), |
| 88 | + dataFilePath |
| 89 | +}; |
| 90 | +const htmlWatcher = chokidar.watch(kComponentsDir, { |
| 91 | + persistent: false, |
| 92 | + awaitWriteFinish: true, |
| 93 | + ignored: (path, stats) => (stats?.isFile() ?? false) && !path.endsWith(".html") |
| 94 | +}); |
| 95 | + |
| 96 | +htmlWatcher.on("change", async(filePath) => { |
| 97 | + await buildContext.rebuild().catch(console.error); |
| 98 | + store.viewBuilder.freeCache(filePath); |
| 99 | +}); |
| 100 | + |
| 101 | +const serving = sirv(kOutDir, { dev: true }); |
| 102 | + |
| 103 | +function defaultRoute(req: http.IncomingMessage, res: http.ServerResponse) { |
| 104 | + if (req.url === "/esbuild") { |
| 105 | + const proxyReq = http.request( |
| 106 | + { hostname: esbuildHosts[0], port: esbuildPort, path: req.url, method: req.method, headers: req.headers }, |
| 107 | + (proxyRes) => { |
| 108 | + res.writeHead(proxyRes.statusCode!, proxyRes.headers); |
| 109 | + proxyRes.pipe(res); |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + proxyReq.on("error", (err) => { |
| 114 | + console.error(`[proxy/esbuild] ${err.message}`); |
| 115 | + res.writeHead(502); |
| 116 | + res.end("Bad Gateway"); |
| 117 | + }); |
| 118 | + |
| 119 | + req.pipe(proxyReq); |
| 120 | + |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + serving(req, res, () => { |
| 125 | + res.writeHead(404); |
| 126 | + res.end("Not Found"); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +const apiRouter = getApiRouter(defaultRoute); |
| 131 | + |
| 132 | +const httpServer = http.createServer((req, res) => als.run(store, () => apiRouter.lookup(req, res))) |
| 133 | + .listen(kDevPort, () => { |
| 134 | + console.log(`Dev server: http://localhost:${kDevPort}`); |
| 135 | + open(`http://localhost:${kDevPort}`); |
| 136 | + }); |
| 137 | + |
| 138 | +new WebSocketServerInstanciator({ cache, logger }); |
| 139 | + |
| 140 | +console.log("Watching..."); |
0 commit comments