zed/script/zed-local
Max Brunsfeld 9367f719f2 Rework db-seeding, so that it doesn't depend on a github auth token
Instead, admins are specified using a JSON file, 'admins.json'. This file is
gitignored. If it is not present, there is a default list of admins in
'admins.default.json'.
2024-01-17 13:28:58 -08:00

127 lines
3.4 KiB
JavaScript
Executable file

#!/usr/bin/env node
const HELP = `
USAGE
zed-local [options] [zed args]
SUMMARY
Runs 1-4 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 2, 3, or 4 Zed instances, with their windows tiled.
--top Arrange the Zed windows so they take up the top half of the screen.
`.trim();
const { spawn, execFileSync } = require("child_process");
const assert = require("assert");
const defaultUsers = require("../crates/collab/.admins.default.json");
let users = defaultUsers;
try {
const customUsers = require("../crates/collab/.admins.json");
assert(customUsers.length > 0);
assert(customUsers.every((user) => typeof user === "string"));
users.splice(0, 0, ...customUsers);
} catch (_) {}
const RESOLUTION_REGEX = /(\d+) x (\d+)/;
const DIGIT_FLAG_REGEX = /^--?(\d+)$/;
let instanceCount = 1;
let isReleaseMode = false;
let isTop = 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 {
break;
}
args.shift();
}
// Parse the resolution of the main screen
const displayInfo = JSON.parse(
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) {
throw new Error("Could not parse screen resolution");
}
const screenWidth = parseInt(mainDisplayResolution[1]);
let screenHeight = parseInt(mainDisplayResolution[2]);
if (isTop) {
screenHeight = Math.floor(screenHeight / 2);
}
// Determine the window size for each instance
let instanceWidth = screenWidth;
let instanceHeight = screenHeight;
if (instanceCount > 1) {
instanceWidth = Math.floor(screenWidth / 2);
if (instanceCount > 2) {
instanceHeight = Math.floor(screenHeight / 2);
}
}
// If a user is specified, make sure it's first in the list
const user = process.env.ZED_IMPERSONATE;
if (user) {
users = [user].concat(users.filter((u) => u !== user));
}
const positions = [
"0,0",
`${instanceWidth},0`,
`0,${instanceHeight}`,
`${instanceWidth},${instanceHeight}`,
];
let buildArgs = ["build"];
let zedBinary = "target/debug/Zed";
if (isReleaseMode) {
buildArgs.push("--release");
zedBinary = "target/release/Zed";
}
execFileSync("cargo", buildArgs, { stdio: "inherit" });
setTimeout(() => {
for (let i = 0; i < instanceCount; i++) {
spawn(zedBinary, i == 0 ? args : [], {
stdio: "inherit",
env: {
ZED_IMPERSONATE: users[i],
ZED_WINDOW_POSITION: positions[i],
ZED_STATELESS: "1",
ZED_ALWAYS_ACTIVE: "1",
ZED_SERVER_URL: "http://localhost:8080",
ZED_ADMIN_API_TOKEN: "secret",
ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}`,
PATH: process.env.PATH,
RUST_LOG: process.env.RUST_LOG || "info",
},
});
}
}, 0.1);