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 RELEASE_MODE = "--release";
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 isReleaseMode = args.some((arg) => arg === RELEASE_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 = (() => {
67 const buildArgs = ["build"];
68 if (isReleaseMode) {
69 buildArgs.push("--release");
70 }
71
72 return buildArgs;
73})();
74const zedBinary = (() => {
75 const target = isReleaseMode ? "release" : "debug";
76 return `target/${target}/Zed`;
77})();
78
79execFileSync("cargo", buildArgs, { stdio: "inherit" });
80setTimeout(() => {
81 for (let i = 0; i < instanceCount; i++) {
82 spawn(zedBinary, i == 0 ? args : [], {
83 stdio: "inherit",
84 env: {
85 ZED_IMPERSONATE: users[i],
86 ZED_WINDOW_POSITION: positions[i],
87 ZED_STATELESS: "1",
88 ZED_ALWAYS_ACTIVE: "1",
89 ZED_SERVER_URL: "http://localhost:8080",
90 ZED_ADMIN_API_TOKEN: "secret",
91 ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}`,
92 PATH: process.env.PATH,
93 RUST_LOG,
94 },
95 });
96 }
97}, 0.1);