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 --stable Use stable Zed release installed on local machine for all instances (except for the first one).
18`.trim();
19
20const { spawn, execSync, execFileSync } = require("child_process");
21const assert = require("assert");
22
23let users;
24if (process.env.SEED_PATH) {
25 users = require(process.env.SEED_PATH).admins;
26} else {
27 users = require("../crates/collab/seed.default.json").admins;
28 try {
29 const defaultUsers = users;
30 const customUsers = require("../crates/collab/seed.json").admins;
31 assert(customUsers.length > 0);
32 users = customUsers.concat(
33 defaultUsers.filter((user) => !customUsers.includes(user)),
34 );
35 } catch (_) {}
36}
37
38const RESOLUTION_REGEX = /(\d+) x (\d+)/;
39const DIGIT_FLAG_REGEX = /^--?(\d+)$/;
40
41let instanceCount = 1;
42let isReleaseMode = false;
43let isTop = false;
44let othersOnStable = false;
45let isStateful = false;
46
47const args = process.argv.slice(2);
48while (args.length > 0) {
49 const arg = args[0];
50
51 const digitMatch = arg.match(DIGIT_FLAG_REGEX);
52 if (digitMatch) {
53 instanceCount = parseInt(digitMatch[1]);
54 } else if (arg === "--release") {
55 isReleaseMode = true;
56 } else if (arg == "--stateful") {
57 isStateful = true;
58 } else if (arg === "--top") {
59 isTop = true;
60 } else if (arg === "--help") {
61 console.log(HELP);
62 process.exit(0);
63 } else if (arg === "--stable") {
64 othersOnStable = true;
65 } else {
66 break;
67 }
68
69 args.shift();
70}
71const os = require("os");
72const platform = os.platform();
73
74let screenWidth, screenHeight;
75const titleBarHeight = 24;
76
77if (platform === "darwin") {
78 // macOS
79 const displayInfo = JSON.parse(
80 execFileSync("system_profiler", ["SPDisplaysDataType", "-json"], {
81 encoding: "utf8",
82 }),
83 );
84 const mainDisplayResolution = displayInfo?.SPDisplaysDataType?.flatMap(
85 (display) => display?.spdisplays_ndrvs,
86 )
87 ?.find((entry) => entry?.spdisplays_main === "spdisplays_yes")
88 ?._spdisplays_resolution?.match(RESOLUTION_REGEX);
89 if (!mainDisplayResolution) {
90 throw new Error("Could not parse screen resolution");
91 }
92 screenWidth = parseInt(mainDisplayResolution[1]);
93 screenHeight = parseInt(mainDisplayResolution[2]) - titleBarHeight;
94} else if (platform === "linux") {
95 // Linux
96 try {
97 const xrandrOutput = execSync('xrandr | grep "\\*" | cut -d" " -f4', {
98 encoding: "utf8",
99 }).trim();
100 [screenWidth, screenHeight] = xrandrOutput.split("x").map(Number);
101 } catch (err) {
102 console.log(err);
103 throw new Error("Could not get screen resolution");
104 }
105}
106
107screenHeight -= titleBarHeight;
108
109if (isTop) {
110 screenHeight = Math.floor(screenHeight / 2);
111}
112
113// Determine the window size for each instance
114let rows;
115let columns;
116switch (instanceCount) {
117 case 1:
118 [rows, columns] = [1, 1];
119 break;
120 case 2:
121 [rows, columns] = [1, 2];
122 break;
123 case 3:
124 case 4:
125 [rows, columns] = [2, 2];
126 break;
127 case 5:
128 case 6:
129 [rows, columns] = [2, 3];
130 break;
131}
132
133const instanceWidth = Math.floor(screenWidth / columns);
134const instanceHeight = Math.floor(screenHeight / rows);
135
136// If a user is specified, make sure it's first in the list
137const user = process.env.ZED_IMPERSONATE;
138if (user) {
139 users = [user].concat(users.filter((u) => u !== user));
140}
141
142let buildArgs = ["build"];
143let zedBinary = "target/debug/zed";
144if (isReleaseMode) {
145 buildArgs.push("--release");
146 zedBinary = "target/release/zed";
147}
148
149try {
150 execFileSync("cargo", buildArgs, {
151 stdio: "inherit",
152 });
153} catch (e) {
154 process.exit(0);
155}
156
157setTimeout(() => {
158 for (let i = 0; i < instanceCount; i++) {
159 const row = Math.floor(i / columns);
160 const column = i % columns;
161 const position = [
162 column * instanceWidth,
163 row * instanceHeight + titleBarHeight,
164 ].join(",");
165 const size = [instanceWidth, instanceHeight].join(",");
166 let binaryPath = zedBinary;
167 if (i != 0 && othersOnStable) {
168 binaryPath = "/Applications/Zed.app/Contents/MacOS/zed";
169 }
170 spawn(binaryPath, i == 0 ? args : [], {
171 stdio: "inherit",
172 env: Object.assign({}, process.env, {
173 ZED_IMPERSONATE: users[i],
174 ZED_WINDOW_POSITION: position,
175 ZED_STATELESS: isStateful && i == 0 ? "" : "1",
176 ZED_ALWAYS_ACTIVE: "1",
177 ZED_SERVER_URL: "http://localhost:3000",
178 ZED_RPC_URL: "http://localhost:8080/rpc",
179 ZED_ADMIN_API_TOKEN: "secret",
180 ZED_WINDOW_SIZE: size,
181 ZED_CLIENT_CHECKSUM_SEED: "development-checksum-seed",
182 RUST_LOG: process.env.RUST_LOG || "info",
183 }),
184 });
185 }
186}, 0.1);