forked from ChinaSiro/claude-code-sourcemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-sources.js
More file actions
51 lines (37 loc) · 1.57 KB
/
extract-sources.js
File metadata and controls
51 lines (37 loc) · 1.57 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
const { SourceMapConsumer } = require('source-map');
const fs = require('fs');
const path = require('path');
async function extractSources() {
const mapFile = path.join(__dirname, 'package/cli.js.map');
console.log('Reading source map...');
const rawMap = JSON.parse(fs.readFileSync(mapFile, 'utf8'));
console.log(`Sources count: ${rawMap.sources.length}`);
console.log('Sample sources:', rawMap.sources.slice(0, 10));
const outDir = path.join(__dirname, 'restored-src');
let written = 0, skipped = 0;
for (let i = 0; i < rawMap.sources.length; i++) {
const sourcePath = rawMap.sources[i];
const content = rawMap.sourcesContent && rawMap.sourcesContent[i];
if (!content) { skipped++; continue; }
// Sanitize path
let relPath = sourcePath
.replace(/^.*node_modules\//, 'node_modules/')
.replace(/^webpack:\/\/\//, '')
.replace(/^webpack:\/\//, '')
.replace(/^\/?\.\.\//, '')
.replace(/\?.*$/, '');
if (!relPath || relPath === 'webpack/bootstrap') {
relPath = `__webpack__/source_${i}.js`;
}
// Remove leading slashes and dangerous path components
relPath = relPath.replace(/^\/+/, '').replace(/\.\.\//g, '_dotdot_/');
const fullPath = path.join(outDir, relPath);
const dir = path.dirname(fullPath);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(fullPath, content, 'utf8');
written++;
if (written % 500 === 0) console.log(`Written ${written} files...`);
}
console.log(`Done. Written: ${written}, Skipped (no content): ${skipped}`);
}
extractSources().catch(console.error);