Skip to content
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

Support for multiple repositories on a single port #102

Merged
merged 10 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 112 additions & 60 deletions lib/dev.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { readJson, writeJson } from "fs-extra";
import path from "path";
import { Server as IoServer } from "socket.io";
import { Gaze } from "gaze";
import { buildApp } from "./build";
import { BaseConfig, loadConfig, readConfig } from "./config";
import { startDevServer } from "./server";
import { startSocket } from "./socket";
import { Network } from "./types";
import { loopThroughFiles, readFile } from "./utils/fs";
import { mergeDeep } from "./utils/objects";
import { mergeDeep, substractDeep } from "./utils/objects";
import { startFileWatcher } from "./watcher";
import { optional } from "joi";

const DEV_DIST_FOLDER = "build";

var appSrcs = [], appDists = [];
var appDevJsons = [];
var appDevJsonPath = "bos-loader.json";
var appDevOptions: null | DevOptions = null;
let io: null | IoServer = null;
let fileWatcher: null | Gaze = null;

export type DevOptions = {
port?: number; // port to run dev server
NoGateway?: boolean; // disable local gateway
Expand All @@ -29,15 +38,12 @@ export type DevOptions = {
* @param opts DevOptions
*/
export async function dev(src: string, opts: DevOptions) {
let io: null | IoServer = null;

let config = await loadConfig(src, opts.network);
const dist = path.join(src, DEV_DIST_FOLDER);
const devJsonPath = path.join(dist, "bos-loader.json");
const hotReloadEnabled = !opts.NoHot;


// Build the app for the first time
let devJson = await generateApp(src, dist, config, opts, devJsonPath);
const config = await loadConfig(src, opts.network);
let devJson = await generateApp(src, dist, config, opts);
await writeJson(devJsonPath, devJson);

// set index widget (temp, this can be done better)
Expand All @@ -46,10 +52,15 @@ export async function dev(src: string, opts: DevOptions) {
}

// Start the dev server
const server = startDevServer(devJsonPath, opts);
appSrcs = [src];
appDists = [dist];
appDevJsons = [devJson];
appDevJsonPath = devJsonPath;
appDevOptions = opts;
const server = startDevServer(appSrcs, appDists, appDevJsonPath, appDevOptions);

// Start the socket server if hot reload is enabled
if (hotReloadEnabled) {
if (!opts.NoHot) {
io = startSocket(server, (io: IoServer) => {
readJson(devJsonPath).then((devJson: DevJson) => {
io?.emit("fileChange", devJson);
Expand All @@ -61,24 +72,15 @@ export async function dev(src: string, opts: DevOptions) {
}

// Watch for changes in the src folder and rebuild the app on changes
startFileWatcher([
path.join(src, "widget/**/*"),
path.join(src, "module/**/*"),
path.join(src, "ipfs/**/*"),
path.join(src, "bos.config.json"),
path.join(src, "aliases.json")
], async (_: string, file: string) => {
if (file.includes("bos.config.json")) {
config = await loadConfig(src, opts.network);
}
log.info(`[${path.relative(src, file)}] changed: rebuilding app...`, LogLevels.DEV);
devJson = await generateApp(src, dist, config, opts, devJsonPath);
await writeJson(devJsonPath, devJson);

if (hotReloadEnabled && io) {
io.emit("fileChange", devJson);
}
});
fileWatcher = startFileWatcher([
path.join(src, "widget/**/*"),
path.join(src, "module/**/*"),
path.join(src, "ipfs/**/*"),
path.join(src, "bos.config.json"),
path.join(src, "aliases.json")
],
fileWatcherCallback
);
}

/**
Expand All @@ -89,25 +91,26 @@ export async function dev(src: string, opts: DevOptions) {
* @param opts DevOptions
*/
export async function devMulti(root: string, srcs: string[], opts: DevOptions) {
let io: null | IoServer = null;
const dist = path.join(root, DEV_DIST_FOLDER);
const devJsonPath = path.join(dist, "bos-loader.json");
let devJson = { components: {}, data: {} };


// Build all apps for the first time and merge devJson
let appDevJson = { components: {}, data: {} };

for (const src of srcs) {
const appDevJson = await generateApp(
src,
path.join(dist, path.relative(root, src)),
await loadConfig(src, opts.network),
opts,
devJsonPath
);
await writeJson(devJsonPath, mergeDeep(devJson, appDevJson))
const config = await loadConfig(src, opts.network);
const devJson = await generateApp(src, path.join(dist, path.relative(root, src)), config, opts);
await writeJson(devJsonPath, mergeDeep(appDevJson, devJson));

appSrcs.push(src);
appDists.push(path.join(dist, path.relative(root, src)));
appDevJsons.push(devJson);
}

// Start the dev server
const server = startDevServer(devJsonPath, opts);
appDevJsonPath = devJsonPath;
appDevOptions = opts;
const server = startDevServer(appSrcs, appDists, appDevJsonPath, appDevOptions);

// Start the socket server if hot reload is enabled
if (!opts.NoHot) {
Expand All @@ -122,30 +125,79 @@ export async function devMulti(root: string, srcs: string[], opts: DevOptions) {
}

// Watch for changes in the mutliple srcs folder and rebuild apps on changes
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 src = srcs.find((src) => file.includes(src));
if (!src) {
return;
}
log.info(`[${path.relative(src, file)}] changed: rebuilding app...`, LogLevels.DEV);
// rebuild app
const appDevJson = await generateApp(
src,
path.join(dist, path.relative(root, src)),
await loadConfig(src, opts.network),
opts,
devJsonPath
fileWatcher = 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(),
fileWatcherCallback
);
}

export async function addApps(srcs: string[], dists: string[]) {
let appDevJson = await readJson(appDevJsonPath, { throws: false });

for (let i = 0; i < srcs.length; i ++) {
const src = srcs[i];
const dist = dists[i];

const config = await loadConfig(src, appDevOptions.network);
const devJson = await generateApp(src, dist, config, appDevOptions);
await writeJson(appDevJsonPath, mergeDeep(appDevJson, devJson));

appSrcs.push(src);
appDists.push(dist);
appDevJsons.push(devJson);
}

if (io) {
io.emit("fileChange", appDevJson);
}

if (fileWatcher) {
fileWatcher.add(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()
);
// write to redirect map
await writeJson(devJsonPath, mergeDeep(devJson, appDevJson))
if (io) {
io.emit("fileChange", devJson);
}
});
}
}

async function fileWatcherCallback(action: string, file: string) {
let appDevJson = await readJson(appDevJsonPath, { throws: false });

// find which app this file belongs to
const index = appSrcs.findIndex((src) => file.includes(path.resolve(src)));
if (index == -1) {
return;
}

const src = appSrcs[index];
const dist = appDists[index];

let devJson = appDevJsons[index];
substractDeep(appDevJson, devJson);

// rebuild app
log.info(`[${path.relative(src, file)}] changed: rebuilding app...`, LogLevels.DEV);
const config = await loadConfig(src, appDevOptions.network);
devJson = await generateApp(src, dist, config, appDevOptions);

// write to redirect map
await writeJson(appDevJsonPath, mergeDeep(appDevJson, devJson));
appDevJsons[index] = devJson;
if (io) {
io.emit("fileChange", appDevJson);
}
}

async function generateApp(src: string, appDist: string, config: BaseConfig, opts: DevOptions, distDevJson: string): Promise<DevJson> {
async function generateApp(src: string, appDist: string, config: BaseConfig, opts: DevOptions): Promise<DevJson> {
await buildApp(src, appDist, opts.network);
return await generateDevJson(appDist, config);
};
Expand Down
78 changes: 69 additions & 9 deletions lib/server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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";
import express, { Request, Response } from 'express';
import { existsSync, readJson } from "fs-extra";
import { existsSync, readJson, writeJson } from "fs-extra";
import http from 'http';
import path from "path";
import { handleReplacements } from './gateway';
Expand All @@ -28,10 +28,44 @@ 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, 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),
},
};

log.info(`Adding workspace to already existing dev server...`);
const req = http.request(options, (res) => {
res.setEncoding('utf8');
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
log.info(`${data}`);
});
});

req.on('error', (e) => {
log.error(`problem with request: ${e.message}`);
});

// Write data to request body
req.write(postData);
req.end();
});
return server;
}

Expand Down Expand Up @@ -82,6 +116,27 @@ export function createApp(devJsonPath: string, opts: DevOptions): Express.Applic
})
});

/**
* Adds the loader json
*/
app.post("/api/apps", (req, res) => {
const srcs = req.body.srcs;
const dists = req.body.dists;
if (srcs.length != dists.length) {
log.info("Number of apps don't match. Aborting.");
return res.status(500).send("Error adding apps to dev server.");
}

log.info(`adding ${srcs} to watch list...`);
addApps(srcs, dists).then(() => {
log.info("New apps added successfully.");
res.status(200).send("New apps added successfully.");
}).catch((err: Error) => {
log.error(err.stack || err.message);
return res.status(500).send("Error adding apps to dev server.");
});
});

function proxyMiddleware(proxyUrl: string) {
return async (req: Request, res: Response, _) => {
let json = {};
Expand Down Expand Up @@ -139,7 +194,7 @@ export function createApp(devJsonPath: string, opts: DevOptions): Express.Applic

/**
* Proxy middleware for RPC requests
* @param proxyUrl
* @param proxyUrl
*/
app.all('/api/proxy-rpc', proxyMiddleware(RPC_URL[opts.network]));

Expand Down Expand Up @@ -184,7 +239,7 @@ export function createApp(devJsonPath: string, opts: DevOptions): Express.Applic
* @param server http server
* @param opts DevOptions
*/
export function startServer(server, opts) {
export function startServer(server, opts, sendAddApps) {
server.listen(opts.port, "127.0.0.1", () => {
if (!opts.NoGateway && !opts.NoOpen) {
// open gateway in browser
Expand Down Expand Up @@ -223,8 +278,13 @@ export function startServer(server, opts) {
`);
log.success(`bos-workspace running on port ${opts.port}!`);
})
.on("error", (err: Error) => {
log.error(err.message);
process.exit(1);
.on("error", async (err: any) => {
if (err.code === "EADDRINUSE") {
log.warn(err.message);
sendAddApps();
} else {
log.error(err.message);
process.exit(1);
}
});
}
33 changes: 33 additions & 0 deletions lib/utils/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,36 @@ export function mergeDeep(target: any, ...sources: any[]): object {
return mergeDeep(target, ...sources);
}

/**
* Deep substract two objects.
* @param target The target object to substract by.
* @param sources The source objects to substract.
* @returns {object} Returns the substracted object.
*/
export function substractDeep(target: any, ...sources: any[]): object {
if (!sources.length) return target;

const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (!target[key])
continue;

if (isObject(source[key])) {
if (isObject(target[key])) {
substractDeep(target[key], source[key]);
// If target[key] is now an empty object, delete it
if (Object.keys(target[key]).length === 0)
delete target[key];
}
} else {
if (target[key] === source[key])
delete target[key];
}
}
}

// Recursively merge remaining sources
return substractDeep(target, ...sources);
}
Loading
Loading