Repository navigation
Expand file tree
/
Copy pathdeleteSourcemaps.js
More file actions
58 lines (48 loc) · 1.53 KB
/
Copy pathdeleteSourcemaps.js
File metadata and controls
58 lines (48 loc) · 1.53 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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
/**
* Recursively walks a directory and returns relative paths of all files
* matching the given extension.
*
* Uses manual recursion instead of `fs.readdirSync({ recursive: true, withFileTypes: true })`
* to avoid a bug in Node 18.17–18.18 where `withFileTypes` returns incorrect `parentPath` values
* when combined with `recursive: true`.
*
* @param {string} rootDir - The root directory to start walking from.
* @param {string} extension - The file extension to match (e.g. '.map').
* @returns {string[]} Relative file paths from rootDir.
*/
function walkDirectory(rootDir, extension) {
const results = [];
function walk(dir) {
let entries;
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(fullPath);
} else if (entry.name.endsWith(extension)) {
results.push(path.relative(rootDir, fullPath));
}
}
}
walk(rootDir);
return results;
}
function deleteSourcemaps(buildPath) {
console.info(`[sentry] Deleting sourcemaps from ${buildPath}`);
// Delete all .map files in the build folder and its subfolders
const mapFiles = walkDirectory(buildPath, '.map');
mapFiles.forEach(file => {
fs.unlinkSync(path.join(buildPath, file));
console.info(`[sentry] Deleted ${file}`);
});
}
module.exports = {
deleteSourcemaps,
};