MCPcopy
hub / github.com/vitejs/vite / editFile

Function editFile

playground/test-utils.ts:181–205  ·  playground/test-utils.ts::editFile
(
  filename: string,
  encodingOrReplacer: BufferEncoding | null | ((content: string) => string),
  maybeReplacer?: ((content: Buffer) => Buffer) | ((content: string) => string),
)

Source from the content-addressed store, hash-verified

179 replacer: ((content: Buffer) => Buffer) | ((content: string) => string),
180): void
181export function editFile(
182 filename: string,
183 encodingOrReplacer: BufferEncoding | null | ((content: string) => string),
184 maybeReplacer?: ((content: Buffer) => Buffer) | ((content: string) => string),
185): void {
186 filename = path.resolve(testDir, filename)
187 const [encoding, replacer] = maybeReplacer
188 ? [encodingOrReplacer as BufferEncoding | null, maybeReplacer]
189 : [class="st">'utf-8' as const, encodingOrReplacer as (content: string) => string]
190 const content: string | Buffer = fs.readFileSync(filename, encoding)
191 const modified = (replacer as (content: string | Buffer) => string | Buffer)(
192 content,
193 )
194 if (Buffer.byteLength(modified) === Buffer.byteLength(content)) {
195 const e = new Error(
196 `editFile(class="st">"${filename}") did not change the file size. The polling ` +
197 `watcher used in tests may miss same-length edits and cause flaky ` +
198 `failures; change the edit so the file's byte length changes. See ` +
199 `https:class="cm">//github.com/vitejs/vite/blob/main/CONTRIBUTING.md#test-env-and-helpers`,
200 )
201 Error.captureStackTrace(e, editFile)
202 throw e
203 }
204 fs.writeFileSync(filename, modified)
205}
206
207export function addFile(filename: string, content: string): void {
208 const resolvedFilename = path.resolve(testDir, filename)

Callers 15

ssr-html.spec.tsFile · 0.50
hmr-root.spec.tsFile · 0.50
assets.spec.tsFile · 0.50
ssr-deps.spec.tsFile · 0.50
cli.spec.tsFile · 0.50
ssr.spec.tsFile · 0.50
hmr-ssr.spec.tsFile · 0.50
testStarExportsFunction · 0.50
tailwind.spec.tsFile · 0.50

Calls 1

resolveMethod · 0.65

Tested by 5

testStarExportsFunction · 0.40
sassTestFunction · 0.40
sassModuleTestsFunction · 0.40
testStarExportsFunction · 0.40
testsFunction · 0.40