1#!/usr/bin/env node
2
3const HELP = `
4USAGE
5 zed-local [options] [zed args]
6
7SUMMARY
8 Runs 1-6 instances of Zed using a locally-running collaboration server.
9 Each instance of Zed will be signed in as a different user specified in
10 either \`.admins.json\` or \`.admins.default.json\`.
11
12OPTIONS
13 --help Print this help message
14 --release Build Zed in release mode
15 -2, -3, -4, ... Spawn multiple Zed instances, with their windows tiled.
16 --top Arrange the Zed windows so they take up the top half of the screen.
17`.trim();
18
19const { spawn, execFileSync } = require("child_process");
20const assert = require("assert");
21
22const defaultUsers = require("../crates/collab/.admins.default.json");
23let users = defaultUsers;
24try {
25 const customUsers = require("../crates/collab/.admins.json");
26 assert(customUsers.length > 0);
27 assert(customUsers.every((user) => typeof user === "string"));
28 users = customUsers.concat(
29 defaultUsers.filter((user) => !customUsers.includes(user)),
30 );
31} catch (_) {}
32
33const RESOLUTION_REGEX = /(\d+) x (\d+)/;
34const DIGIT_FLAG_REGEX = /^--?(\d+)$/;
35
36let instanceCount = 1;
37let isReleaseMode = false;
38let isTop = false;
39
40const args = process.argv.slice(2);
41while (args.length > 0) {
42 const arg = args[0];
43
44 const digitMatch = arg.match(DIGIT_FLAG_REGEX);
45 if (digitMatch) {
46 instanceCount = parseInt(digitMatch[1]);
47 } else if (arg === "--release") {
48 isReleaseMode = true;
49 } else if (arg === "--top") {
50 isTop = true;
51 } else if (arg === "--help") {
52 console.log(HELP);
53 process.exit(0);
54 } else {
55 break;
56 }
57
58 args.shift();
59}
60
61// Parse the resolution of the main screen
62const displayInfo = JSON.parse(
63 execFileSync("system_profiler", ["SPDisplaysDataType", "-json"], {
64 encoding: "utf8",
65 }),
66);
67const mainDisplayResolution =
68 displayInfo?.SPDisplaysDataType[0]?.spdisplays_ndrvs
69 ?.find((entry) => entry.spdisplays_main === "spdisplays_yes")
70 ?._spdisplays_resolution?.match(RESOLUTION_REGEX);
71if (!mainDisplayResolution) {
72 throw new Error("Could not parse screen resolution");
73}
74const titleBarHeight = 24;
75const screenWidth = parseInt(mainDisplayResolution[1]);
76let screenHeight = parseInt(mainDisplayResolution[2]) - titleBarHeight;
77
78if (isTop) {
79 screenHeight = Math.floor(screenHeight / 2);
80}
81
82// Determine the window size for each instance
83let rows;
84let columns;
85switch (instanceCount) {
86 case 1:
87 [rows, columns] = [1, 1];
88 break;
89 case 2:
90 [rows, columns] = [1, 2];
91 break;
92 case 3:
93 case 4:
94 [rows, columns] = [2, 2];
95 break;
96 case 5:
97 case 6:
98 [rows, columns] = [2, 3];
99 break;
100}
101
102const instanceWidth = Math.floor(screenWidth / columns);
103const instanceHeight = Math.floor(screenHeight / rows);
104
105// If a user is specified, make sure it's first in the list
106const user = process.env.ZED_IMPERSONATE;
107if (user) {
108 users = [user].concat(users.filter((u) => u !== user));
109}
110
111let buildArgs = ["build"];
112let zedBinary = "target/debug/Zed";
113if (isReleaseMode) {
114 buildArgs.push("--release");
115 zedBinary = "target/release/Zed";
116}
117
118try {
119 execFileSync("cargo", buildArgs, { stdio: "inherit" });
120} catch (e) {
121 process.exit(0);
122}
123
124setTimeout(() => {
125 for (let i = 0; i < instanceCount; i++) {
126 const row = Math.floor(i / columns);
127 const column = i % columns;
128 const position = [
129 column * instanceWidth,
130 row * instanceHeight + titleBarHeight,
131 ].join(",");
132 const size = [instanceWidth, instanceHeight].join(",");
133
134 spawn(zedBinary, i == 0 ? args : [], {
135 stdio: "inherit",
136 env: {
137 ZED_IMPERSONATE: users[i],
138 ZED_WINDOW_POSITION: position,
139 ZED_STATELESS: "1",
140 ZED_ALWAYS_ACTIVE: "1",
141 ZED_SERVER_URL: "http://localhost:3000",
142 ZED_RPC_URL: "http://localhost:8080/rpc",
143 ZED_ADMIN_API_TOKEN: "secret",
144 ZED_WINDOW_SIZE: size,
145 ZED_CLIENT_CHECKSUM_SEED: "development-checksum-seed",
146 PATH: process.env.PATH,
147 RUST_LOG: process.env.RUST_LOG || "info",
148 },
149 });
150 }
151}, 0.1);