forked from franzenzenhofer/lighthouse-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanRuns.js
48 lines (39 loc) · 1.36 KB
/
cleanRuns.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
// cleanRuns.js
import { readPastRunsFile, writePastRunsFile, generateIndexHTML } from './index-utils.mjs';
import fs from 'fs/promises';
import path from 'path';
const pastRunsFile = 'pastRuns.json';
const indexFile = 'index.html';
const resultsDirectory = 'results';
async function deleteFilesAndDirectories(directory) {
const files = await fs.readdir(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stat = await fs.stat(filePath);
if (stat.isDirectory()) {
await deleteFilesAndDirectories(filePath);
const filesInDir = await fs.readdir(filePath);
if (filesInDir.length === 0) {
await fs.rmdir(filePath);
}
} else {
const ext = path.extname(filePath);
if (['.json', '.html', '.csv'].includes(ext)) {
await fs.unlink(filePath);
}
}
}
}
async function cleanRuns() {
// Empty the past runs in the JSON file
await writePastRunsFile(pastRunsFile, []);
// Generate an empty index.html file
const emptyIndexHTML = generateIndexHTML([]);
await fs.writeFile(indexFile, emptyIndexHTML);
// Delete JSON, HTML, and CSV files in the results directory and remove empty subdirectories
await deleteFilesAndDirectories(resultsDirectory);
console.log('Past runs cleaned.');
}
cleanRuns().catch(error => {
console.error('Unexpected error:', error);
});