/
general.ts
134 lines (124 loc) · 3.57 KB
/
general.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { existsSync } from 'node:fs'
import type { Component, NuxtApp, NuxtPage } from 'nuxt/schema'
import type { Import, Unimport } from 'unimport'
import { resolveBuiltinPresets } from 'unimport'
import { resolve } from 'pathe'
import { logger } from '@nuxt/kit'
import type { HookInfo, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
import { setupHooksDebug } from '../runtime/shared/hooks'
export function setupGeneralRPC({ nuxt, refresh }: NuxtDevtoolsServerContext) {
const components: Component[] = []
const imports: Import[] = []
const importPresets: Import[] = []
let importDirs: string[] = []
const serverPages: NuxtPage[] = []
let serverApp: NuxtApp | undefined
const serverHooks: Record<string, HookInfo> = setupHooksDebug(nuxt.hooks)
let unimport: Unimport | undefined
let app: NuxtApp | undefined
// Nuxt Hooks to collect data
nuxt.hook('components:extend', (v) => {
components.length = 0
components.push(...v)
components.sort((a, b) => a.pascalName.localeCompare(b.pascalName))
refresh('getComponents')
})
nuxt.hook('imports:extend', (v) => {
imports.length = 0
imports.push(...v)
refresh('getAutoImports')
})
nuxt.hook('pages:extend', (v) => {
serverPages.length = 0
const pagesSet = new Set(v)
function searchChildren(page: NuxtPage) {
if (pagesSet.has(page))
return
pagesSet.add(page)
page.children?.forEach(searchChildren)
}
v.forEach(searchChildren)
serverPages.push(...Array.from(pagesSet).sort((a, b) => a.path.localeCompare(b.path)))
refresh('getServerPages')
})
nuxt.hook('app:resolve', (app) => {
serverApp = app
})
nuxt.hook('imports:sources', async (v) => {
const result = (await resolveBuiltinPresets(v)).flat()
importPresets.length = 0
importPresets.push(...result)
refresh('getAutoImports')
})
nuxt.hook('imports:context', (_unimport: Unimport) => {
unimport = _unimport
})
nuxt.hook('imports:dirs', (dirs) => {
importDirs = dirs
})
nuxt.hook('app:resolve', (v) => {
app = v
})
return {
getServerConfig() {
return nuxt.options
},
getServerApp() {
return serverApp
},
getComponents() {
return components
},
async getComponentsRelationships() {
return [] // replaced by vite-inspector setup
},
getServerPages() {
return serverPages
},
getAutoImports() {
return {
imports: [
...imports,
...importPresets,
],
metadata: unimport?.getMetadata(),
dirs: importDirs,
}
},
getServerLayouts() {
return Object.values(app?.layouts || [])
},
getServerHooks() {
return Object.values(serverHooks)
},
async openInEditor(input: string) {
if (input.startsWith('./'))
input = resolve(process.cwd(), input)
// separate line and column syntax
const match = input.match(/^(.*?)(:[:\d]*)$/)
let suffix = ''
if (match) {
input = match[1]
suffix = match[2]
}
// search for existing path
const path = [
input,
`${input}.js`,
`${input}.mjs`,
`${input}.ts`,
].find(i => existsSync(i))
if (path) {
// @ts-expect-error missin types
await import('launch-editor').then(r => (r.default || r)(path + suffix))
}
else {
console.error('File not found:', input)
}
},
restartNuxt(hard = true) {
logger.info('Restarting Nuxt...')
return nuxt.callHook('restart', { hard })
},
} satisfies Partial<ServerFunctions>
}