Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions packages/devtools/src/server-rpc/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,35 @@ export function setupGeneralRPC({
},
async openInEditor(token: string, input: string): Promise<boolean> {
await ensureDevAuthToken(token)
if (input.startsWith('./') || !ABSOLUTE_PATH_RE.test(input))
input = resolve(process.cwd(), input)
const isRelative = input.startsWith('./') || !ABSOLUTE_PATH_RE.test(input)

// separate line and column syntax
// separate line and column syntax before resolving, so `:line:col`
// is not part of the filename
const match = input.match(FILE_LINE_COL_RE)
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))
// Inspector paths are workspace-relative. cwd is the package directory
// in a workspace, so try cwd, rootDir, then workspaceDir.
const candidates = isRelative
? [
...new Set([
process.cwd(),
nuxt.options.rootDir,
nuxt.options.workspaceDir,
].filter(Boolean)),
].map(dir => resolve(dir, input))
: [input]

const path = candidates
.flatMap(i => [i, `${i}.js`, `${i}.mjs`, `${i}.ts`])
.find(i => existsSync(i))

if (!path) {
console.error('File not found:', input)
console.error('File not found:', candidates[0])
return false
}

Expand Down
119 changes: 119 additions & 0 deletions packages/devtools/test/open-in-editor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { Nuxt } from 'nuxt/schema'
import type { NuxtDevtoolsServerContext } from '../src/types'
import fsp from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { createHooks } from 'hookable'
import { join, normalize } from 'pathe'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { setupGeneralRPC } from '../src/server-rpc/general'
import { getOptions } from '../src/server-rpc/options'

vi.mock('launch-editor', () => ({
default: vi.fn(),
}))

vi.mock('../src/server-rpc/options', () => ({
getOptions: vi.fn(),
}))

const TOKEN = 'token'

function fakeContext(root: string, handledByHook = true) {
const hooks = createHooks()
const opened: string[] = []
const ctx = {
nuxt: {
options: {
rootDir: join(root, 'docs'),
workspaceDir: root,
},
hooks,
hook: hooks.hook.bind(hooks),
} as unknown as Nuxt,
options: {},
refresh: () => {},
ensureDevAuthToken: async () => {},
openInEditorHooks: [
(filepath: string) => {
opened.push(filepath)
return handledByHook
},
],
} as unknown as NuxtDevtoolsServerContext
return { ctx, opened }
}

describe('openInEditor', () => {
let root: string

beforeEach(async () => {
root = normalize(await fsp.mkdtemp(join(tmpdir(), 'devtools-editor-')))
await fsp.mkdir(join(root, 'docs/app/components'), { recursive: true })
await fsp.mkdir(join(root, 'src/components/button'), { recursive: true })
await fsp.writeFile(join(root, 'docs/app/components/DocPage.vue'), '<template><div /></template>')
await fsp.writeFile(join(root, 'src/components/button/Button.demo.vue'), '<template><div /></template>')
vi.spyOn(process, 'cwd').mockReturnValue(join(root, 'docs'))
vi.mocked(getOptions).mockReset()
})

afterEach(async () => {
vi.restoreAllMocks()
await fsp.rm(root, { recursive: true, force: true })
})

it('opens a path relative to the current working directory', async () => {
const { ctx, opened } = fakeContext(root)
const { openInEditor } = setupGeneralRPC(ctx)

await expect(openInEditor(TOKEN, 'app/components/DocPage.vue')).resolves.toBe(true)
expect(opened).toEqual([
join(root, 'docs/app/components/DocPage.vue'),
])
})

it('opens a path relative to the root directory when the cwd holds no such file', async () => {
vi.spyOn(process, 'cwd').mockReturnValue(join(root, 'tools'))
const { ctx, opened } = fakeContext(root)
const { openInEditor } = setupGeneralRPC(ctx)

await expect(openInEditor(TOKEN, 'app/components/DocPage.vue')).resolves.toBe(true)
expect(opened).toEqual([
join(root, 'docs/app/components/DocPage.vue'),
])
})

it('opens a workspace-relative path while the cwd is the package directory', async () => {
const { ctx, opened } = fakeContext(root)
const { openInEditor } = setupGeneralRPC(ctx)

await expect(openInEditor(TOKEN, 'docs/app/components/DocPage.vue:3:5')).resolves.toBe(true)
expect(opened).toEqual([`${join(root, 'docs/app/components/DocPage.vue')}:3:5`])
})

it('opens a file that lives at the workspace root, outside the Nuxt package', async () => {
const { ctx, opened } = fakeContext(root)
const { openInEditor } = setupGeneralRPC(ctx)

await expect(openInEditor(TOKEN, 'src/components/button/Button.demo.vue:27:2')).resolves.toBe(true)
expect(opened).toEqual([`${join(root, 'src/components/button/Button.demo.vue')}:27:2`])
})

it('opens an absolute path without touching the base directories', async () => {
const { ctx, opened } = fakeContext(root)
const { openInEditor } = setupGeneralRPC(ctx)
const filepath = join(root, 'docs/app/components/DocPage.vue')

await expect(openInEditor(TOKEN, filepath)).resolves.toBe(true)
expect(opened).toEqual([filepath])
})

it('reports the cwd candidate when the file exists under none of the bases', async () => {
const error = vi.spyOn(console, 'error').mockImplementation(() => {})
const { ctx, opened } = fakeContext(root)
const { openInEditor } = setupGeneralRPC(ctx)

await expect(openInEditor(TOKEN, 'app/components/Missing.vue')).resolves.toBe(false)
expect(opened).toEqual([])
expect(error).toHaveBeenCalledWith('File not found:', join(root, 'docs/app/components/Missing.vue'))
})
})
Loading