zed/script/zed-local

152 lines
4.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
const HELP = `
USAGE
zed-local [options] [zed args]
SUMMARY
Runs 1-6 instances of Zed using a locally-running collaboration server.
Each instance of Zed will be signed in as a different user specified in
either \`.admins.json\` or \`.admins.default.json\`.
OPTIONS
--help Print this help message
--release Build Zed in release mode
-2, -3, -4, ... Spawn multiple Zed instances, with their windows tiled.
--top Arrange the Zed windows so they take up the top half of the screen.
--stable Use stable Zed release installed on local machine for all instances (except for the first one).
`.trim();
2023-12-16 00:27:58 +03:00
const { spawn, execFileSync } = require("child_process");
const assert = require("assert");
const users = require(
process.env.SEED_PATH || "../crates/collab/seed.default.json",
).admins;
2023-12-16 00:27:58 +03:00
const RESOLUTION_REGEX = /(\d+) x (\d+)/;
const DIGIT_FLAG_REGEX = /^--?(\d+)$/;
2023-12-16 00:27:58 +03:00
let instanceCount = 1;
let isReleaseMode = false;
let isTop = false;
let othersOnStable = false;
const args = process.argv.slice(2);
while (args.length > 0) {
const arg = args[0];
const digitMatch = arg.match(DIGIT_FLAG_REGEX);
if (digitMatch) {
instanceCount = parseInt(digitMatch[1]);
} else if (arg === "--release") {
isReleaseMode = true;
} else if (arg === "--top") {
isTop = true;
} else if (arg === "--help") {
console.log(HELP);
process.exit(0);
} else if (arg === "--stable") {
othersOnStable = true;
} else {
break;
}
args.shift();
}
// Parse the resolution of the main screen
const displayInfo = JSON.parse(
2023-12-16 00:27:58 +03:00
execFileSync("system_profiler", ["SPDisplaysDataType", "-json"], {
encoding: "utf8",
}),
);
const mainDisplayResolution =
displayInfo?.SPDisplaysDataType[0]?.spdisplays_ndrvs
?.find((entry) => entry.spdisplays_main === "spdisplays_yes")
?._spdisplays_resolution?.match(RESOLUTION_REGEX);
if (!mainDisplayResolution) {
2023-12-16 00:27:58 +03:00
throw new Error("Could not parse screen resolution");
}
const titleBarHeight = 24;
2023-12-16 00:27:58 +03:00
const screenWidth = parseInt(mainDisplayResolution[1]);
let screenHeight = parseInt(mainDisplayResolution[2]) - titleBarHeight;
if (isTop) {
screenHeight = Math.floor(screenHeight / 2);
}
// Determine the window size for each instance
let rows;
let columns;
switch (instanceCount) {
case 1:
[rows, columns] = [1, 1];
break;
case 2:
[rows, columns] = [1, 2];
break;
case 3:
case 4:
[rows, columns] = [2, 2];
break;
case 5:
case 6:
[rows, columns] = [2, 3];
break;
}
const instanceWidth = Math.floor(screenWidth / columns);
const instanceHeight = Math.floor(screenHeight / rows);
// If a user is specified, make sure it's first in the list
2023-12-16 00:27:58 +03:00
const user = process.env.ZED_IMPERSONATE;
if (user) {
2023-12-16 00:27:58 +03:00
users = [user].concat(users.filter((u) => u !== user));
}
let buildArgs = ["build"];
let zedBinary = "target/debug/Zed";
if (isReleaseMode) {
buildArgs.push("--release");
zedBinary = "target/release/Zed";
}
try {
execFileSync("cargo", buildArgs, { stdio: "inherit" });
} catch (e) {
process.exit(0);
}
setTimeout(() => {
for (let i = 0; i < instanceCount; i++) {
const row = Math.floor(i / columns);
const column = i % columns;
const position = [
column * instanceWidth,
row * instanceHeight + titleBarHeight,
].join(",");
const size = [instanceWidth, instanceHeight].join(",");
let binaryPath = zedBinary;
if (i != 0 && othersOnStable) {
binaryPath = "/Applications/Zed.app/Contents/MacOS/zed";
}
spawn(binaryPath, i == 0 ? args : [], {
2023-12-16 00:27:58 +03:00
stdio: "inherit",
env: {
ZED_IMPERSONATE: users[i],
ZED_WINDOW_POSITION: position,
2023-12-16 00:27:58 +03:00
ZED_STATELESS: "1",
ZED_ALWAYS_ACTIVE: "1",
ZED_SERVER_URL: "http://localhost:3000",
ZED_RPC_URL: "http://localhost:8080/rpc",
2023-12-16 00:27:58 +03:00
ZED_ADMIN_API_TOKEN: "secret",
ZED_WINDOW_SIZE: size,
ZED_CLIENT_CHECKSUM_SEED: "development-checksum-seed",
2023-10-22 17:03:13 +03:00
PATH: process.env.PATH,
RUST_LOG: process.env.RUST_LOG || "info",
2023-12-16 00:27:58 +03:00
},
});
}
2023-12-16 00:27:58 +03:00
}, 0.1);