|
| 1 | +import { opendir, readFile, writeFile } from 'fs/promises'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { addDebugIdToSource, stringToUUID, DEFAULT_EXTENSIONS, addDebugIdToSourcemap } from './common'; |
| 4 | + |
| 5 | +async function walk(path: string, extensions = DEFAULT_EXTENSIONS): Promise<string[]> { |
| 6 | + const extensionsAndMaps = [...extensions, ...extensions.map((e) => `${e}.map`)]; |
| 7 | + let files = []; |
| 8 | + |
| 9 | + for await (const file of await opendir(path)) { |
| 10 | + if (file.isDirectory()) { |
| 11 | + files.push(...(await walk(join(path, file.name)))); |
| 12 | + } else { |
| 13 | + if (extensionsAndMaps.some((ext) => file.name.endsWith(ext))) { |
| 14 | + files.push(join(path, file.name)); |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + return files; |
| 20 | +} |
| 21 | + |
| 22 | +async function groupSourceAndMapFiles(files: string[]): Promise<Array<[string, string | undefined]>> { |
| 23 | + const sourceFiles = files.filter((f) => !f.endsWith('.map')); |
| 24 | + const mapFiles = files.filter((f) => f.endsWith('.map')); |
| 25 | + |
| 26 | + return sourceFiles.map((sourceFile) => { |
| 27 | + const mapFile = mapFiles.find((f) => f === `${sourceFile}.map`); |
| 28 | + return [sourceFile, mapFile]; |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +(async () => { |
| 33 | + console.time('Adding debug IDs'); |
| 34 | + if (!process.argv[2]) { |
| 35 | + console.error('Usage: debugids <dir>'); |
| 36 | + process.exit(1); |
| 37 | + } |
| 38 | + |
| 39 | + const results = await walk(process.argv[2]); |
| 40 | + const groupedResults = await groupSourceAndMapFiles(results); |
| 41 | + let modifiedFiles: Array<{ source: string; map: string; debugId: string }> = []; |
| 42 | + let missingMaps = new Set<string>(); |
| 43 | + let addedDebugIds = 0; |
| 44 | + |
| 45 | + const promises = groupedResults.map(async ([source, map]) => { |
| 46 | + if (!map) { |
| 47 | + missingMaps.add(source); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const [sourceContent, mapContent] = await Promise.all([readFile(source, 'utf-8'), readFile(map, 'utf-8')]); |
| 52 | + |
| 53 | + const debugId = stringToUUID(sourceContent); |
| 54 | + const newSourceContent = addDebugIdToSource(sourceContent, debugId); |
| 55 | + const newMapContent = addDebugIdToSourcemap(mapContent, debugId); |
| 56 | + |
| 57 | + modifiedFiles.push({ source, map, debugId }); |
| 58 | + |
| 59 | + await Promise.all([writeFile(source, newSourceContent), writeFile(map, newMapContent)]); |
| 60 | + addedDebugIds++; |
| 61 | + }); |
| 62 | + |
| 63 | + await Promise.all(promises); |
| 64 | + |
| 65 | + console.timeEnd('Adding debug IDs'); |
| 66 | + console.log(`${addedDebugIds} source files modified`); |
| 67 | + console.table(modifiedFiles.sort((a, b) => a.source.localeCompare(b.source))); |
| 68 | + |
| 69 | + if (missingMaps.size > 0) { |
| 70 | + console.log(`The following files were missing sourcemaps: ${[...missingMaps].join(', ')}`); |
| 71 | + } |
| 72 | +})(); |
0 commit comments