zed-local

  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, 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}
 71
 72// Parse the resolution of the main screen
 73const displayInfo = JSON.parse(
 74  execFileSync("system_profiler", ["SPDisplaysDataType", "-json"], {
 75    encoding: "utf8",
 76  }),
 77);
 78const mainDisplayResolution = displayInfo?.SPDisplaysDataType?.flatMap(
 79  (display) => display?.spdisplays_ndrvs,
 80)
 81  ?.find((entry) => entry?.spdisplays_main === "spdisplays_yes")
 82  ?._spdisplays_resolution?.match(RESOLUTION_REGEX);
 83if (!mainDisplayResolution) {
 84  throw new Error("Could not parse screen resolution");
 85}
 86const titleBarHeight = 24;
 87const screenWidth = parseInt(mainDisplayResolution[1]);
 88let screenHeight = parseInt(mainDisplayResolution[2]) - titleBarHeight;
 89
 90if (isTop) {
 91  screenHeight = Math.floor(screenHeight / 2);
 92}
 93
 94// Determine the window size for each instance
 95let rows;
 96let columns;
 97switch (instanceCount) {
 98  case 1:
 99    [rows, columns] = [1, 1];
100    break;
101  case 2:
102    [rows, columns] = [1, 2];
103    break;
104  case 3:
105  case 4:
106    [rows, columns] = [2, 2];
107    break;
108  case 5:
109  case 6:
110    [rows, columns] = [2, 3];
111    break;
112}
113
114const instanceWidth = Math.floor(screenWidth / columns);
115const instanceHeight = Math.floor(screenHeight / rows);
116
117// If a user is specified, make sure it's first in the list
118const user = process.env.ZED_IMPERSONATE;
119if (user) {
120  users = [user].concat(users.filter((u) => u !== user));
121}
122
123let buildArgs = ["build"];
124let zedBinary = "target/debug/Zed";
125if (isReleaseMode) {
126  buildArgs.push("--release");
127  zedBinary = "target/release/Zed";
128}
129
130try {
131  execFileSync("cargo", buildArgs, { stdio: "inherit" });
132} catch (e) {
133  process.exit(0);
134}
135
136setTimeout(() => {
137  for (let i = 0; i < instanceCount; i++) {
138    const row = Math.floor(i / columns);
139    const column = i % columns;
140    const position = [
141      column * instanceWidth,
142      row * instanceHeight + titleBarHeight,
143    ].join(",");
144    const size = [instanceWidth, instanceHeight].join(",");
145    let binaryPath = zedBinary;
146    if (i != 0 && othersOnStable) {
147      binaryPath = "/Applications/Zed.app/Contents/MacOS/zed";
148    }
149    spawn(binaryPath, i == 0 ? args : [], {
150      stdio: "inherit",
151      env: Object.assign({}, process.env, {
152        ZED_IMPERSONATE: users[i],
153        ZED_WINDOW_POSITION: position,
154        ZED_STATELESS: isStateful && i == 0 ? "1" : "",
155        ZED_ALWAYS_ACTIVE: "1",
156        ZED_SERVER_URL: "http://localhost:3000",
157        ZED_RPC_URL: "http://localhost:8080/rpc",
158        ZED_ADMIN_API_TOKEN: "secret",
159        ZED_WINDOW_SIZE: size,
160        ZED_CLIENT_CHECKSUM_SEED: "development-checksum-seed",
161        RUST_LOG: process.env.RUST_LOG || "info",
162      }),
163    });
164  }
165}, 0.1);