-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathrundev.js
103 lines (88 loc) · 2.63 KB
/
rundev.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
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
/**
* @file Development script, which performs the following actions.
*
* 1. Runs `npm run build -- --watch` on the functions directory.
* 2. Starts the Firebase Local Emulator Suite.
* 3. Manually executes the `clearConnections` function on a schedule.
* 4. Starts the frontend React app with Fast Refresh enabled.
*/
import { PubSub } from "@google-cloud/pubsub";
import { spawn, spawnSync } from "node:child_process";
import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
// Patch for __dirname not being available in ES modules.
const __dirname = path.dirname(fileURLToPath(import.meta.url));
console.log("[setwithfriends] Starting development script...");
let build, emulators, app;
// Initial build
spawnSync("npm", ["run", "build"], {
cwd: path.join(__dirname, "functions"),
stdio: ["ignore", "inherit", "inherit"],
});
// Incremental watch builds
build = spawn(
"npm",
["run", "build", "--", "--watch", "--preserveWatchOutput"],
{
cwd: path.join(__dirname, "functions"),
stdio: ["ignore", "inherit", "inherit"],
},
);
// Start emulators
emulators = spawn(
"firebase",
[
"emulators:start",
"--project",
"staging",
"--import=./data",
"--export-on-exit",
],
{
cwd: __dirname,
stdio: ["ignore", "inherit", "inherit"],
},
);
// Frontend application
app = spawn("npm", ["run", "dev"], {
cwd: __dirname,
stdio: ["ignore", "pipe", "inherit"],
env: Object.assign({ FORCE_COLOR: true }, process.env),
});
app.stdout.pipe(process.stdout);
const pubsub = new PubSub({
apiEndpoint: "localhost:8085",
projectId: "setwithfriends-dev",
});
// This is a workaround for the Pub/Sub emulator not supporting scheduled functions.
// https://github.com/firebase/firebase-tools/issues/2034
const pubsubIntervals = [
setInterval(async () => {
await pubsub
.topic("firebase-schedule-clearConnections")
.publishMessage({ json: {} });
}, 60 * 1000), // every minute
setInterval(async () => {
await pubsub
.topic("firebase-schedule-archiveStaleGames")
.publishMessage({ json: {} });
}, 3600 * 1000), // every hour
];
let shutdownCalled = false;
async function shutdown() {
if (shutdownCalled) return;
shutdownCalled = true;
for (const interval of pubsubIntervals) {
clearInterval(interval);
}
const waitForChild = (p) => new Promise((resolve) => p.on("exit", resolve));
await Promise.all([
waitForChild(build),
waitForChild(emulators),
waitForChild(app),
]);
console.log("[setwithfriends] Finished development script, goodbye!");
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);