forked from hathora/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
executable file
·150 lines (139 loc) · 4.83 KB
/
cli.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env node
import { createHash } from "crypto";
import { outputFileSync, existsSync, readdirSync } from "fs-extra";
import { join } from "path";
import { pathToFileURL } from "url";
import shelljs from "shelljs";
import { v4 as uuidv4 } from "uuid";
import { createServer, build as buildClient } from "vite";
import { build as buildServer } from "esbuild";
import { generate } from "./generate";
import "./helpers";
function getProjectRoot(cwd: string): string {
if (existsSync(join(cwd, "hathora.yml"))) {
return cwd;
}
const parentDir = join(cwd, "..");
if (parentDir === cwd) {
throw new Error("Doesn't appear to be inside a hathora project");
}
return getProjectRoot(parentDir);
}
function getCommand(argv: string[]) {
return argv.length <= 2 ? "generate" : argv[2];
}
function npmInstall(dir: string) {
console.log(`Installing dependencies in ${dir}`);
if (existsSync(join(dir, "yarn.lock"))) {
shelljs.exec(`yarn install --cwd ${dir}`);
} else if (existsSync(join(dir, "package.json"))) {
shelljs.cd(dir);
shelljs.exec("npm install");
}
}
function install() {
npmInstall(join(rootDir, "api"));
readdirSync(clientDir).forEach((dir) => npmInstall(join(clientDir, dir)));
if (existsSync(join(clientDir, "prototype-ui", "plugins"))) {
readdirSync(join(clientDir, "prototype-ui", "plugins")).forEach((dir) =>
npmInstall(join(clientDir, "prototype-ui", "plugins", dir))
);
}
npmInstall(serverDir);
npmInstall(join(serverDir, ".hathora"));
}
async function startServer() {
shelljs.cd(join(serverDir, ".hathora"));
process.env.DATA_DIR = join(serverDir, ".hathora/data");
process.env.DOTENV_CONFIG_PATH = join(rootDir, ".env");
process.env.NODE_LOADER_CONFIG = join(__dirname, "node-loader.config.mjs");
const loaderPath = pathToFileURL(require.resolve("@node-loader/core/lib/node-loader-core.js"));
const storePath = join(serverDir, ".hathora/store.ts");
const cp = shelljs.exec(`node --loader ${loaderPath} --experimental-specifier-resolution=node ${storePath}`, {
async: true,
});
return new Promise((resolve, reject) => {
cp.stdout?.on("data", resolve);
cp.on("error", reject);
});
}
async function startFrontend(root: string) {
console.log(`Starting frontend at ${root}`);
return createServer({
root,
build: { target: ["esnext"] },
envDir: rootDir,
clearScreen: false,
server: { host: "0.0.0.0" },
})
.then((server) => server.listen())
.then((server) => server.printUrls());
}
async function startFrontends() {
for (const dir of readdirSync(clientDir)) {
if (existsSync(join(clientDir, dir, "index.html"))) {
await startFrontend(join(clientDir, dir));
}
}
}
const rootDir = getProjectRoot(process.cwd());
const clientDir = join(rootDir, "client");
const serverDir = join(rootDir, "server");
const appSecret = process.env.APP_SECRET ?? uuidv4();
const appId = createHash("sha256").update(appSecret).digest("hex");
if (!existsSync(join(rootDir, ".env"))) {
outputFileSync(join(rootDir, ".env"), `APP_SECRET=${appSecret}\nVITE_APP_ID=${appId}\n`);
}
console.log(`Project root: ${rootDir}`);
const command = getCommand(process.argv);
if (command === "init") {
if (existsSync(join(serverDir, "impl.ts"))) {
console.error("Cannot init inside existing project, delete impl.ts to regenerate");
} else {
generate(rootDir, "templates/lang/ts");
generate(rootDir, "templates/base");
}
} else if (command === "generate") {
if (!existsSync(join(serverDir, "impl.ts"))) {
console.error("Missing impl.ts, make sure to run hathora init first");
} else {
generate(rootDir, "templates/base");
}
} else if (command === "install") {
install();
} else if (command === "start") {
startServer().then(startFrontends);
} else if (command === "dev") {
if (!existsSync(join(serverDir, "impl.ts"))) {
console.error("Missing impl.ts, make sure to run hathora init first");
} else {
generate(rootDir, "templates/base");
}
install();
startServer().then(startFrontends);
} else if (command === "build") {
process.env.VITE_APP_ID = appId;
for (const dir of readdirSync(clientDir)) {
if (existsSync(join(clientDir, dir, "index.html"))) {
buildClient({
root: join(clientDir, dir),
build: { outDir: join(rootDir, "dist", "client", dir), target: ["esnext"] },
clearScreen: false,
});
}
}
const outDir = join(rootDir, "dist", "server");
outputFileSync(join(outDir, ".env"), `APP_SECRET=${appSecret}\n`);
buildServer({
entryPoints: [join(serverDir, ".hathora", "store.ts")],
bundle: true,
platform: "node",
format: "esm",
outfile: join(outDir, "index.mjs"),
banner: {
js: "import { createRequire as topLevelCreateRequire } from 'module';\n const require = topLevelCreateRequire(import.meta.url);",
},
});
} else {
console.error(`Unknown command: ${command}`);
}