/
state.ts
314 lines (268 loc) · 8.74 KB
/
state.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import type { Component } from 'nuxt/schema'
import { $fetch } from 'ofetch'
import { unref } from 'vue'
import type { Ref } from 'vue'
import type { MaybeRef } from '@vueuse/core'
import { objectPick } from '@antfu/utils'
import type { HookInfo, InstallModuleReturn, InstalledModuleInfo, ModuleBuiltinTab, ModuleCustomTab, ModuleStaticInfo, RouteInfo, TabCategory } from '../../src/types'
const ignoredModules = [
'pages',
'meta',
'components',
'imports',
'nuxt-config-schema',
'@nuxt/devtools',
'@nuxt/telemetry',
]
export function useModulesList() {
return useAsyncData('modules-list', async () => {
const modules = await $fetch<ModuleStaticInfo[]>('https://cdn.jsdelivr.net/npm/@nuxt/modules@latest/modules.json')
return modules
.filter((m: ModuleStaticInfo) => !ignoredModules.includes(m.npm) && m.compatibility.nuxt.includes('^3'))
}).data
}
export function useInstalledModules() {
return useState('installed-modules', () => {
const config = useServerConfig()
const modules = useModulesList()
return computed(() => (config.value?._installedModules || [])
.map((mod): InstalledModuleInfo => {
const isPackageModule = mod.entryPath && isNodeModulePath(mod.entryPath)
const name = mod.meta?.name
? mod.meta?.name
: mod.entryPath
? isPackageModule
? getModuleNameFromPath(mod.entryPath)
: config.value?.rootDir
? parseReadablePath(mod.entryPath, config.value?.rootDir).path
: undefined
: undefined
const isUninstallable = config.value?.modules?.includes(name)
const info = modules.value?.find(m => m.npm === name)
return {
name,
isPackageModule,
isUninstallable,
info,
...mod,
}
})
.filter(i => !i.name || !ignoredModules.includes(i.name)),
)
})
}
type ModuleActionType = 'install' | 'uninstall'
export const ModuleDialog = createTemplatePromise<boolean, [info: ModuleStaticInfo, result: InstallModuleReturn, type: ModuleActionType]>()
export async function useModuleAction(item: ModuleStaticInfo, type: ModuleActionType) {
const router = useRouter()
const method = type === 'install' ? rpc.installNuxtModule : rpc.uninstallNuxtModule
const result = await method(item.npm, true)
if (!result.commands)
return
if (!await ModuleDialog.start(item, result, type))
return
router.push(`/modules/terminals?id=${encodeURIComponent(result.processId)}`)
await method(item.npm, false)
}
export function useComponents() {
const client = useClient()
const serverComponents = useAsyncState('getComponents', () => rpc.getComponents())
const globalComponents = computed(() =>
Object
.entries(client.value?.nuxt?.vueApp._context.components || {})
.map(([key]) => ({
pascalName: key,
global: true,
} as unknown as Component))
// filter out lazy components
.filter(i => !/^Lazy[A-Z]/.test(i.pascalName))
// dedupe server components
.filter(i => !(serverComponents.value || [])
.find((s: any) => s.pascalName === i.pascalName)),
)
return computed(() => [
...globalComponents.value,
...serverComponents.value || [],
].sort((a: any, b: any) => a.pascalName.localeCompare(b.pascalName)))
}
export function useServerPages() {
return useAsyncState('getServerPages', () => rpc.getServerPages())
}
export function useServerRoutes() {
return useAsyncState('getServerRoutes', () => rpc.getServerRoutes())
}
export function useComponentsRelationships() {
return useAsyncState('getComponentsRelationships', () => rpc.getComponentsRelationships())
}
export function useServerHooks() {
return useAsyncState('getServerHooks', () => rpc.getServerHooks(), {
default: () => [],
}) as Ref<HookInfo[]>
}
export function useLayouts() {
return useAsyncState('getServerLayouts', () => rpc.getServerLayouts())
}
export function useAutoImports() {
return useAsyncState('getAutoImports', () => rpc.getAutoImports())
}
export function useStaticAssets() {
return useAsyncState('getStaticAssets', () => rpc.getStaticAssets())
}
export function useServerConfig() {
return useAsyncState('getServerConfig', () => rpc.getServerConfig())
}
export function useServerApp() {
return useAsyncState('getServerApp', () => rpc.getServerApp())
}
export function useCustomTabs() {
return useAsyncState('getCustomTabs', () => rpc.getCustomTabs())
}
export function useTerminals() {
return useAsyncState('getTerminals', () => rpc.getTerminals())
}
export function useAllTabs() {
const customTabs = useCustomTabs()
const settings = useDevToolsOptions()
const router = useRouter()
const client = useClient()
const builtin = computed(() => [
...router.getRoutes()
.filter(route => route.path.startsWith('/modules/') && route.meta.title && !route.meta.wip)
.filter(route => !route.meta.experimental || (route.meta.experimental && settings.showExperimentalFeatures.value))
.sort((a, b) => (a.meta.order || 100) - (b.meta.order || 100))
.map((i): ModuleBuiltinTab => {
return {
name: i.name as string,
path: i.path,
...i.meta,
}
}),
<ModuleBuiltinTab>{
name: 'builtin-inspector',
title: 'Inspect Vue components',
icon: 'i-carbon-select-window',
category: 'app',
show() {
return !!client.value?.inspector?.instance
},
onClick() {
if (!client.value?.inspector?.instance)
return
client.value.inspector.enable()
router.push('/__inspecting')
},
},
...(customTabs.value || []).filter(i => i.name.startsWith('builtin-')),
])
const custom = computed(() => (customTabs.value || [])
.filter(i => !i.name.startsWith('builtin-')))
return computed(() => [
...builtin.value,
...custom.value,
])
}
function getCategorizedRecord(): Record<TabCategory, (ModuleCustomTab | ModuleBuiltinTab)[]> {
return {
app: [],
server: [],
analyze: [],
modules: [],
documentation: [],
advanced: [],
}
}
export function getCategorizedTabs(tabs: MaybeRef<(ModuleCustomTab | ModuleBuiltinTab)[]>) {
return computed(() => {
const categories = getCategorizedRecord()
for (const tab of unref(tabs)) {
const category = (tab.category || 'app')
if (!categories[category])
console.warn(`Unknown tab category: ${category}`)
else
categories[category].push(tab)
}
for (const key of Object.keys(categories)) {
if (categories[key as TabCategory].length === 0)
delete categories[key as TabCategory]
}
return Object.entries(categories)
})
}
export function useCategorizedTabs(enabledOnly = true) {
const tabs = enabledOnly
? useEnabledTabs()
: useAllTabs()
return getCategorizedTabs(tabs)
}
export function useEnabledTabs() {
const tabs = useAllTabs()
const settings = useDevToolsOptions()
const categoryOrder = Object.keys(getCategorizedRecord())
return computed(() => tabs.value
.filter((tab) => {
const _tab = tab as ModuleBuiltinTab
if (_tab.show && !_tab.show())
return false
if (settings.hiddenTabs.value.includes(_tab.name))
return false
if (settings.hiddenTabCategories.value.includes(tab.category || 'app'))
return false
return true
})
.sort((a, b) => categoryOrder.indexOf(a.category || 'app') - categoryOrder.indexOf(b.category || 'app')),
)
}
export function useAllRoutes() {
const router = useClientRouter()
const serverPages = useServerPages()
return computed((): RouteInfo[] => {
return (router.value?.getRoutes() || [])
.map(i => objectPick(i, ['path', 'name', 'meta', 'props', 'children']))
.map((i) => {
return {
...serverPages.value?.find(j => j.name && j.name === i.name),
...i,
}
})
})
}
interface RestartDialog {
id: string
message: string
}
export function useRestartDialogs() {
return useState<RestartDialog[]>('devtools:restart-dialogs', () => [])
}
export interface VfsData {
rootDir: string
entries: {
id: string
path: string
}[]
}
export interface VfsFile {
id: string
content: string
}
export function useVirtualFiles() {
const { data } = useFetch<VfsData>('/_vfs.json', {
key: 'vfs-list',
baseURL: '/',
responseType: 'json',
})
return data
}
export function useMergedRouteList() {
const serverPages = useServerPages()
const router = useClientRouter()
return computed((): RouteInfo[] => {
return (router.value?.getRoutes() || [])
.map(i => objectPick(i, ['path', 'name', 'meta', 'props', 'children']))
.map((i) => {
return {
...serverPages.value?.find(j => j.name && j.name === i.name),
...i,
}
})
})
}