-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathfs.ts
More file actions
60 lines (55 loc) · 2.01 KB
/
Copy pathfs.ts
File metadata and controls
60 lines (55 loc) · 2.01 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
import type { BrowserCommands } from 'vitest/browser'
import type { BrowserCommand, TestProject } from 'vitest/node'
import fs, { promises as fsp } from 'node:fs'
import { basename, dirname, resolve } from 'node:path'
import mime from 'mime/lite'
import { isFileServingAllowed } from 'vitest/node'
function assertFileAccess(path: string, project: TestProject) {
if (
!isFileServingAllowed(path, project.vite)
&& !isFileServingAllowed(path, project.vitest.vite)
) {
throw new Error(
`Access denied to "${path}". See Vite config documentation for "server.fs": https://vitejs.dev/config/server-options.html#server-fs-strict.`,
)
}
}
export const readFile: BrowserCommand<
Parameters<BrowserCommands['readFile']>
> = async ({ project }, path, options = {}) => {
const filepath = resolve(project.config.root, path)
assertFileAccess(filepath, project)
// never return a Buffer
if (typeof options === 'object' && !options.encoding) {
options.encoding = 'utf-8'
}
return fsp.readFile(filepath, options)
}
export const writeFile: BrowserCommand<
Parameters<BrowserCommands['writeFile']>
> = async ({ project }, path, data, options) => {
const filepath = resolve(project.config.root, path)
assertFileAccess(filepath, project)
const dir = dirname(filepath)
if (!fs.existsSync(dir)) {
await fsp.mkdir(dir, { recursive: true })
}
await fsp.writeFile(filepath, data, options)
}
export const removeFile: BrowserCommand<
Parameters<BrowserCommands['removeFile']>
> = async ({ project }, path) => {
const filepath = resolve(project.config.root, path)
assertFileAccess(filepath, project)
await fsp.rm(filepath)
}
export const _fileInfo: BrowserCommand<[path: string, encoding: BufferEncoding]> = async ({ project }, path, encoding) => {
const filepath = resolve(project.config.root, path)
assertFileAccess(filepath, project)
const content = await fsp.readFile(filepath, encoding || 'base64')
return {
content,
basename: basename(filepath),
mime: mime.getType(filepath),
}
}