Skip to content

Commit 9b41467

Browse files
committed
fix: launch editor with proper project filepaths
1 parent 4424c2f commit 9b41467

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

packages/dev-server/src/plugins/devtools/devtoolsPlugin.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FastifyInstance } from 'fastify';
22
import fastifyPlugin from 'fastify-plugin';
33
import launchEditor from 'launch-editor';
44
import open from 'open';
5+
import { parseSourceFilename } from '../../utils/parseSourceFilename.js';
56

67
interface OpenURLRequestBody {
78
url: string;
@@ -18,7 +19,10 @@ function parseRequestBody<T>(body: unknown): T {
1819
throw new Error(`Unsupported body type: ${typeof body}`);
1920
}
2021

21-
async function devtoolsPlugin(instance: FastifyInstance) {
22+
async function devtoolsPlugin(
23+
instance: FastifyInstance,
24+
{ rootDir }: { rootDir: string }
25+
) {
2226
// reference implementation in `@react-native-community/cli-server-api`:
2327
// https://github.com/react-native-community/cli/blob/46436a12478464752999d34ed86adf3212348007/packages/cli-server-api/src/openURLMiddleware.ts
2428
instance.route({
@@ -37,11 +41,9 @@ async function devtoolsPlugin(instance: FastifyInstance) {
3741
method: ['POST'],
3842
url: '/open-stack-frame',
3943
handler: async (request, reply) => {
40-
const { file, lineNumber } = parseRequestBody<OpenStackFrameRequestBody>(
41-
request.body
42-
);
43-
// TODO fix rewriting of `webpack://` to rootDir of the project
44-
launchEditor(`${file}:${lineNumber}`, process.env.REACT_EDITOR);
44+
const body = parseRequestBody<OpenStackFrameRequestBody>(request.body);
45+
const filepath = parseSourceFilename(body.file, rootDir);
46+
launchEditor(`${filepath}:${body.lineNumber}`, process.env.REACT_EDITOR);
4547
reply.send('OK');
4648
},
4749
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'node:path';
2+
3+
export function parseSourceFilename(filename: string, rootDir: string) {
4+
const prefix = filename.split('/')[0];
5+
let filepath = filename;
6+
7+
// Handle [projectRoot] and [projectRoot^N] prefixes
8+
const projectRootMatch = prefix.match(/^\[projectRoot(?:\^(\d+))?\]$/);
9+
if (projectRootMatch) {
10+
const upLevels = projectRootMatch[1];
11+
if (upLevels) {
12+
// For [projectRoot^N], go up N levels from rootDir
13+
const upPath = '../'.repeat(Number(upLevels));
14+
filepath = filepath.replace(`${prefix}/`, rootDir + '/' + upPath);
15+
} else {
16+
// For plain [projectRoot], just replace with rootDir
17+
filepath = filepath.replace('[projectRoot]', rootDir);
18+
}
19+
}
20+
21+
return path.resolve(filepath);
22+
}

0 commit comments

Comments
 (0)