-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev.js
78 lines (71 loc) · 1.95 KB
/
dev.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { networkInterfaces } from "node:os";
import { spawnSync } from "node:child_process";
import { build } from "esbuild";
import chokidar from "chokidar";
import express from "express";
import serveStatic from "serve-static";
import cors from "cors";
const baseUrl = process.argv[2];
if (!baseUrl) {
console.log("Error! You must specify the URL of the Switch application!");
process.exit(2);
}
console.log(`Switch URL: ${new URL(baseUrl)}`);
const url = new URL("/__dev", baseUrl);
const host = Object.values(networkInterfaces())
.flatMap((i) => i)
.find((i) => !i.internal && i.family === "IPv4");
if (!host) {
throw new Error("Could not determine host IP address");
}
const DEV_HOST = `http://${host.address}:8080/`;
console.log(` Host URL: ${DEV_HOST}`);
const app = express();
app.use(cors());
app.use(serveStatic("build/client"));
app.listen(8080);
chokidar
.watch("app", { ignoreInitial: true })
.on("all", async (event, path) => {
console.log(`${event}: ${path}`);
console.time("Rebuilding");
try {
spawnSync("remix", ["vite:build"], {
stdio: "inherit",
env: {
...process.env,
DEV_HOST,
},
});
} catch (err) {
console.error(err);
return;
} finally {
console.timeEnd("Rebuilding");
}
const bundle = await build({
entryPoints: ["build/server/index.js"],
bundle: true,
write: false,
format: "cjs",
});
const output = bundle.outputFiles[0];
console.log("Updating dev server");
console.time("Dev server ready");
try {
await fetch(url, {
method: "POST",
body: output.contents,
});
} catch (err) {
if (err.cause?.code === "ECONNREFUSED") {
console.error(
"ERROR: Please ensure the Remix app is running on your Switch in Development Mode"
);
} else {
console.error(err);
}
} finally {
console.timeEnd("Dev server ready");
}
});