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`.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 screenWidth = parseInt(mainDisplayResolution[1]);
 75let screenHeight = parseInt(mainDisplayResolution[2]);
 76
 77if (isTop) {
 78  screenHeight = Math.floor(screenHeight / 2);
 79}
 80
 81// Determine the window size for each instance
 82let rows;
 83let columns;
 84switch (instanceCount) {
 85  case 1:
 86    [rows, columns] = [1, 1];
 87    break;
 88  case 2:
 89    [rows, columns] = [1, 2];
 90    break;
 91  case 3:
 92  case 4:
 93    [rows, columns] = [2, 2];
 94    break;
 95  case 5:
 96  case 6:
 97    [rows, columns] = [2, 3];
 98    break;
 99}
100
101const instanceWidth = Math.floor(screenWidth / columns);
102const instanceHeight = Math.floor(screenHeight / rows);
103
104// If a user is specified, make sure it's first in the list
105const user = process.env.ZED_IMPERSONATE;
106if (user) {
107  users = [user].concat(users.filter((u) => u !== user));
108}
109
110let buildArgs = ["build"];
111let zedBinary = "target/debug/Zed";
112if (isReleaseMode) {
113  buildArgs.push("--release");
114  zedBinary = "target/release/Zed";
115}
116
117execFileSync("cargo", buildArgs, { stdio: "inherit" });
118setTimeout(() => {
119  for (let i = 0; i < instanceCount; i++) {
120    const row = Math.floor(i / columns);
121    const column = i % columns;
122    const position = [column * instanceWidth, row * instanceHeight].join(",");
123    const size = [instanceWidth, instanceHeight].join(",");
124
125    spawn(zedBinary, i == 0 ? args : [], {
126      stdio: "inherit",
127      env: {
128        ZED_IMPERSONATE: users[i],
129        ZED_WINDOW_POSITION: position,
130        ZED_STATELESS: "1",
131        ZED_ALWAYS_ACTIVE: "1",
132        ZED_SERVER_URL: "http://localhost:8080",
133        ZED_ADMIN_API_TOKEN: "secret",
134        ZED_WINDOW_SIZE: size,
135        PATH: process.env.PATH,
136        RUST_LOG: process.env.RUST_LOG || "info",
137      },
138    });
139  }
140}, 0.1);