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