-
Notifications
You must be signed in to change notification settings - Fork 161
/
index.ts
138 lines (119 loc) · 3.43 KB
/
index.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
import { useNuxt } from '@nuxt/kit'
import type { BirpcGroup } from 'birpc'
import { execa } from 'execa'
import type { ModuleCustomTab, NuxtDevtoolsInfo, NuxtDevtoolsServerContext, SubprocessOptions, TerminalState } from './types'
/**
* Hooks to extend a custom tab in devtools.
*
* Provide a function to pass a factory that can be updated dynamically.
*/
export function addCustomTab(tab: ModuleCustomTab | (() => ModuleCustomTab | Promise<ModuleCustomTab>), nuxt = useNuxt()) {
nuxt.hook('devtools:customTabs', async (tabs) => {
if (typeof tab === 'function')
tab = await tab()
tabs.push(tab)
})
}
/**
* Retrigger update for custom tabs, `devtools:customTabs` will be called again.
*/
export function refreshCustomTabs(nuxt = useNuxt()) {
return nuxt.callHook('devtools:customTabs:refresh')
}
/**
* Create a subprocess that handled by the DevTools.
*/
export function startSubprocess(
execaOptions: SubprocessOptions,
tabOptions: TerminalState,
nuxt = useNuxt(),
) {
const id = tabOptions.id
let restarting = false
function start() {
const process = execa(
execaOptions.command,
execaOptions.args,
{
...execaOptions,
env: {
COLORS: 'true',
FORCE_COLOR: 'true',
...execaOptions.env,
},
},
)
nuxt.callHook('devtools:terminal:write', { id, data: `> ${[execaOptions.command, ...execaOptions.args || []].join(' ')}\n\n` })
process.stdout!.on('data', (data) => {
nuxt.callHook('devtools:terminal:write', { id, data: data.toString() })
})
process.stderr!.on('data', (data) => {
nuxt.callHook('devtools:terminal:write', { id, data: data.toString() })
})
process.on('exit', (code) => {
if (!restarting) {
nuxt.callHook('devtools:terminal:write', { id, data: `\n> process terminalated with ${code}\n` })
nuxt.callHook('devtools:terminal:exit', { id, code: code || 0 })
}
})
return process
}
register()
nuxt.hook('close', () => {
terminate()
})
let process = start()
function restart() {
restarting = true
process?.kill()
clear()
process = start()
restarting = false
}
function clear() {
tabOptions.buffer = ''
register()
}
function terminate() {
restarting = false
try {
process?.kill()
}
catch (e) {
}
nuxt.callHook('devtools:terminal:remove', { id })
}
function register() {
nuxt.callHook('devtools:terminal:register', {
onActionRestart: tabOptions.restartable === false ? undefined : restart,
onActionTerminate: tabOptions.terminatable === false ? undefined : terminate,
isTerminated: false,
...tabOptions,
})
}
return {
getProcess() {
return process
},
terminate,
restart,
clear,
}
}
export function extendServerRpc<ClientFunctions = {}, ServerFunctions = {}>(
namespace: string,
functions: ServerFunctions,
nuxt = useNuxt(),
): BirpcGroup<ClientFunctions, ServerFunctions> {
const ctx = _getContext(nuxt)
if (!ctx)
throw new Error('Failed to get devtools context.')
return ctx.extendServerRpc<ClientFunctions, ServerFunctions>(namespace, functions)
}
export function onDevToolsInitialized(fn: (info: NuxtDevtoolsInfo) => void, nuxt = useNuxt()) {
nuxt.hook('devtools:initialized', fn)
}
function _getContext(nuxt = useNuxt()): NuxtDevtoolsServerContext | undefined {
// @ts-expect-error - internal
return nuxt?.devtools
}