1#!/usr/bin/env node
2
3const { spawn, execFileSync } = require("child_process");
4
5const RESOLUTION_REGEX = /(\d+) x (\d+)/;
6const DIGIT_FLAG_REGEX = /^--?(\d+)$/;
7const ZED_2_MODE = "--zed2";
8
9const args = process.argv.slice(2);
10
11// Parse the number of Zed instances to spawn.
12let instanceCount = 1;
13const digitMatch = args[0]?.match(DIGIT_FLAG_REGEX);
14if (digitMatch) {
15 instanceCount = parseInt(digitMatch[1]);
16 args.shift();
17}
18const isZed2 = args.some((arg) => arg === ZED_2_MODE);
19if (instanceCount > 4) {
20 throw new Error("Cannot spawn more than 4 instances");
21}
22
23// Parse the resolution of the main screen
24const displayInfo = JSON.parse(
25 execFileSync("system_profiler", ["SPDisplaysDataType", "-json"], {
26 encoding: "utf8",
27 }),
28);
29const mainDisplayResolution =
30 displayInfo?.SPDisplaysDataType[0]?.spdisplays_ndrvs
31 ?.find((entry) => entry.spdisplays_main === "spdisplays_yes")
32 ?._spdisplays_resolution?.match(RESOLUTION_REGEX);
33if (!mainDisplayResolution) {
34 throw new Error("Could not parse screen resolution");
35}
36const screenWidth = parseInt(mainDisplayResolution[1]);
37const screenHeight = parseInt(mainDisplayResolution[2]);
38
39// Determine the window size for each instance
40let instanceWidth = screenWidth;
41let instanceHeight = screenHeight;
42if (instanceCount > 1) {
43 instanceWidth = Math.floor(screenWidth / 2);
44 if (instanceCount > 2) {
45 instanceHeight = Math.floor(screenHeight / 2);
46 }
47}
48
49let users = ["nathansobo", "as-cii", "maxbrunsfeld", "iamnbutler"];
50
51const RUST_LOG = process.env.RUST_LOG || "info";
52
53// If a user is specified, make sure it's first in the list
54const user = process.env.ZED_IMPERSONATE;
55if (user) {
56 users = [user].concat(users.filter((u) => u !== user));
57}
58
59const positions = [
60 "0,0",
61 `${instanceWidth},0`,
62 `0,${instanceHeight}`,
63 `${instanceWidth},${instanceHeight}`,
64];
65
66const buildArgs = isZed2 ? ["build", "-p", "zed2"] : ["build"];
67const zedBinary = isZed2 ? "target/debug/Zed2" : "target/debug/Zed";
68execFileSync("cargo", buildArgs, { stdio: "inherit" });
69setTimeout(() => {
70 for (let i = 0; i < instanceCount; i++) {
71 spawn(zedBinary, i == 0 ? args : [], {
72 stdio: "inherit",
73 env: {
74 ZED_IMPERSONATE: users[i],
75 ZED_WINDOW_POSITION: positions[i],
76 ZED_STATELESS: "1",
77 ZED_ALWAYS_ACTIVE: "1",
78 ZED_SERVER_URL: "http://localhost:8080",
79 ZED_ADMIN_API_TOKEN: "secret",
80 ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}`,
81 PATH: process.env.PATH,
82 RUST_LOG,
83 },
84 });
85 }
86}, 0.1);