MCPcopy Create free account
hub / github.com/TanStack/router / createHmrFileEditor

Function createHmrFileEditor

e2e/e2e-utils/src/hmrFileEditor.ts:14–127  ·  view source on GitHub ↗
(
  options: HmrFileEditorOptions<TFileKey>,
)

Source from the content-addressed store, hash-verified

12}
13
14export function createHmrFileEditor<TFileKey extends string>(
15 options: HmrFileEditorOptions<TFileKey>,
16) {
17 const files = Object.fromEntries(
18 Object.entries(options.files).map(([key, filePath]) => [
19 key,
20 options.rootDir && !path.isAbsolute(filePath as string)
21 ? path.join(options.rootDir, filePath as string)
22 : (filePath as string),
23 ]),
24 ) as Record<TFileKey, string>
25 const originalContents: Partial<Record<TFileKey, string>> = {}
26 const pendingRestoreKeys = new Set<TFileKey>()
27 const normalizeSource =
28 options.normalizeSource ?? ((_fileKey: TFileKey, source: string) => source)
29
30 async function captureOriginals() {
31 for (const [key, filePath] of Object.entries(files) as Array<
32 [TFileKey, string]
33 >) {
34 const current = await readFile(filePath, 'utf8')
35 const normalized = normalizeSource(key, current)
36
37 if (normalized !== current) {
38 await writeFile(filePath, normalized)
39 pendingRestoreKeys.add(key)
40 }
41
42 originalContents[key] = normalized
43 }
44 }
45
46 const capturePromise = captureOriginals()
47
48 async function restoreFiles(forceFileKeys: Iterable<TFileKey> = []) {
49 const forceRestoreKeys = new Set(forceFileKeys)
50 const restoredFileKeys: Array<TFileKey> = []
51
52 for (const [key, filePath] of Object.entries(files) as Array<
53 [TFileKey, string]
54 >) {
55 const content = originalContents[key]
56 if (content === undefined) continue
57
58 const current = await readFile(filePath, 'utf8')
59
60 if (current !== content || forceRestoreKeys.has(key)) {
61 await writeFile(filePath, content)
62 restoredFileKeys.push(key)
63 }
64 }
65
66 return restoredFileKeys
67 }
68
69 async function replaceText(fileKey: TFileKey, from: string, to: string) {
70 const filePath = files[fileKey]
71 const source = await readFile(filePath, 'utf8')

Callers 2

app.spec.tsFile · 0.85
hydration.spec.tsFile · 0.85

Calls 1

captureOriginalsFunction · 0.85

Tested by

no test coverage detected