forked from OFlavioBliss/GrafanaFE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeReport.js
72 lines (61 loc) · 2.05 KB
/
mergeReport.js
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
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')
const outputDir = 'cypress/test-results-merged'
const baseName = 'testOutput'
const extension = 'json'
const maxFiles = 10
//Ensure the output directory exists
try {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}
} catch (err) {
console.error(`Error creating output directory: ${err.message}`)
process.exit(1)
}
function getNextFilename(basePath, baseName, extension) {
const files = fs.readdirSync(basePath)
const pattern = new RegExp(`${baseName}(\\d*)\\.${extension}`)
let maxIndex = 0
files.forEach(file => {
const match = file.match(pattern)
if (match) {
const index = parseInt(match[1], 10) || 0
if (index > maxIndex) {
maxIndex = index
}
}
})
return `${baseName}${String(maxIndex + 1).padStart(3, '0')}.${extension}`
}
function maintainMaxFiles(basePath, maxFiles) {
try {
const files = fs.readdirSync(basePath)
.filter(file => file.endsWith(`.${extension}`))
.map(file => ({
name: file,
time: fs.statSync(path.join(basePath, file)).mtime.getTime()
}))
.sort((a, b) => a.time - b.time) // Sort by modification time (oldest first)
while (files.length >= maxFiles) {
const oldestFile = files.shift(); // Remove the oldest file from the array
fs.unlinkSync(path.join(basePath, oldestFile.name)) // Delete the oldest file
console.log(`Deleted old file: ${oldestFile.name}`)
}
} catch (err) {
console.error(`Error maintaining max files: ${err.message}`)
process.exit(1)
}
}
//Maintain the max number of files
maintainMaxFiles(outputDir, maxFiles)
//Generate the next filename
const nextFilename = getNextFilename(outputDir, baseName, extension)
//Command to merge the files
const command = `npx mochawesome-merge cypress/test-results/jsonResults/*.json > ${path.join(outputDir, nextFilename)}`
exec(command, (err, stdout, stderr) => {
if (err) {
console.error(`Error executing command: ${err}`)
}
})