Skip to content

Commit 463f6ad

Browse files
gsxdsmantfu
andauthored
feat: improves vscode integration, support multiple backends (#763)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 3703d20 commit 463f6ad

File tree

5 files changed

+125
-15
lines changed

5 files changed

+125
-15
lines changed

docs/content/1.guide/1.features.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,72 @@ Learn more about [Nitro Storage](https://nitro.unjs.io/guide/storage)
9090

9191
The VS Code Server integration in Nuxt DevTools enhances your development experience by bringing the power of Visual Studio Code directly into your browser. With this feature, you can seamlessly edit and debug your Nuxt projects using the familiar interface of VS Code.
9292

93-
To get started with VS Code Server, follow the installation instructions provided by [Code Server Installation Guide](https://coder.com/docs/code-server/latest/install)
93+
Nuxt DevTools supports the following ways of integrating with VS Code:
94+
95+
### Connecting to an existing code-server instance
96+
97+
Set `reuseExistingServer` to true in runtime config for `devtools/vscode` and set the `port` option to specify a port (defalts to 3080):
98+
99+
```ts [nuxt.config.ts]
100+
export default defineNuxtConfig({
101+
devtools: {
102+
vscode: {
103+
reuseExistingServer: true,
104+
port: 3090
105+
}
106+
}
107+
})
108+
```
109+
110+
### Running a code-server instance locally
111+
112+
You can use either the [Microsoft Visual Studio Code Server](https://code.visualstudio.com/docs/remote/vscode-server) (via the `code` or `code-server` cli tools) or the [Coder VS Code Server](https://coder.com/docs/code-server/latest/install) (via the `code-server` cli tool) by setting the `codeServer` parameter under `devtools/vscode` in the runtime configuration.
113+
114+
Options for the codeServer parameter are:
115+
|Type|Option|
116+
|----|------|
117+
|MS Code CLI|`ms-code-cli`|
118+
|MS Code Server|`ms-code-server`|
119+
|Coder Code Server|`coder-code-server`|
120+
121+
You can set the `port` parameter to listen on a specific port (default 3080) and you can set the `host` parameter if you need to listen on a particular host interface (useful for devcontainers or docker containers that listen on ipv6 by default).
122+
123+
**Example**:
124+
125+
```ts [nuxt.config.ts]
126+
export default defineNuxtConfig({
127+
devtools: {
128+
vscode: {
129+
codeServer: 'ms-code-server',
130+
host: '0.0.0.0',
131+
port: 3090
132+
}
133+
}
134+
})
135+
```
136+
137+
### Remotely via a MS VS Code server tunnel
138+
139+
Set the `mode` option in `devtools/vscode` runtime configuration to `tunnel`. You can set the name of the tunnel to connect to using the `tunnel` option under `devtools/vscode/tunnel` in runtime configuration)
140+
141+
```ts [nuxt.config.ts]
142+
export default defineNuxtConfig({
143+
devtools: {
144+
vscode: {
145+
mode: 'tunnel',
146+
tunnel: {
147+
name: 'my-tunnel-name'
148+
}
149+
}
150+
}
151+
})
152+
```
153+
154+
### Code Server Installation Instructions
155+
156+
To get started with Microsoft VS Code Server, follow the installation instructions provided by [Microsoft Visual Studio Code Server](https://code.visualstudio.com/docs/remote/vscode-server)
157+
158+
To get started with Coder Code Server, follow the installation instructions provided by [Code Server Installation Guide](https://coder.com/docs/code-server/latest/install)
94159

95160
For more information on the benefits and features of VS Code Server, refer to [the official Visual Studio Code blog](https://code.visualstudio.com/blogs/2022/07/07/vscode-server)
96161

packages/devtools-kit/src/_types/integrations.ts

+7
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,10 @@ export interface ComponentWithRelationships {
232232
dependencies?: string[]
233233
dependents?: string[]
234234
}
235+
236+
export interface CodeServerOptions {
237+
codeBinary: string
238+
launchArg: string
239+
licenseTermsArg: string
240+
connectionTokenArg: string
241+
}

packages/devtools-kit/src/_types/options.ts

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { VitePluginInspectorOptions } from 'vite-plugin-vue-inspector'
33
import type { ModuleCustomTab } from './custom-tabs'
44
import type { ServerRouteInfo, ServerRouteInput, ServerTaskInfo } from './integrations'
55

6+
export type CodeServerType = 'ms-code-cli' | 'ms-code-server' | 'coder-code-server'
7+
68
export interface ModuleOptions {
79
/**
810
* Enable DevTools
@@ -153,6 +155,21 @@ export interface VSCodeIntegrationOptions {
153155
* Options for VS Code tunnel
154156
*/
155157
tunnel?: VSCodeTunnelOptions
158+
159+
/**
160+
* Determines which binary and arguments to use for VS Code.
161+
*
162+
* By default, uses the MS Code Server (ms-code-server).
163+
* Can alternatively use the open source Coder code-server (coder-code-server),
164+
* or the MS VS Code CLI (ms-code-cli)
165+
* @default 'ms-code-server'
166+
*/
167+
codeServer?: CodeServerType
168+
169+
/**
170+
* Host address to listen on. Unspecified by default.
171+
*/
172+
host?: string
156173
}
157174

158175
export interface VSCodeTunnelOptions {

packages/devtools/src/integrations/vscode.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NuxtDevtoolsServerContext } from '../types'
1+
import type { CodeServerOptions, CodeServerType, NuxtDevtoolsServerContext } from '../types'
22
import { existsSync } from 'node:fs'
33
import fs from 'node:fs/promises'
44
import { hostname } from 'node:os'
@@ -10,13 +10,35 @@ import { checkPort, getPort } from 'get-port-please'
1010
import which from 'which'
1111
import { LOG_PREFIX } from '../logger'
1212

13-
export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevtoolsServerContext) {
14-
const installed = !!await which('code-server').catch(() => null)
13+
const codeBinaryOptions: Record<CodeServerType, CodeServerOptions> = {
14+
'ms-code-cli': {
15+
codeBinary: 'code',
16+
launchArg: 'serve-web',
17+
licenseTermsArg: '--accept-server-license-terms',
18+
connectionTokenArg: '--without-connection-token',
19+
},
20+
'ms-code-server': {
21+
codeBinary: 'code-server',
22+
launchArg: 'serve-local',
23+
licenseTermsArg: '--accept-server-license-terms',
24+
connectionTokenArg: '--without-connection-token',
25+
},
26+
'coder-code-server': {
27+
codeBinary: 'code-server',
28+
launchArg: 'serve-local',
29+
licenseTermsArg: '',
30+
connectionTokenArg: '',
31+
},
32+
}
1533

34+
export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevtoolsServerContext) {
1635
const vsOptions = options?.vscode || {}
17-
36+
const codeServer: CodeServerType = vsOptions?.codeServer || 'ms-code-server'
37+
const { codeBinary, launchArg, licenseTermsArg, connectionTokenArg } = codeBinaryOptions[codeServer]
38+
const installed = !!await which(codeBinary).catch(() => null)
1839
let port = vsOptions?.port || 3080
1940
let url = `http://localhost:${port}`
41+
const host = vsOptions?.host ? `--host=${vsOptions.host}` : ''
2042
let loaded = false
2143
let promise: Promise<void> | null = null
2244
const mode = vsOptions?.mode || 'local-serve'
@@ -32,7 +54,7 @@ export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevto
3254
// we can open files in VS Code Server
3355
try {
3456
const { port } = JSON.parse(await fs.readFile(vscodeServerControllerFile, 'utf-8')) as any
35-
const url = `http://localhost:${port}/open?path=${encodeURIComponent(file)}`
57+
const url = `http://localhost:${port}/open?path=${encodeURIComponent(`${root}/${file}`)}`
3658
await fetch(url)
3759
rpc.broadcast.navigateTo('/modules/custom-builtin-vscode')
3860
return true
@@ -64,21 +86,20 @@ export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevto
6486

6587
// Install VS Code Server Controller
6688
// https://github.com/antfu/vscode-server-controller
67-
execa('code-server', [
68-
'serve-local',
69-
'--accept-server-license-terms',
89+
execa(codeBinary, [
7090
'--install-extension',
7191
'antfu.vscode-server-controller',
7292
], { stderr: 'inherit', stdout: 'ignore', reject: false })
7393

7494
startSubprocess(
7595
{
76-
command: 'code-server',
96+
command: codeBinary,
7797
args: [
78-
'serve-local',
79-
'--accept-server-license-terms',
80-
'--without-connection-token',
98+
launchArg,
99+
licenseTermsArg,
100+
connectionTokenArg,
81101
`--port=${port}`,
102+
host,
82103
],
83104
},
84105
{
@@ -144,7 +165,7 @@ export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevto
144165
icon: 'bxl-visual-studio',
145166
category: 'modules',
146167
requireAuth: true,
147-
view: !installed
168+
view: !installed && !(vsOptions?.mode === 'tunnel')
148169
? {
149170
type: 'launch',
150171
title: 'Install VS Code Server',

packages/devtools/src/server-rpc/general.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function setupGeneralRPC({
177177

178178
try {
179179
for (const hook of openInEditorHooks) {
180-
const result = await hook(path)
180+
const result = await hook(path + suffix)
181181
if (result)
182182
return true
183183
}

0 commit comments

Comments
 (0)