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