|
1 | | -import { normalizePath } from "vite" |
2 | | -import { checkPath } from "./utils.js" |
| 1 | +import { normalizePath } from 'vite' |
| 2 | +import { checkPath } from './utils.js' |
3 | 3 |
|
4 | 4 | type OpenSourceData = { |
5 | | - type: "open-source" |
6 | | - data: { |
7 | | - /** The source file to open */ |
8 | | - source?: string |
9 | | - /** The react router route ID, usually discovered via the hook useMatches */ |
10 | | - routeID?: string |
11 | | - /** The line number in the source file */ |
12 | | - line?: number |
13 | | - /** The column number in the source file */ |
14 | | - column?: number |
15 | | - } |
| 5 | + type: 'open-source' |
| 6 | + data: { |
| 7 | + /** The source file to open */ |
| 8 | + source?: string |
| 9 | + /** The react router route ID, usually discovered via the hook useMatches */ |
| 10 | + routeID?: string |
| 11 | + /** The line number in the source file */ |
| 12 | + line?: number |
| 13 | + /** The column number in the source file */ |
| 14 | + column?: number |
| 15 | + } |
16 | 16 | } |
17 | 17 |
|
18 | 18 | export type EditorConfig = { |
19 | | - /** The name of the editor, used for debugging purposes */ |
20 | | - name: string |
21 | | - /** Callback to open a file in the editor */ |
22 | | - open: (path: string, lineNumber: string | undefined, columnNumber?: string) => Promise<void> |
| 19 | + /** The name of the editor, used for debugging purposes */ |
| 20 | + name: string |
| 21 | + /** Callback to open a file in the editor */ |
| 22 | + open: ( |
| 23 | + path: string, |
| 24 | + lineNumber: string | undefined, |
| 25 | + columnNumber?: string, |
| 26 | + ) => Promise<void> |
23 | 27 | } |
24 | 28 |
|
25 | 29 | export const DEFAULT_EDITOR_CONFIG: EditorConfig = { |
26 | | - name: "VSCode", |
27 | | - open: async (path, lineNumber, columnNumber) => { |
28 | | - const { exec } = await import("node:child_process") |
29 | | - exec(`code -g "${normalizePath(path).replaceAll("$", "\\$")}${lineNumber ? `:${lineNumber}` : ""}${columnNumber ? `:${columnNumber}` : ""}"`) |
30 | | - }, |
| 30 | + name: 'VSCode', |
| 31 | + open: async (path, lineNumber, columnNumber) => { |
| 32 | + const { exec } = await import('node:child_process') |
| 33 | + exec( |
| 34 | + `code -g "${normalizePath(path).replaceAll('$', '\\$')}${lineNumber ? `:${lineNumber}` : ''}${columnNumber ? `:${columnNumber}` : ''}"`, |
| 35 | + ) |
| 36 | + }, |
31 | 37 | } |
32 | 38 |
|
33 | 39 | export const handleOpenSource = async ({ |
34 | | - data, |
35 | | - openInEditor, |
36 | | - appDir, |
| 40 | + data, |
| 41 | + openInEditor, |
| 42 | + appDir, |
37 | 43 | }: { |
38 | | - data: OpenSourceData |
39 | | - appDir: string |
40 | | - openInEditor: EditorConfig["open"] |
| 44 | + data: OpenSourceData |
| 45 | + appDir: string |
| 46 | + openInEditor: EditorConfig['open'] |
41 | 47 | }) => { |
42 | | - const { source, line, routeID } = data.data |
43 | | - const lineNum = line ? `${line}` : undefined |
44 | | - const fs = await import("node:fs") |
45 | | - const path = await import("node:path") |
46 | | - if (source) { |
47 | | - return openInEditor(source, lineNum) |
48 | | - } |
| 48 | + const { source, line, routeID } = data.data |
| 49 | + const lineNum = line ? `${line}` : undefined |
| 50 | + const fs = await import('node:fs') |
| 51 | + const path = await import('node:path') |
| 52 | + if (source) { |
| 53 | + return openInEditor(source, lineNum) |
| 54 | + } |
49 | 55 |
|
50 | | - if (routeID) { |
51 | | - const routePath = path.join(appDir, routeID) |
52 | | - const checkedPath = await checkPath(routePath) |
| 56 | + if (routeID) { |
| 57 | + const routePath = path.join(appDir, routeID) |
| 58 | + const checkedPath = await checkPath(routePath) |
53 | 59 |
|
54 | | - if (!checkedPath) return |
55 | | - const { type, validPath } = checkedPath |
| 60 | + if (!checkedPath) return |
| 61 | + const { type, validPath } = checkedPath |
56 | 62 |
|
57 | | - const reactExtensions = ["tsx", "jsx"] |
58 | | - const allExtensions = ["ts", "js", ...reactExtensions] |
59 | | - const isRoot = routeID === "root" |
60 | | - const findFileByExtension = (prefix: string, filePaths: Array<string>) => { |
61 | | - const file = filePaths.find((file) => allExtensions.some((ext) => file === `${prefix}.${ext}`)) |
62 | | - return file |
63 | | - } |
| 63 | + const reactExtensions = ['tsx', 'jsx'] |
| 64 | + const allExtensions = ['ts', 'js', ...reactExtensions] |
| 65 | + const isRoot = routeID === 'root' |
| 66 | + const findFileByExtension = (prefix: string, filePaths: Array<string>) => { |
| 67 | + const file = filePaths.find((file) => |
| 68 | + allExtensions.some((ext) => file === `${prefix}.${ext}`), |
| 69 | + ) |
| 70 | + return file |
| 71 | + } |
64 | 72 |
|
65 | | - if (isRoot) { |
66 | | - if (!fs.existsSync(appDir)) return |
67 | | - const filesInReactRouterPath = fs.readdirSync(appDir) |
68 | | - const rootFile = findFileByExtension("root", filesInReactRouterPath) |
69 | | - rootFile && openInEditor(path.join(appDir, rootFile), lineNum) |
70 | | - return |
71 | | - } |
| 73 | + if (isRoot) { |
| 74 | + if (!fs.existsSync(appDir)) return |
| 75 | + const filesInReactRouterPath = fs.readdirSync(appDir) |
| 76 | + const rootFile = findFileByExtension('root', filesInReactRouterPath) |
| 77 | + rootFile && openInEditor(path.join(appDir, rootFile), lineNum) |
| 78 | + return |
| 79 | + } |
72 | 80 |
|
73 | | - // If its not the root route, then we find the file or folder in the routes folder |
74 | | - // We know that the route ID is in the form of "routes/contact" or "routes/user.profile" when is not root |
75 | | - // so the ID already contains the "routes" segment, so we just need to find the file or folder in the routes folder |
76 | | - if (type === "directory") { |
77 | | - const filesInFolderRoute = fs.readdirSync(validPath) |
78 | | - const routeFile = findFileByExtension("route", filesInFolderRoute) |
79 | | - routeFile && openInEditor(path.join(appDir, routeID, routeFile), lineNum) |
80 | | - return |
81 | | - } |
82 | | - return openInEditor(validPath, lineNum) |
83 | | - } |
| 81 | + // If its not the root route, then we find the file or folder in the routes folder |
| 82 | + // We know that the route ID is in the form of "routes/contact" or "routes/user.profile" when is not root |
| 83 | + // so the ID already contains the "routes" segment, so we just need to find the file or folder in the routes folder |
| 84 | + if (type === 'directory') { |
| 85 | + const filesInFolderRoute = fs.readdirSync(validPath) |
| 86 | + const routeFile = findFileByExtension('route', filesInFolderRoute) |
| 87 | + routeFile && openInEditor(path.join(appDir, routeID, routeFile), lineNum) |
| 88 | + return |
| 89 | + } |
| 90 | + return openInEditor(validPath, lineNum) |
| 91 | + } |
84 | 92 | } |
0 commit comments