-
Notifications
You must be signed in to change notification settings - Fork 804
/
index.ts
58 lines (47 loc) · 1.46 KB
/
index.ts
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
import { ChemicalServer } from "chemicaljs";
import express from "express";
import fs from "fs";
import { execSync } from "child_process";
import chalk from "chalk";
import path from "path";
import { Request, Response } from "express";
import pages from "./src/pages.json";
import themes from "./src/themes.json";
const [app, listen] = new ChemicalServer();
const __dirname = path.resolve();
if (!fs.existsSync("build")) {
console.log("No build found. Building...");
execSync("npm run build");
console.log("Built!");
}
const port = process.env.PORT || 3000;
app.use(express.static("build"));
app.serveChemical();
app.use((req: Request, res: Response) => {
if (pages.includes(req.url)) {
return res.sendFile(__dirname + "/build/index.html");
} else {
return res.status(404).sendFile(__dirname + "/build/index.html");
}
});
listen(port, () => {
const theme = chalk.hex(
themes.filter((theme) => (theme.id = "default"))[0].theme.primary
);
console.log(`${chalk.bold(theme("Metallic"))}`);
console.log(`- ${chalk.bold("Local:")} http://localhost:${port}`);
if (process.env.REPL_SLUG && process.env.REPL_OWNER) {
console.log(
`- ${chalk.bold("Replit:")} https://${process.env.REPL_SLUG}.${
process.env.REPL_OWNER
}.repl.co`
);
}
if (process.env.HOSTNAME && process.env.GITPOD_WORKSPACE_CLUSTER_HOST) {
console.log(
`- ${chalk.bold("Gitpod:")} https://${port}-${process.env.HOSTNAME}.${
process.env.GITPOD_WORKSPACE_CLUSTER_HOST
}`
);
}
});