-
Notifications
You must be signed in to change notification settings - Fork 51
tech: enable watch mode & esbuild server in dev mode #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ErwanRaulo
wants to merge
1
commit into
NodeSecure:master
Choose a base branch
from
ErwanRaulo:watch-mode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Import Node.js Dependencies | ||
| import fsAsync from "node:fs/promises"; | ||
| import http from "node:http"; | ||
| import path from "node:path"; | ||
|
|
||
| // Import Third-party Dependencies | ||
| import { getBuildConfiguration } from "@nodesecure/documentation-ui/node"; | ||
| import * as i18n from "@nodesecure/i18n"; | ||
| import chokidar from "chokidar"; | ||
| import esbuild from "esbuild"; | ||
| import open from "open"; | ||
| import sirv from "sirv"; | ||
|
|
||
| // Import Internal Dependencies | ||
| import english from "./i18n/english.js"; | ||
| import french from "./i18n/french.js"; | ||
| import { context as alsContext } from "./workspaces/server/src/ALS.ts"; | ||
| import { ViewBuilder } from "./workspaces/server/src/ViewBuilder.class.ts"; | ||
| import { cache } from "./workspaces/server/src/cache.ts"; | ||
| import { getApiRouter } from "./workspaces/server/src/endpoints/index.ts"; | ||
| import { logger } from "./workspaces/server/src/logger.ts"; | ||
| import { WebSocketServerInstanciator } from "./workspaces/server/src/websocket/index.ts"; | ||
|
|
||
| // CONSTANTS | ||
| const kPublicDir = path.join(import.meta.dirname, "public"); | ||
| const kOutDir = path.join(import.meta.dirname, "dist"); | ||
| const kImagesDir = path.join(kPublicDir, "img"); | ||
| const kNodeModulesDir = path.join(import.meta.dirname, "node_modules"); | ||
| const kComponentsDir = path.join(kPublicDir, "components"); | ||
| const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json"); | ||
| const kDevPort = Number(process.env.DEV_PORT ?? 8080); | ||
|
|
||
| await Promise.all([ | ||
| i18n.getLocalLang(), | ||
| i18n.extendFromSystemPath(path.join(import.meta.dirname, "i18n")) | ||
| ]); | ||
|
|
||
| const imagesFiles = await fsAsync.readdir(kImagesDir); | ||
|
|
||
| await Promise.all([ | ||
| ...imagesFiles | ||
| .map((name) => fsAsync.copyFile(path.join(kImagesDir, name), path.join(kOutDir, name))), | ||
| fsAsync.copyFile(path.join(kPublicDir, "favicon.ico"), path.join(kOutDir, "favicon.ico")) | ||
| ]); | ||
|
|
||
| const buildContext = await esbuild.context({ | ||
| entryPoints: [ | ||
| path.join(kPublicDir, "main.js"), | ||
| path.join(kPublicDir, "main.css"), | ||
| path.join(kNodeModulesDir, "highlight.js", "styles", "github.css"), | ||
| ...getBuildConfiguration().entryPoints | ||
| ], | ||
|
|
||
| loader: { | ||
| ".jpg": "file", | ||
| ".png": "file", | ||
| ".woff": "file", | ||
| ".woff2": "file", | ||
| ".eot": "file", | ||
| ".ttf": "file", | ||
| ".svg": "file" | ||
| }, | ||
| platform: "browser", | ||
| bundle: true, | ||
| sourcemap: true, | ||
| treeShaking: true, | ||
| outdir: kOutDir | ||
| }); | ||
|
|
||
| await buildContext.watch(); | ||
|
|
||
| const { hosts: esbuildHosts, port: esbuildPort } = await buildContext.serve({ | ||
| servedir: kOutDir | ||
| }); | ||
|
|
||
| const htmlWatcher = chokidar.watch(kComponentsDir, { | ||
| persistent: false, | ||
| awaitWriteFinish: true, | ||
| ignored: (path, stats) => (stats?.isFile() ?? false) && !path.endsWith(".html") | ||
| }); | ||
|
|
||
| const dataFilePath = await fsAsync.access(kDefaultPayloadPath).then(() => kDefaultPayloadPath, () => undefined); | ||
|
|
||
| if (dataFilePath === undefined) { | ||
| cache.startFromZero = true; | ||
| } | ||
|
|
||
| const store = { | ||
| i18n: { english: { ui: english.ui }, french: { ui: french.ui } }, | ||
| viewBuilder: new ViewBuilder({ | ||
| projectRootDir: import.meta.dirname, | ||
| componentsDir: kComponentsDir | ||
| }), | ||
| dataFilePath | ||
| }; | ||
|
|
||
| htmlWatcher.on("change", async(filePath) => { | ||
| await buildContext.rebuild().catch(console.error); | ||
| store.viewBuilder.freeCache(filePath); | ||
| }); | ||
|
|
||
| const serving = sirv(kOutDir, { dev: true }); | ||
|
|
||
| function defaultRoute(req: http.IncomingMessage, res: http.ServerResponse) { | ||
| if (req.url === "/esbuild") { | ||
| const proxyReq = http.request( | ||
| { hostname: esbuildHosts[0], port: esbuildPort, path: req.url, method: req.method, headers: req.headers }, | ||
| (proxyRes) => { | ||
| res.writeHead(proxyRes.statusCode!, proxyRes.headers); | ||
| proxyRes.pipe(res); | ||
| } | ||
| ); | ||
|
|
||
| proxyReq.on("error", (err) => { | ||
| console.error(`[proxy/esbuild] ${err.message}`); | ||
| res.writeHead(502); | ||
| res.end("Bad Gateway"); | ||
| }); | ||
|
|
||
| req.pipe(proxyReq); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| serving(req, res, () => { | ||
| res.writeHead(404); | ||
| res.end("Not Found"); | ||
| }); | ||
| } | ||
|
|
||
| const apiRouter = getApiRouter(defaultRoute); | ||
|
|
||
| http.createServer((req, res) => alsContext.run(store, () => apiRouter.lookup(req, res))) | ||
| .listen(kDevPort, () => { | ||
| console.log(`Dev server: http://localhost:${kDevPort}`); | ||
| open(`http://localhost:${kDevPort}`); | ||
| }); | ||
|
|
||
| new WebSocketServerInstanciator({ cache, logger }); | ||
|
|
||
| console.log("Watching..."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conflicts with -d of --depth, giving Infinity as value instead of boolean.
dev mode was always enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think to update the documentation (./docs/cli) for commands such as auto, etc