zed-local

 1#!/usr/bin/env node
 2
 3const {spawn, execFileSync} = require('child_process')
 4
 5const RESOLUTION_REGEX = /(\d+) x (\d+)/
 6const DIGIT_FLAG_REGEX = /^--?(\d+)$/
 7const ZED_2_MODE = "--zed2"
 8
 9const args = process.argv.slice(2)
10
11// Parse the number of Zed instances to spawn.
12let instanceCount = 1
13const digitMatch = args[0]?.match(DIGIT_FLAG_REGEX)
14if (digitMatch) {
15  instanceCount = parseInt(digitMatch[1])
16  args.shift()
17}
18const isZed2 = args.some(arg => arg === ZED_2_MODE);
19if (instanceCount > 4) {
20  throw new Error('Cannot spawn more than 4 instances')
21}
22
23// Parse the resolution of the main screen
24const displayInfo = JSON.parse(
25  execFileSync(
26    'system_profiler',
27    ['SPDisplaysDataType', '-json'],
28    {encoding: 'utf8'}
29  )
30)
31const mainDisplayResolution = displayInfo
32  ?.SPDisplaysDataType[0]
33  ?.spdisplays_ndrvs
34  ?.find(entry => entry.spdisplays_main === "spdisplays_yes")
35  ?._spdisplays_resolution
36  ?.match(RESOLUTION_REGEX)
37if (!mainDisplayResolution) {
38  throw new Error('Could not parse screen resolution')
39}
40const screenWidth = parseInt(mainDisplayResolution[1])
41const screenHeight = parseInt(mainDisplayResolution[2])
42
43// Determine the window size for each instance
44let instanceWidth = screenWidth
45let instanceHeight = screenHeight
46if (instanceCount > 1) {
47  instanceWidth = Math.floor(screenWidth / 2)
48  if (instanceCount > 2) {
49    instanceHeight = Math.floor(screenHeight / 2)
50  }
51}
52
53let users = [
54  'nathansobo',
55  'as-cii',
56  'maxbrunsfeld',
57  'iamnbutler'
58]
59
60const RUST_LOG = process.env.RUST_LOG || 'info'
61
62// If a user is specified, make sure it's first in the list
63const user = process.env.ZED_IMPERSONATE
64if (user) {
65  users = [user].concat(users.filter(u => u !== user))
66}
67
68const positions = [
69  '0,0',
70  `${instanceWidth},0`,
71  `0,${instanceHeight}`,
72  `${instanceWidth},${instanceHeight}`
73]
74
75const buildArgs = isZed2 ? ["build", "-p", "zed2"] : ["build"]
76const zedBinary = isZed2 ? "target/debug/Zed2" : "target/debug/Zed"
77execFileSync('cargo', buildArgs, { stdio: 'inherit' })
78setTimeout(() => {
79  for (let i = 0; i < instanceCount; i++) {
80    spawn(zedBinary, i == 0 ? args : [], {
81      stdio: 'inherit',
82      env: {
83        ZED_IMPERSONATE: users[i],
84        ZED_WINDOW_POSITION: positions[i],
85        ZED_STATELESS: '1',
86        ZED_ALWAYS_ACTIVE: '1',
87        ZED_SERVER_URL: 'http://localhost:8080',
88        ZED_ADMIN_API_TOKEN: 'secret',
89        ZED_WINDOW_SIZE: `${instanceWidth},${instanceHeight}`,
90        PATH: process.env.PATH,
91        RUST_LOG,
92      }
93    })
94  }
95}, 0.1)