Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improves vscode integration, support multiple backends #763

Merged
merged 11 commits into from
Dec 26, 2024
7 changes: 7 additions & 0 deletions packages/devtools-kit/src/_types/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,10 @@ export interface ComponentWithRelationships {
dependencies?: string[]
dependents?: string[]
}

export interface CodeServerOptions {
codeBinary: string
launchArg: string
licenseTermsArg: string
connectionTokenArg: string
}
16 changes: 16 additions & 0 deletions packages/devtools-kit/src/_types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { VitePluginInspectorOptions } from 'vite-plugin-vue-inspector'
import type { ModuleCustomTab } from './custom-tabs'
import type { ServerRouteInfo, ServerRouteInput, ServerTaskInfo } from './integrations'

export type CodeServerType = 'ms-code-cli' | 'ms-code-server' | 'coder-code-server'
export interface ModuleOptions {
/**
* Enable DevTools
Expand Down Expand Up @@ -153,6 +154,21 @@ export interface VSCodeIntegrationOptions {
* Options for VS Code tunnel
*/
tunnel?: VSCodeTunnelOptions

/**
* Determines which binary and arguments to use for VS Code.
*
* By default, uses the MS Code Server (ms-code-server).
* Can alternatively use the open source Coder code-server (coder-code-server),
* or the MS VS Code CLI (ms-code-cli)
* @default 'ms-code-server'
*/
codeServer?: CodeServerType

/**
* Host address to listen on. Unspecified by default.
*/
host?: string
}

export interface VSCodeTunnelOptions {
Expand Down
50 changes: 37 additions & 13 deletions packages/devtools/src/integrations/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NuxtDevtoolsServerContext } from '../types'
import type { CodeServerOptions, CodeServerType, NuxtDevtoolsServerContext } from '../types'
import { existsSync } from 'node:fs'
import fs from 'node:fs/promises'
import { hostname } from 'node:os'
Expand All @@ -10,13 +10,35 @@ import { checkPort, getPort } from 'get-port-please'
import which from 'which'
import { LOG_PREFIX } from '../logger'

export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevtoolsServerContext) {
const installed = !!await which('code-server').catch(() => null)
const codeBinaryOptions: Record<CodeServerType, CodeServerOptions> = {
'ms-code-cli': {
codeBinary: 'code',
launchArg: 'serve-web',
licenseTermsArg: '--accept-server-license-terms',
connectionTokenArg: '--without-connection-token',
},
'ms-code-server': {
codeBinary: 'code-server',
launchArg: 'serve-local',
licenseTermsArg: '--accept-server-license-terms',
connectionTokenArg: '--without-connection-token',
},
'coder-code-server': {
codeBinary: 'code-server',
launchArg: 'serve-local',
licenseTermsArg: '',
connectionTokenArg: '',
},
}

export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevtoolsServerContext) {
const vsOptions = options?.vscode || {}

const codeServer: CodeServerType = vsOptions?.codeServer || 'ms-code-server'
const { codeBinary, launchArg, licenseTermsArg, connectionTokenArg } = codeBinaryOptions[codeServer]
const installed = !!await which(codeBinary).catch(() => null)
let port = vsOptions?.port || 3080
let url = `http://localhost:${port}`
const host = vsOptions?.host ? `--host=${vsOptions.host}` : ''
let loaded = false
let promise: Promise<void> | null = null
const mode = vsOptions?.mode || 'local-serve'
Expand All @@ -32,7 +54,7 @@ export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevto
// we can open files in VS Code Server
try {
const { port } = JSON.parse(await fs.readFile(vscodeServerControllerFile, 'utf-8')) as any
const url = `http://localhost:${port}/open?path=${encodeURIComponent(file)}`
const url = `http://localhost:${port}/open?path=${encodeURIComponent(`${root}/${file}`)}`
await fetch(url)
rpc.broadcast.navigateTo('/modules/custom-builtin-vscode')
return true
Expand Down Expand Up @@ -64,21 +86,23 @@ export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevto

// Install VS Code Server Controller
// https://github.com/antfu/vscode-server-controller
execa('code-server', [
'serve-local',
'--accept-server-license-terms',
execa(codeBinary, [
launchArg,
licenseTermsArg,
'--install-extension',
'antfu.vscode-server-controller',
host,
], { stderr: 'inherit', stdout: 'ignore', reject: false })

startSubprocess(
{
command: 'code-server',
command: codeBinary,
args: [
'serve-local',
'--accept-server-license-terms',
'--without-connection-token',
launchArg,
licenseTermsArg,
connectionTokenArg,
`--port=${port}`,
host,
],
},
{
Expand Down Expand Up @@ -144,7 +168,7 @@ export async function setup({ nuxt, options, openInEditorHooks, rpc }: NuxtDevto
icon: 'bxl-visual-studio',
category: 'modules',
requireAuth: true,
view: !installed
view: !installed && !(vsOptions?.mode === 'tunnel')
? {
type: 'launch',
title: 'Install VS Code Server',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if we could provide a page in the docs and change the guide here to mention that there are different options to install. Are you interested in helping that as well? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I can do that!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming you mean updating this page in the docs?

When you say guide, which guide are you referring to?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, exactly!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the ms-code-cli the default? It seems to be the recommended approach from MS, but it's different from the current behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note - would be good to publish antfu.vscode-server-controller into open-vsx marketplace so the install step works for code-server (from Coder) - https://open-vsx.org/user-settings/extensions

Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/server-rpc/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function setupGeneralRPC({

try {
for (const hook of openInEditorHooks) {
const result = await hook(path)
const result = await hook(path + suffix)
if (result)
return true
}
Expand Down