|
2 | 2 | const fs = require('fs'); |
3 | 3 | const path = require('path'); |
4 | 4 |
|
5 | | -const { globSync } = require('glob'); |
| 5 | +/** |
| 6 | + * Recursively walks a directory and returns relative paths of all files |
| 7 | + * matching the given extension. |
| 8 | + * |
| 9 | + * Uses manual recursion instead of `fs.readdirSync({ recursive: true, withFileTypes: true })` |
| 10 | + * to avoid a bug in Node 18.17–18.18 where `withFileTypes` returns incorrect `parentPath` values |
| 11 | + * when combined with `recursive: true`. |
| 12 | + * |
| 13 | + * @param {string} rootDir - The root directory to start walking from. |
| 14 | + * @param {string} extension - The file extension to match (e.g. '.map'). |
| 15 | + * @returns {string[]} Relative file paths from rootDir. |
| 16 | + */ |
| 17 | +function walkDirectory(rootDir, extension) { |
| 18 | + const results = []; |
| 19 | + |
| 20 | + function walk(dir) { |
| 21 | + let entries; |
| 22 | + try { |
| 23 | + entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 24 | + } catch { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + for (const entry of entries) { |
| 29 | + const fullPath = path.join(dir, entry.name); |
| 30 | + |
| 31 | + if (entry.isDirectory()) { |
| 32 | + walk(fullPath); |
| 33 | + } else if (entry.name.endsWith(extension)) { |
| 34 | + results.push(path.relative(rootDir, fullPath)); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + walk(rootDir); |
| 40 | + return results; |
| 41 | +} |
6 | 42 |
|
7 | 43 | function deleteSourcemaps(buildPath) { |
8 | 44 | console.info(`[sentry] Deleting sourcemaps from ${buildPath}`); |
9 | 45 |
|
10 | 46 | // Delete all .map files in the build folder and its subfolders |
11 | | - const mapFiles = globSync('**/*.map', { cwd: buildPath }); |
| 47 | + const mapFiles = walkDirectory(buildPath, '.map'); |
12 | 48 |
|
13 | 49 | mapFiles.forEach(file => { |
14 | 50 | fs.unlinkSync(path.join(buildPath, file)); |
|
0 commit comments