From 45d33846cc262ca43f6ad3713e06d4f5eda148c0 Mon Sep 17 00:00:00 2001 From: ittechhunter Date: Sat, 25 May 2024 00:57:01 +0800 Subject: [PATCH] Implement new apps add function --- lib/dev.ts | 49 +++++++++++++++++++++++++-- lib/server.ts | 94 +++++++++++++++++++++++++-------------------------- 2 files changed, 93 insertions(+), 50 deletions(-) diff --git a/lib/dev.ts b/lib/dev.ts index c226dd6..2bf066f 100644 --- a/lib/dev.ts +++ b/lib/dev.ts @@ -46,7 +46,7 @@ export async function dev(src: string, opts: DevOptions) { } // Start the dev server - const server = startDevServer(devJsonPath, opts); + const server = startDevServer([src], [dist], devJsonPath, opts); // Start the socket server if hot reload is enabled if (hotReloadEnabled) { @@ -95,7 +95,10 @@ export async function devMulti(root: string, srcs: string[], opts: DevOptions) { let devJson = { components: {}, data: {} }; // Build all apps for the first time and merge devJson + let dists = []; for (const src of srcs) { + dists.push(path.join(dist, path.relative(root, src))); + const appDevJson = await generateApp( src, path.join(dist, path.relative(root, src)), @@ -107,7 +110,7 @@ export async function devMulti(root: string, srcs: string[], opts: DevOptions) { } // Start the dev server - const server = startDevServer(devJsonPath, opts); + const server = startDevServer(srcs, dists, devJsonPath, opts); // Start the socket server if hot reload is enabled if (!opts.NoHot) { @@ -145,6 +148,48 @@ export async function devMulti(root: string, srcs: string[], opts: DevOptions) { }); } +export async function addApps(srcs: string[], dists: string[], devJsonPath: string, opts: DevOptions) { + let devJson = await readJson(devJsonPath, { throws: false }); + + for (let i = 0; i < srcs.length; i ++) { + const src = srcs[i]; + const dist = dists[i]; + + const appDevJson = await generateApp( + src, + dist, + await loadConfig(src, opts.network), + opts, + devJsonPath + ); + await writeJson(devJsonPath, mergeDeep(devJson, appDevJson)) + } + + startFileWatcher(srcs.map((src) => [path.join(src, "widget/**/*"), path.join(src, "module/**/*"), path.join(src, "ipfs/**/*"), path.join(src, "bos.config.json"), path.join(src, "aliases.json")]).flat(), async (_: string, file: string) => { + // find which app this file belongs to + const index = srcs.findIndex((src) => file.includes(src)); + if (index == -1) { + return; + } + + const src = srcs[index]; + const dist = dists[index]; + + log.info(`[${file}] changed: rebuilding app...`, LogLevels.DEV); + // rebuild app + const appDevJson = await generateApp( + src, + dist, + await loadConfig(src, opts.network), + opts, + devJsonPath + ); + + // write to redirect map + await writeJson(devJsonPath, mergeDeep(devJson, appDevJson)) + }); +} + async function generateApp(src: string, appDist: string, config: BaseConfig, opts: DevOptions, distDevJson: string): Promise { await buildApp(src, appDist, opts.network); return await generateDevJson(appDist, config); diff --git a/lib/server.ts b/lib/server.ts index 7124484..6664560 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -1,4 +1,4 @@ -import { DevJson, DevOptions } from '@/lib/dev'; +import { DevJson, DevOptions, addApps } from '@/lib/dev'; import { fetchJson } from "@near-js/providers"; import bodyParser from "body-parser"; import { exec } from "child_process"; @@ -29,10 +29,46 @@ const SOCIAL_CONTRACT = { * @param opts DevOptions * @returns http server */ -export function startDevServer(devJsonPath: string, opts: DevOptions): http.Server { +export function startDevServer(srcs: string[], dists: string[], devJsonPath: string, opts: DevOptions): http.Server { const app = createApp(devJsonPath, opts); const server = http.createServer(app); - startServer(server, devJsonPath, opts); + startServer(server, opts, () => { + const postData = JSON.stringify({srcs: srcs.map((src) => path.resolve(src)), dists: dists.map((dist) => path.resolve(dist))}); + const options = { + hostname: '127.0.0.1', + port: opts.port, + path: `/api/apps`, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(postData), + }, + }; + + const req = http.request(options, (res) => { + console.log(`STATUS: ${res.statusCode}`); + console.log(`HEADERS: ${JSON.stringify(res.headers)}`); + + res.setEncoding('utf8'); + let data = ''; + + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + console.log(`Response: ${data}`); + }); + }); + + req.on('error', (e) => { + console.log(`problem with request: ${e.message}`); + }); + + // Write data to request body + req.write(postData); + req.end(); + }); return server; } @@ -86,13 +122,13 @@ export function createApp(devJsonPath: string, opts: DevOptions): Express.Applic /** * Adds the loader json */ - app.post("/api/loader", (req, res) => { + app.post("/api/apps", (req, res) => { console.log(`Request: ${JSON.stringify(req.body)}`); + const srcs = req.body.srcs; + const dists = req.body.dists; - readJson(devJsonPath).then((devJson: DevJson) => { - const newDevJson = mergeDeep(devJson, req.body); - writeJson(devJsonPath, newDevJson); - res.json(newDevJson); + addApps(srcs, dists, devJsonPath, opts).then(() => { + res.status(200).send("Success"); }).catch((err: Error) => { log.error(err.stack || err.message); return res.status(500).send("Error reading redirect map."); @@ -201,7 +237,7 @@ export function createApp(devJsonPath: string, opts: DevOptions): Express.Applic * @param server http server * @param opts DevOptions */ -export function startServer(server, devJsonPath, opts) { +export function startServer(server, opts, sendAddApps) { server.listen(opts.port, "127.0.0.1", () => { if (!opts.NoGateway && !opts.NoOpen) { // open gateway in browser @@ -243,45 +279,7 @@ export function startServer(server, devJsonPath, opts) { .on("error", async (err: any) => { if (err.code === "EADDRINUSE") { log.warn(err.message); - - const devJson = await readJson(devJsonPath, { throws: false }); - - const postData = JSON.stringify(devJson); - const options = { - hostname: '127.0.0.1', - port: opts.port, - path: `/api/loader`, - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Content-Length': Buffer.byteLength(postData), - }, - }; - - const req = http.request(options, (res) => { - console.log(`STATUS: ${res.statusCode}`); - console.log(`HEADERS: ${JSON.stringify(res.headers)}`); - - res.setEncoding('utf8'); - let data = ''; - - res.on('data', (chunk) => { - data += chunk; - }); - - res.on('end', () => { - console.log(`Response: ${data}`); - }); - }); - - req.on('error', (e) => { - console.log(`problem with request: ${e.message}`); - }); - - // Write data to request body - req.write(postData); - req.end(); - + sendAddApps(); } else { log.error(err.message); process.exit(1);