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