forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
107 lines (88 loc) · 3.15 KB
/
utils.js
File metadata and controls
107 lines (88 loc) · 3.15 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const { pipeline } = require('stream')
const { promisify } = require('util')
const { createGzip, unzip } = require('zlib')
const { readdir, writeFile, readFile, unlink, createReadStream, createWriteStream } = require('mz/fs')
const fixturesPath = './src/integration/__fixtures__'
const recordingFileName = 'recording.har'
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const buildCompressedFilePath = filePath => `${filePath}.gz`
const buildDecompressedFilePath = compressedFilePath => compressedFilePath.replace(/\.gz$/, '')
const findRecordingPath = async (path, isCompressed) => {
const content = await readdir(path, { withFileTypes: true })
if (content.length === 0) {
return
}
if (content[0].isDirectory()) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return findRecordingPath(`${path}/${content[0].name}`, isCompressed)
}
const recording = content.find(
element =>
element.isFile() &&
element.name === (isCompressed ? buildCompressedFilePath(recordingFileName) : recordingFileName)
)
if (recording) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return `${path}/${recording.name}`
}
}
const pipe = promisify(pipeline)
const compress = async (input, output) => {
const gzip = createGzip()
const source = createReadStream(input)
const destination = createWriteStream(output)
await pipe(source, gzip, destination)
}
const compressRecordings = async () => {
const folders = await readdir(fixturesPath)
await Promise.all(
folders.map(async folder => {
const filePath = await findRecordingPath(`${fixturesPath}/${folder}`, false)
if (filePath) {
try {
await compress(filePath, buildCompressedFilePath(filePath))
await unlink(filePath) // delete original recording
} catch (error) {
console.error('An error occurred:', error)
process.exitCode = 1
}
}
})
)
}
const unzipAsPromise = promisify(unzip)
const decompressRecordings = async () => {
const folders = await readdir(fixturesPath)
await Promise.all(
folders.map(async folder => {
const filePath = await findRecordingPath(`${fixturesPath}/${folder}`, true)
if (filePath) {
try {
const content = await readFile(filePath, 'base64')
const result = await unzipAsPromise(Buffer.from(content, 'base64'))
await writeFile(buildDecompressedFilePath(filePath), result)
} catch (error) {
console.error('An error occurred:', error)
process.exitCode = 1
}
}
})
)
}
const deleteRecordings = async () => {
const folders = await readdir(fixturesPath)
await Promise.all(
folders.map(async folder => {
const filePath = await findRecordingPath(`${fixturesPath}/${folder}`, false)
if (filePath) {
try {
await unlink(filePath) // delete original recording
} catch (error) {
console.error('An error occurred:', error)
process.exitCode = 1
}
}
})
)
}
module.exports = { compressRecordings, decompressRecordings, deleteRecordings }