@octane-xplat/devtools 0.0.0-nightly.g1b95ef7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/app.d.ts +28 -0
- package/dist/app.js +66 -0
- package/dist/backend.d.ts +31 -0
- package/dist/backend.js +182 -0
- package/dist/client.d.ts +49 -0
- package/dist/client.js +168 -0
- package/dist/host/desktop.d.ts +15 -0
- package/dist/host/desktop.js +133 -0
- package/dist/host/panels.d.ts +14 -0
- package/dist/host/panels.js +156 -0
- package/dist/host/records.d.ts +5 -0
- package/dist/host/records.js +46 -0
- package/dist/host/server.d.ts +8 -0
- package/dist/host/server.js +67 -0
- package/dist/host/web.d.ts +5 -0
- package/dist/host/web.js +74 -0
- package/dist/macos/index.macos.d.ts +3 -0
- package/dist/macos/index.macos.js +5 -0
- package/dist/macos/servers.macos.d.ts +4 -0
- package/dist/macos/servers.macos.js +57 -0
- package/dist/macos/socket.macos.d.ts +2 -0
- package/dist/macos/socket.macos.js +152 -0
- package/dist/macos/ws-frames.d.ts +24 -0
- package/dist/macos/ws-frames.js +205 -0
- package/dist/node.d.ts +12 -0
- package/dist/node.js +51 -0
- package/dist/record.d.ts +9 -0
- package/dist/record.js +38 -0
- package/dist/types.d.ts +128 -0
- package/dist/types.js +25 -0
- package/package.json +88 -0
- package/src/app.ts +105 -0
- package/src/assets.d.ts +2 -0
- package/src/backend.ts +278 -0
- package/src/client.ts +248 -0
- package/src/desktop/Shell.macos.tsrx +158 -0
- package/src/desktop/Shell.ts +16 -0
- package/src/desktop/index.macos.ts +7 -0
- package/src/desktop/main.macos.ts +130 -0
- package/src/desktop/panels.d.ts +10 -0
- package/src/desktop/target.ts +34 -0
- package/src/host/desktop.ts +176 -0
- package/src/host/panels.ts +193 -0
- package/src/host/records.ts +62 -0
- package/src/host/server.ts +92 -0
- package/src/host/web.ts +68 -0
- package/src/macos/index.macos.ts +7 -0
- package/src/macos/servers.macos.ts +83 -0
- package/src/macos/socket.macos.ts +218 -0
- package/src/macos/ws-frames.ts +249 -0
- package/src/node.ts +60 -0
- package/src/panel/App.tsrx +288 -0
- package/src/panel/index.html +12 -0
- package/src/panel/index.ts +24 -0
- package/src/panel/main.tsrx +2 -0
- package/src/panel/main.web.tsrx +89 -0
- package/src/panel/model.ts +216 -0
- package/src/panel/panel.css +189 -0
- package/src/panel/plugin.ts +65 -0
- package/src/panel/styles.ts +5 -0
- package/src/panel/tsconfig.browser.json +9 -0
- package/src/record.ts +66 -0
- package/src/types.ts +129 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Octane Xplat devtools desktop: the shared devtools panel from
|
|
2
|
+
// @octane-xplat/devtools/panel over the Network.framework socket from
|
|
3
|
+
// @octane-xplat/devtools/macos, in a single AppKit window. The Shell asks
|
|
4
|
+
// which live `xplat dev` server record to target when several exist, and
|
|
5
|
+
// auto-targets the app selected at launch when that record resolves.
|
|
6
|
+
// runMacOSApp owns NSApplication startup and the event loop.
|
|
7
|
+
import '@nativescript/macos-node-api'
|
|
8
|
+
import { runMacOSApp } from '@octane-xplat/macos-appkit/host'
|
|
9
|
+
import type { NSView } from '@octane-xplat/platform/macos'
|
|
10
|
+
import { writeFileSync } from 'node:fs'
|
|
11
|
+
import { Shell } from './Shell.macos.tsrx'
|
|
12
|
+
|
|
13
|
+
// The project-built variant sets a Vite define for direct development launches.
|
|
14
|
+
declare const __XPLAT_DEVTOOLS_PREFERRED_ROOT__: string
|
|
15
|
+
|
|
16
|
+
const PREFERRED_ROOT =
|
|
17
|
+
typeof __XPLAT_DEVTOOLS_PREFERRED_ROOT__ === 'string'
|
|
18
|
+
? __XPLAT_DEVTOOLS_PREFERRED_ROOT__
|
|
19
|
+
: (process.env.XPLAT_DEVTOOLS_PREFERRED_ROOT ?? null)
|
|
20
|
+
|
|
21
|
+
const SELFTEST = process.env.DEVTOOLS_SELFTEST === '1'
|
|
22
|
+
const VIEWDUMP = process.env.DEVTOOLS_VIEWDUMP ?? '/tmp/devtools-desktop-viewtree.json'
|
|
23
|
+
const SELFTEST_PICK = Number.parseInt(process.env.DEVTOOLS_SELFTEST_PICK ?? '', 10)
|
|
24
|
+
const SELFTEST_TAB = process.env.DEVTOOLS_SELFTEST_TAB
|
|
25
|
+
|
|
26
|
+
const selftest: { pick?: (index: number) => void } = {}
|
|
27
|
+
|
|
28
|
+
runMacOSApp(
|
|
29
|
+
{
|
|
30
|
+
title: 'devtools',
|
|
31
|
+
size: { width: 420, height: 720 },
|
|
32
|
+
terminateAfterLastWindowClosed: true,
|
|
33
|
+
},
|
|
34
|
+
({ window, root }) => {
|
|
35
|
+
window.contentMinSize = { width: 380, height: 360 }
|
|
36
|
+
root.render(Shell as never, {
|
|
37
|
+
preferredRoot: PREFERRED_ROOT,
|
|
38
|
+
initialTab: SELFTEST_TAB,
|
|
39
|
+
onKeepOnTop: (onTop: boolean) => {
|
|
40
|
+
// NSFloatingWindowLevel (3) above NSNormalWindowLevel (0).
|
|
41
|
+
const level = onTop ? 3 : 0
|
|
42
|
+
const host = window as unknown as {
|
|
43
|
+
setLevel?: (level: number) => void
|
|
44
|
+
level?: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (typeof host.setLevel === 'function') {
|
|
48
|
+
host.setLevel(level)
|
|
49
|
+
} else {
|
|
50
|
+
host.level = level
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
onTitle: (title: string) => {
|
|
54
|
+
window.title = title
|
|
55
|
+
},
|
|
56
|
+
selftest,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
if (!SELFTEST) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function viewTree(v: NSView): Record<string, unknown> {
|
|
64
|
+
const f = v.frame ?? { origin: { x: 0, y: 0 }, size: { width: 0, height: 0 } }
|
|
65
|
+
const node: Record<string, unknown> = {
|
|
66
|
+
class: String(v.className ?? 'NSView'),
|
|
67
|
+
x: f.origin?.x ?? 0,
|
|
68
|
+
y: f.origin?.y ?? 0,
|
|
69
|
+
w: f.size?.width ?? 0,
|
|
70
|
+
h: f.size?.height ?? 0,
|
|
71
|
+
hidden: Boolean(v.isHidden?.() ?? v.hidden ?? false),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const prop of ['stringValue', 'title', 'label', 'string', 'placeholderString']) {
|
|
75
|
+
try {
|
|
76
|
+
const raw = (v as never as Record<string, unknown>)[prop]
|
|
77
|
+
const t = typeof raw === 'function' ? raw.call(v) : raw
|
|
78
|
+
|
|
79
|
+
if (typeof t === 'string' && t.length) {
|
|
80
|
+
node.text = t
|
|
81
|
+
} else if (t && typeof t === 'object') {
|
|
82
|
+
node.text = String(t)
|
|
83
|
+
}
|
|
84
|
+
} catch {}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const subs = v.subviews
|
|
88
|
+
|
|
89
|
+
if (subs && typeof subs.count === 'number' && subs.count > 0) {
|
|
90
|
+
node.children = []
|
|
91
|
+
for (let i = 0; i < subs.count; i++) {
|
|
92
|
+
try {
|
|
93
|
+
;(node.children as unknown[]).push(viewTree(subs.objectAtIndex(i) as NSView))
|
|
94
|
+
} catch {}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return node
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Dev-only self-test: dump the main window's NSView tree
|
|
102
|
+
// (class/frame/hidden/text) as JSON so picker/panel content can be
|
|
103
|
+
// asserted without screenshots. Gated by DEVTOOLS_SELFTEST=1.
|
|
104
|
+
// DEVTOOLS_SELFTEST_PICK=<index> additionally picks that picker row after
|
|
105
|
+
// the first dump and writes a second dump to `${VIEWDUMP}.picked.json` —
|
|
106
|
+
// this proves the state transition, not OS hit-testing.
|
|
107
|
+
function dumpViews(path: string) {
|
|
108
|
+
try {
|
|
109
|
+
const json = JSON.stringify({
|
|
110
|
+
windows: [{ title: String(window.title ?? ''), tree: viewTree(window.contentView) }],
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
writeFileSync(path, json)
|
|
114
|
+
console.log(`[selftest] viewtree written ${path} bytes=${json.length}`)
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error('[selftest] viewtree dump failed', error)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Give the picker a moment to list records / the panel socket to connect,
|
|
121
|
+
// then dump; a pick index renders its transition in the second dump.
|
|
122
|
+
setTimeout(() => {
|
|
123
|
+
dumpViews(VIEWDUMP)
|
|
124
|
+
if (Number.isInteger(SELFTEST_PICK)) {
|
|
125
|
+
selftest.pick?.(SELFTEST_PICK)
|
|
126
|
+
setTimeout(() => dumpViews(`${VIEWDUMP}.picked.json`), 4000)
|
|
127
|
+
}
|
|
128
|
+
}, 4000)
|
|
129
|
+
},
|
|
130
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Ambient types for the virtual module served by `devtoolsPanels`
|
|
2
|
+
// (src/host/panels.ts) — wired into the generated `xplat devtools` config and
|
|
3
|
+
// the apps/devtools harness config.
|
|
4
|
+
declare module 'virtual:octane-devtools-panels' {
|
|
5
|
+
import type { DevtoolsPanelPlugin } from '../panel/index.ts'
|
|
6
|
+
|
|
7
|
+
const plugins: readonly DevtoolsPanelPlugin[]
|
|
8
|
+
|
|
9
|
+
export default plugins
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Pure dev-server targeting rule for the desktop shell — deliberately
|
|
2
|
+
// non-suffixed so Node tests import it and no `.macos.ts` sibling shadows it.
|
|
3
|
+
import type { ListedDevtoolsServer } from '../record.ts'
|
|
4
|
+
|
|
5
|
+
function normalizeRoot(path: string): string {
|
|
6
|
+
let end = path.length
|
|
7
|
+
|
|
8
|
+
while (end > 1 && path[end - 1] === '/') {
|
|
9
|
+
end--
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return path.slice(0, end)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Order: an explicit preferred root (the app `xplat devtools` ran from) wins;
|
|
16
|
+
// a sole record auto-targets; otherwise the picker decides. Both sides are
|
|
17
|
+
// canonical real paths — Go writes appRoot through osfs.Canonical and the
|
|
18
|
+
// launcher realpaths the selected project — so trailing-slash
|
|
19
|
+
// normalization is defensive only.
|
|
20
|
+
export function autoTarget(
|
|
21
|
+
records: ListedDevtoolsServer[],
|
|
22
|
+
preferredRoot: string | null,
|
|
23
|
+
): ListedDevtoolsServer | null {
|
|
24
|
+
if (preferredRoot) {
|
|
25
|
+
const root = normalizeRoot(preferredRoot)
|
|
26
|
+
const match = records.find((record) => normalizeRoot(record.appRoot) === root)
|
|
27
|
+
|
|
28
|
+
if (match) {
|
|
29
|
+
return match
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return records.length === 1 ? records[0] : null
|
|
34
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// `xplat devtools` launcher: resolves the selected project's macOS dev engine,
|
|
2
|
+
// generates a disposable Vite config whose entry is this package's desktop app
|
|
3
|
+
// (src/desktop/main.macos.ts), and runs it through runMacOSDev. The service
|
|
4
|
+
// worker (packages/cli devtools-desktop op) calls this from the app's own
|
|
5
|
+
// Node process; Go owns argv/cwd/env and the process group.
|
|
6
|
+
|
|
7
|
+
import { createRequire } from 'node:module'
|
|
8
|
+
import { lstatSync, realpathSync, statSync } from 'node:fs'
|
|
9
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { dirname, join, relative, sep } from 'node:path'
|
|
12
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
13
|
+
|
|
14
|
+
export const DEVTOOLS_DESKTOP_LIFECYCLE_VERSION = 1
|
|
15
|
+
|
|
16
|
+
export { discoverDevtoolsPanels, devtoolsPanels } from './panels.ts'
|
|
17
|
+
export type { DiscoveredDevtoolsPanel } from './panels.ts'
|
|
18
|
+
|
|
19
|
+
interface MacOSDevEngine {
|
|
20
|
+
runMacOSDev(
|
|
21
|
+
appRoot: string,
|
|
22
|
+
options: {
|
|
23
|
+
target?: Record<string, unknown>
|
|
24
|
+
workshop?: boolean
|
|
25
|
+
resourceRoot?: string
|
|
26
|
+
onReady?: (metadata: {
|
|
27
|
+
pid: number
|
|
28
|
+
renderer: string
|
|
29
|
+
executable: string
|
|
30
|
+
observedAt?: string
|
|
31
|
+
url?: string
|
|
32
|
+
}) => void
|
|
33
|
+
},
|
|
34
|
+
): Promise<void>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Resolve the selected project's explicit engine, never a global fallback.
|
|
38
|
+
async function loadMacOSDevEngine(project: string): Promise<MacOSDevEngine> {
|
|
39
|
+
const require = createRequire(join(project, 'package.json'))
|
|
40
|
+
|
|
41
|
+
for (let at = project; ; at = dirname(at)) {
|
|
42
|
+
const candidate = join(at, 'node_modules/@octane-xplat/macos-build')
|
|
43
|
+
let found = false
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
lstatSync(candidate)
|
|
47
|
+
found = true
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (!(error instanceof Error) || !('code' in error) || error.code !== 'ENOENT') {
|
|
50
|
+
throw error
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (found) {
|
|
55
|
+
const root = realpathSync(candidate)
|
|
56
|
+
let entry: string
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
entry = realpathSync(require.resolve('@octane-xplat/macos-build/dev-engine'))
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Update or reinstall @octane-xplat/macos-build in the selected project: development engine export unavailable; ${String(error)}`,
|
|
63
|
+
{ cause: error },
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!entry.startsWith(root + sep) || !statSync(entry).isFile()) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
'Reinstall @octane-xplat/macos-build: its development engine must stay inside the selected package',
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const api = (await import(pathToFileURL(entry).href)) as MacOSDevEngine
|
|
74
|
+
|
|
75
|
+
if (typeof api.runMacOSDev !== 'function') {
|
|
76
|
+
throw new Error(
|
|
77
|
+
'Update @octane-xplat/macos-build in the selected project: development engine API is missing',
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return api
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (dirname(at) === at) {
|
|
85
|
+
break
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
throw new Error(
|
|
90
|
+
'Install the macOS project engine before launching devtools: bun add -D @octane-xplat/macos-build',
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface DevtoolsDesktopOptions {
|
|
95
|
+
cwd?: string
|
|
96
|
+
resourceRoot?: string
|
|
97
|
+
profileRoot?: string
|
|
98
|
+
onStarted?: (evidence: { kind: string; pid?: number; url?: string }) => void
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Generate a separate consumer-owned cache profile; leave app entries untouched. */
|
|
102
|
+
export async function devtoolsDesktop({
|
|
103
|
+
cwd,
|
|
104
|
+
resourceRoot,
|
|
105
|
+
profileRoot,
|
|
106
|
+
onStarted,
|
|
107
|
+
}: DevtoolsDesktopOptions = {}): Promise<void> {
|
|
108
|
+
if (process.platform !== 'darwin' || (process.arch !== 'arm64' && process.arch !== 'x64')) {
|
|
109
|
+
throw new Error('The native devtools app requires a macOS arm64 or x86_64 execution host')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const root = realpathSync(cwd ?? process.cwd())
|
|
113
|
+
const { runMacOSDev } = await loadMacOSDevEngine(root)
|
|
114
|
+
const require = createRequire(join(root, 'package.json'))
|
|
115
|
+
|
|
116
|
+
for (const name of [
|
|
117
|
+
'@octane-xplat/macos-appkit',
|
|
118
|
+
'@nativescript/macos-node-api',
|
|
119
|
+
'octane',
|
|
120
|
+
'@octanejs/vite-plugin',
|
|
121
|
+
'vite',
|
|
122
|
+
]) {
|
|
123
|
+
try {
|
|
124
|
+
require.resolve(name)
|
|
125
|
+
} catch {
|
|
126
|
+
throw new Error(`Devtools desktop prerequisite missing: bun add -D ${name}`)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Generated config/bundle live only in an owned per-run directory: the
|
|
131
|
+
// service worker's unique compiler profile, or OS temp for direct calls.
|
|
132
|
+
// Nothing may land in project output dirs — `xplat dev` holds that lock.
|
|
133
|
+
const directory = profileRoot
|
|
134
|
+
? await mkdtemp(join(profileRoot, 'session-'))
|
|
135
|
+
: await mkdtemp(join(tmpdir(), 'octane-devtools-desktop-'))
|
|
136
|
+
|
|
137
|
+
// This module runs from src/ in the monorepo and dist/host/ in an install —
|
|
138
|
+
// the desktop entry is shipped as source, so resolve through src/ either way.
|
|
139
|
+
const source = dirname(fileURLToPath(import.meta.url)).replace('/dist/', '/src/')
|
|
140
|
+
const entry = join(source, '../desktop/main.macos.ts')
|
|
141
|
+
const bundle = join(directory, 'devtools.cjs')
|
|
142
|
+
|
|
143
|
+
await writeFile(
|
|
144
|
+
join(directory, 'devtools.config.ts'),
|
|
145
|
+
`import { macos } from '@octane-xplat/cli/macos/vite'
|
|
146
|
+
import { devtoolsPanels } from '@octane-xplat/devtools/desktop-node'
|
|
147
|
+
const root = ${JSON.stringify(root)}
|
|
148
|
+
const entry = ${JSON.stringify(entry)}
|
|
149
|
+
|
|
150
|
+
export default async ({ mode }) => {
|
|
151
|
+
const settings = await macos(mode, { root, hmr: false, entry })
|
|
152
|
+
return { ...settings, plugins: [...(settings.plugins ?? []), devtoolsPanels({ root })], build: { ...settings.build, outDir: ${JSON.stringify(directory)}, emptyOutDir: false,
|
|
153
|
+
lib: { entry, formats: ['cjs'], fileName: () => 'devtools.cjs' } },
|
|
154
|
+
define: { ...(settings.define ?? {}), __XPLAT_DEVTOOLS_PREFERRED_ROOT__: ${JSON.stringify(JSON.stringify(root))} } }
|
|
155
|
+
}
|
|
156
|
+
`,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
await runMacOSDev(root, {
|
|
161
|
+
resourceRoot,
|
|
162
|
+
onReady: (meta) => onStarted?.({ kind: 'native-host-spawned', pid: meta.pid }),
|
|
163
|
+
target: {
|
|
164
|
+
runtime: 'appkit-node-api',
|
|
165
|
+
renderer: 'appkit',
|
|
166
|
+
dev: {
|
|
167
|
+
productName: 'Octane Xplat devtools',
|
|
168
|
+
viteConfig: relative(root, join(directory, 'devtools.config.ts')),
|
|
169
|
+
bundleFile: relative(root, bundle),
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
} finally {
|
|
174
|
+
await rm(directory, { recursive: true, force: true })
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Panel-plugin discovery for the project DevTools bundles: a package opts in
|
|
2
|
+
// with `"octaneXplat": { "devtoolsPanel": "./devtools" }` in its package.json
|
|
3
|
+
// (a subpath or an array of subpaths; each module must
|
|
4
|
+
// default-export a DevtoolsPanelPlugin). `devtoolsPanels` serves the
|
|
5
|
+
// discovered modules as `virtual:octane-devtools-panels` to the web and desktop
|
|
6
|
+
// shells.
|
|
7
|
+
//
|
|
8
|
+
// Scan scope is one level: the selected project's dependencies +
|
|
9
|
+
// devDependencies, plus octane-xplat's own dependencies when the umbrella is
|
|
10
|
+
// installed (resolved from the umbrella's location, so Bun's isolated install
|
|
11
|
+
// layout keeps transitive panel packages reachable). Candidates are resolved
|
|
12
|
+
// through createRequire from the declaring package and must realpath inside
|
|
13
|
+
// the plugin package's root — nothing outside a package's own files may be
|
|
14
|
+
// bundled. Malformed or unresolvable candidates are skipped with one stderr
|
|
15
|
+
// warning each; a package without the field is simply not a candidate.
|
|
16
|
+
|
|
17
|
+
import { createRequire } from 'node:module'
|
|
18
|
+
import { lstatSync, readFileSync, realpathSync, statSync } from 'node:fs'
|
|
19
|
+
import { dirname, join, sep } from 'node:path'
|
|
20
|
+
import type { Plugin } from 'vite'
|
|
21
|
+
|
|
22
|
+
export interface DiscoveredDevtoolsPanel {
|
|
23
|
+
/** The declaring package name. */
|
|
24
|
+
name: string
|
|
25
|
+
/** Realpath'd entry file the virtual module default-imports. */
|
|
26
|
+
file: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const FIELD = 'devtoolsPanel'
|
|
30
|
+
|
|
31
|
+
function readManifest(file: string): Record<string, unknown> | null {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(readFileSync(file, 'utf8')) as Record<string, unknown>
|
|
34
|
+
} catch {
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Locate a dependency's package.json: its exports map first, then a physical
|
|
40
|
+
* node_modules walk for packages that do not export ./package.json. */
|
|
41
|
+
function locatePackage(name: string, requireFrom: NodeRequire, fromDir: string): string | null {
|
|
42
|
+
try {
|
|
43
|
+
return requireFrom.resolve(`${name}/package.json`)
|
|
44
|
+
} catch {
|
|
45
|
+
// Fall through to the physical walk — a package without ./package.json in
|
|
46
|
+
// its exports map still carries the file on disk.
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (let at = fromDir; ; at = dirname(at)) {
|
|
50
|
+
const candidate = join(at, 'node_modules', name, 'package.json')
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
if (lstatSync(candidate).isFile()) {
|
|
54
|
+
return candidate
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
// keep walking
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (dirname(at) === at) {
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function warn(message: string) {
|
|
67
|
+
console.error(`[devtools] ${message}`)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function discoverDevtoolsPanels(projectRoot: string): DiscoveredDevtoolsPanel[] {
|
|
71
|
+
const projectManifest = readManifest(join(projectRoot, 'package.json')) ?? {}
|
|
72
|
+
const names = new Set<string>([
|
|
73
|
+
...Object.keys((projectManifest.dependencies as Record<string, string>) ?? {}),
|
|
74
|
+
...Object.keys((projectManifest.devDependencies as Record<string, string>) ?? {}),
|
|
75
|
+
])
|
|
76
|
+
|
|
77
|
+
// Scan sources in stable order: project deps (alphabetical), then the
|
|
78
|
+
// umbrella's dependencies (alphabetical, deduped against project names).
|
|
79
|
+
const sources: { dir: string; names: string[] }[] = [
|
|
80
|
+
{ dir: projectRoot, names: [...names].sort() },
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
if (names.has('octane-xplat')) {
|
|
84
|
+
try {
|
|
85
|
+
const requireFrom = createRequire(join(projectRoot, 'package.json'))
|
|
86
|
+
const umbrellaDir = dirname(requireFrom.resolve('octane-xplat/package.json'))
|
|
87
|
+
const umbrella = readManifest(join(umbrellaDir, 'package.json')) ?? {}
|
|
88
|
+
const extra = Object.keys((umbrella.dependencies as Record<string, string>) ?? {})
|
|
89
|
+
.filter((name) => !names.has(name))
|
|
90
|
+
.sort()
|
|
91
|
+
|
|
92
|
+
if (extra.length) {
|
|
93
|
+
sources.push({ dir: umbrellaDir, names: extra })
|
|
94
|
+
}
|
|
95
|
+
} catch (error) {
|
|
96
|
+
warn(`skipping octane-xplat dependency scan — ${String(error)}`)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const out: DiscoveredDevtoolsPanel[] = []
|
|
101
|
+
|
|
102
|
+
for (const source of sources) {
|
|
103
|
+
const requireFrom = createRequire(join(source.dir, 'package.json'))
|
|
104
|
+
|
|
105
|
+
for (const name of source.names) {
|
|
106
|
+
const manifestPath = locatePackage(name, requireFrom, source.dir)
|
|
107
|
+
|
|
108
|
+
if (!manifestPath) {
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let packageRoot: string
|
|
113
|
+
let manifest: Record<string, unknown> | null
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
packageRoot = realpathSync(dirname(manifestPath))
|
|
117
|
+
manifest = readManifest(join(packageRoot, 'package.json'))
|
|
118
|
+
} catch {
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const field = (manifest?.octaneXplat as Record<string, unknown> | undefined)?.[FIELD]
|
|
123
|
+
|
|
124
|
+
if (field === undefined) {
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const fail = (why: string) => warn(`skipping devtools panel candidate "${name}" — ${why}`)
|
|
129
|
+
|
|
130
|
+
const subpaths: unknown[] = Array.isArray(field) ? field : [field]
|
|
131
|
+
|
|
132
|
+
for (const subpath of subpaths) {
|
|
133
|
+
if (typeof subpath !== 'string' || !subpath.startsWith('./') || subpath.includes('..')) {
|
|
134
|
+
fail(
|
|
135
|
+
`octaneXplat.${FIELD} must be an export subpath like "./devtools" or an array of subpaths`,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let file: string
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
file = realpathSync(requireFrom.resolve(`${name}/${subpath.slice(2)}`))
|
|
145
|
+
} catch (error) {
|
|
146
|
+
fail(`cannot resolve ${name}/${subpath.slice(2)} — ${String(error)}`)
|
|
147
|
+
continue
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
if (!file.startsWith(packageRoot + sep) || !statSync(file).isFile()) {
|
|
152
|
+
fail(`resolved panel module escapes its package root — ${file}`)
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
fail(`resolved panel module is not a file — ${file}`)
|
|
157
|
+
continue
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
out.push({ name, file })
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return out
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export const DEVTOOLS_PANELS_VIRTUAL_ID = 'virtual:octane-devtools-panels'
|
|
169
|
+
|
|
170
|
+
const RESOLVED_ID = '\0' + DEVTOOLS_PANELS_VIRTUAL_ID
|
|
171
|
+
|
|
172
|
+
/** Vite plugin serving `virtual:octane-devtools-panels`: a default-exported
|
|
173
|
+
* array of the discovered panel plugins (`[]` when none). */
|
|
174
|
+
export function devtoolsPanels(options: { root: string }): Plugin {
|
|
175
|
+
return {
|
|
176
|
+
name: 'octane-devtools-panels',
|
|
177
|
+
resolveId(id) {
|
|
178
|
+
return id === DEVTOOLS_PANELS_VIRTUAL_ID ? RESOLVED_ID : null
|
|
179
|
+
},
|
|
180
|
+
load(id) {
|
|
181
|
+
if (id !== RESOLVED_ID) {
|
|
182
|
+
return null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const panels = discoverDevtoolsPanels(options.root)
|
|
186
|
+
const imports = panels
|
|
187
|
+
.map((panel, i) => `import p${i} from ${JSON.stringify(panel.file)}`)
|
|
188
|
+
.join('\n')
|
|
189
|
+
|
|
190
|
+
return `${imports}\nexport default [${panels.map((_, i) => `p${i}`).join(', ')}]`
|
|
191
|
+
},
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Enumerates live `xplat dev` server discovery records for the panel picker.
|
|
2
|
+
// Mirrors dev_server_record.go: records live under the native history cache in
|
|
3
|
+
// dev-servers-v1/<app-key>/server.json, one per app, each carrying the panel's
|
|
4
|
+
// devtoolsToken. The RPC token is never returned to the panel.
|
|
5
|
+
|
|
6
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
|
7
|
+
import { homedir, platform } from 'node:os'
|
|
8
|
+
import { isAbsolute, join } from 'node:path'
|
|
9
|
+
import { parseDevtoolsServerRecord, type ListedDevtoolsServer } from '../record.ts'
|
|
10
|
+
|
|
11
|
+
export type { ListedDevtoolsServer } from '../record.ts'
|
|
12
|
+
|
|
13
|
+
function cacheRoot(): string {
|
|
14
|
+
if (platform() === 'darwin') {
|
|
15
|
+
return join(homedir(), 'Library', 'Caches')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (process.platform === 'win32') {
|
|
19
|
+
return process.env.LOCALAPPDATA ?? join(homedir(), 'AppData', 'Local')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return process.env.XDG_CACHE_HOME ?? join(homedir(), '.cache')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function devServerRecordsDir(): string {
|
|
26
|
+
return (
|
|
27
|
+
process.env.XPLAT_DEV_RECORDS_DIR ??
|
|
28
|
+
join(cacheRoot(), 'octane-xplat', 'cli-go', 'dev-servers-v1')
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Returns every structurally valid live record, newest first. */
|
|
33
|
+
export function listDevtoolsServers(): ListedDevtoolsServer[] {
|
|
34
|
+
const root = devServerRecordsDir()
|
|
35
|
+
const found: { server: ListedDevtoolsServer; written: number }[] = []
|
|
36
|
+
|
|
37
|
+
if (!existsSync(root)) {
|
|
38
|
+
return []
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const key of readdirSync(root)) {
|
|
42
|
+
const file = join(root, key, 'server.json')
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
const info = statSync(file)
|
|
46
|
+
|
|
47
|
+
if (!info.isFile()) {
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const server = parseDevtoolsServerRecord(key, readFileSync(file, 'utf8'), isAbsolute)
|
|
52
|
+
|
|
53
|
+
if (server) {
|
|
54
|
+
found.push({ server, written: info.mtimeMs })
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return found.sort((a, b) => b.written - a.written).map(({ server }) => server)
|
|
62
|
+
}
|