Skip to content

Commit e2de424

Browse files
committed
feat: add the desktop app that runs devhub ui in an Electron window
1 parent cd60f29 commit e2de424

20 files changed

Lines changed: 621 additions & 3 deletions

‎.github/workflows/ci-build.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ jobs:
7575

7676
- name: Install dependencies
7777
run: pnpm install --frozen-lockfile
78+
env:
79+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
7880

7981
- name: Typecheck
8082
run: pnpm run typecheck

‎.github/workflows/ci-format.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333

3434
- name: Install dependencies
3535
run: pnpm install --frozen-lockfile
36+
env:
37+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
3638

3739
# Prettier does not format Markdown here. `.prettierignore` gives the
3840
# reason.

‎.github/workflows/deploy-web.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ jobs:
5353

5454
- name: Install dependencies
5555
run: pnpm install --frozen-lockfile
56+
env:
57+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
5658

5759
# The site imports nothing from the package. It copies the committed
5860
# packages/devhub/llms.txt, and ci-build.yml checks that file against

‎.github/workflows/gate-cache-provider.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ jobs:
3939

4040
- name: Install dependencies
4141
run: pnpm install --frozen-lockfile
42+
env:
43+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
4244

4345
# The example loads the cache provider from packages/devhub/dist.
4446
- name: Build package
@@ -67,6 +69,8 @@ jobs:
6769

6870
- name: Install dependencies
6971
run: pnpm install --frozen-lockfile
72+
env:
73+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
7074

7175
- name: Build package
7276
run: pnpm run build

‎.github/workflows/release-manual.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ jobs:
100100

101101
- name: Install dependencies
102102
run: pnpm install --frozen-lockfile
103+
env:
104+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
103105

104106
- name: Check the release
105107
run: node scripts/check-release.mjs --version "${{ inputs.version }}" --dist-tag "${{ inputs.tag }}"

‎.github/workflows/release.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ jobs:
8181

8282
- name: Install dependencies
8383
run: pnpm install --frozen-lockfile
84+
env:
85+
ELECTRON_SKIP_BINARY_DOWNLOAD: "1"
8486

8587
- name: Check the release
8688
run: node scripts/check-release.mjs --version "${{ inputs.version }}" --dist-tag "${{ inputs.tag }}"

‎apps/desktop/package.json‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@rootnative/devhub-desktop",
3+
"version": "0.0.0",
4+
"private": true,
5+
"description": "The desktop app of DevHub by Root Native. It starts the devhub bin as a child process, and shows the devhub ui page in a window.",
6+
"type": "module",
7+
"main": "dist/main.js",
8+
"scripts": {
9+
"build": "tsup",
10+
"start": "node scripts/start.mjs",
11+
"typecheck": "tsc --noEmit",
12+
"test": "vitest run",
13+
"lint": "eslint . --max-warnings 0",
14+
"clean": "node -e \"for (const p of ['dist', '.turbo']) require('node:fs').rmSync(p, { recursive: true, force: true })\""
15+
},
16+
"dependencies": {
17+
"@rootnative/devhub": "workspace:*"
18+
},
19+
"devDependencies": {
20+
"@types/node": "^22.20.2",
21+
"electron": "44.9.1",
22+
"tsup": "^8.5.1",
23+
"typescript": "^5.9.3",
24+
"vitest": "^3.2.4"
25+
}
26+
}

‎apps/desktop/scripts/start.mjs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Starts Electron with the built main process. A terminal of VS Code sets
2+
// ELECTRON_RUN_AS_NODE=1, and with it Electron runs as plain Node and the
3+
// app cannot import "electron". The script removes it for the child.
4+
import { spawn } from 'node:child_process'
5+
import { existsSync } from 'node:fs'
6+
import { createRequire } from 'node:module'
7+
import path from 'node:path'
8+
import { fileURLToPath } from 'node:url'
9+
10+
const app = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
11+
const require = createRequire(import.meta.url)
12+
const electron = require('electron')
13+
14+
if (!existsSync(electron)) {
15+
console.error(
16+
`The Electron binary is missing at ${electron}. Run "node node_modules/electron/install.js" in the repository root, and then start again.`,
17+
)
18+
process.exit(1)
19+
}
20+
21+
const env = { ...process.env }
22+
delete env.ELECTRON_RUN_AS_NODE
23+
24+
const child = spawn(electron, [app, ...process.argv.slice(2)], {
25+
env,
26+
stdio: 'inherit',
27+
})
28+
child.on('exit', (code) => process.exit(code ?? 1))
29+
for (const signal of ['SIGINT', 'SIGTERM']) {
30+
process.on(signal, () => child.kill(signal))
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { readAnnouncement } from '../announcement'
3+
4+
const ANNOUNCEMENT = JSON.stringify(
5+
{ url: 'http://127.0.0.1:51234/#token=abc', port: 51234 },
6+
null,
7+
2,
8+
)
9+
10+
describe('readAnnouncement', () => {
11+
it('reads the JSON of devhub ui --json', () => {
12+
expect(readAnnouncement(`${ANNOUNCEMENT}\n`)).toEqual({
13+
url: 'http://127.0.0.1:51234/#token=abc',
14+
port: 51234,
15+
})
16+
})
17+
18+
it('waits while the object is not complete', () => {
19+
const half = ANNOUNCEMENT.slice(0, ANNOUNCEMENT.indexOf('"port"'))
20+
expect(readAnnouncement(half)).toBeUndefined()
21+
expect(readAnnouncement('')).toBeUndefined()
22+
expect(readAnnouncement('DevHub by Root Native runs at')).toBeUndefined()
23+
})
24+
25+
it('reads the object when text comes before and after it', () => {
26+
expect(readAnnouncement(`noise\n${ANNOUNCEMENT}\nmore\n`)).toEqual({
27+
url: 'http://127.0.0.1:51234/#token=abc',
28+
port: 51234,
29+
})
30+
})
31+
32+
it('refuses a URL that is not the loopback server on the port', () => {
33+
const wrong = (url: string, port = 51234) =>
34+
readAnnouncement(JSON.stringify({ url, port }))
35+
expect(wrong('http://localhost:51234/#token=abc')).toBeUndefined()
36+
expect(wrong('https://127.0.0.1:51234/')).toBeUndefined()
37+
expect(wrong('http://127.0.0.1:51235/#token=abc')).toBeUndefined()
38+
expect(wrong('http://127.0.0.1:51234/', 0)).toBeUndefined()
39+
expect(wrong('not a url')).toBeUndefined()
40+
})
41+
42+
it('refuses an object with another shape', () => {
43+
expect(readAnnouncement('{"error":"nope"}')).toBeUndefined()
44+
expect(readAnnouncement('{"url":1,"port":"5"}')).toBeUndefined()
45+
expect(readAnnouncement('[]')).toBeUndefined()
46+
})
47+
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { decideNavigation } from '../navigation'
3+
4+
const PAGE = 'http://127.0.0.1:51234/#token=abc'
5+
6+
describe('decideNavigation', () => {
7+
it('allows the origin of the page', () => {
8+
expect(decideNavigation(PAGE, 'http://127.0.0.1:51234/')).toBe('allow')
9+
expect(decideNavigation(PAGE, 'http://127.0.0.1:51234/#token=xyz')).toBe(
10+
'allow',
11+
)
12+
expect(decideNavigation(PAGE, 'http://127.0.0.1:51234/api/builds')).toBe(
13+
'allow',
14+
)
15+
})
16+
17+
it('opens a web link to another origin in the browser', () => {
18+
expect(decideNavigation(PAGE, 'https://expo.dev/')).toBe('external')
19+
expect(decideNavigation(PAGE, 'http://127.0.0.1:51235/')).toBe('external')
20+
expect(decideNavigation(PAGE, 'http://localhost:51234/')).toBe('external')
21+
})
22+
23+
it('blocks every other URL', () => {
24+
expect(decideNavigation(PAGE, 'file:///etc/passwd')).toBe('block')
25+
expect(decideNavigation(PAGE, 'javascript:alert(1)')).toBe('block')
26+
expect(decideNavigation(PAGE, 'devhub://sync')).toBe('block')
27+
expect(decideNavigation(PAGE, 'not a url')).toBe('block')
28+
expect(decideNavigation('not a url', 'https://expo.dev/')).toBe('block')
29+
})
30+
})

0 commit comments

Comments
 (0)