-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
182 lines (153 loc) · 6.37 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
const fs = require('fs');
const { SourceMapConsumer } = require('source-map');
const path = require('path');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const prettier = require('prettier'); // Ensure prettier is installed
const argv = yargs(hideBin(process.argv))
.usage('Usage: $0 [options]')
.option('input', {
description: 'The input path to the minified JavaScript file or a directory containing multiple files',
alias: 'i',
type: 'string',
demandOption: true
})
.option('maxDepth', {
description: 'The maximum depth of the directory tree to print',
type: 'number',
default: 3
})
.option('maxFiles', {
description: 'The maximum number of files to display per directory',
type: 'number',
default: 5
})
.help()
.alias('help', 'h')
.argv;
// Array to store the recovered file paths
const recoveredFiles = [];
const handleFile = async (minifiedFilePath) => {
// Check if the Source Map exists
const sourceMapPath = minifiedFilePath + '.map';
if (!fs.existsSync(sourceMapPath)) {
console.error(`No source map found at ${sourceMapPath}`);
return;
}
// Read the minified file and the Source Map
const minifiedCode = fs.readFileSync(minifiedFilePath, 'utf8');
const rawSourceMap = fs.readFileSync(sourceMapPath);
const rawSourceMapJson = JSON.parse(rawSourceMap);
await SourceMapConsumer.with(rawSourceMapJson, null, (sourceMapConsumer) => {
const sources = sourceMapConsumer.sources;
const sourceContents = sources.reduce((contents, source) => {
contents[source] = sourceMapConsumer.sourceContentFor(source, true);
return contents;
}, {});
if (Object.values(sourceContents).some(content => content !== null)) {
// If sourcesContent exists, write it to the file
for (const source in sourceContents) {
if (sourceContents[source] !== null) {
const originalFilePath = path.join(path.dirname(minifiedFilePath), source + '');
fs.mkdirSync(path.dirname(originalFilePath), { recursive: true });
fs.writeFileSync(originalFilePath, sourceContents[source]);
// Add to the list of recovered files
recoveredFiles.push(originalFilePath);
}
}
} else {
// If sourcesContent doesn't exist, reconstruct the source code
const lines = minifiedCode.split('\n');
let reconstructedSource = '';
lines.forEach((line, lineIndex) => {
const lineNum = lineIndex + 1;
const columnCount = line.length;
for (let column = 0; column < columnCount; column++) {
const pos = { line: lineNum, column: column };
const originalPosition = sourceMapConsumer.originalPositionFor(pos);
if (originalPosition.source === null) continue;
if (originalPosition.name) {
reconstructedSource += originalPosition.name;
} else {
reconstructedSource += minifiedCode.charAt(column);
}
}
reconstructedSource += '\n';
});
// prettify the code
try {
reconstructedSource = prettier.format(reconstructedSource, { semi: false, parser: "babel" });
} catch (error) {
console.error("An error occurred while prettifying the code:", error);
}
const originalFilePath = path.join(path.dirname(minifiedFilePath), path.basename(minifiedFilePath, '.js') + '-recovered.js');
fs.mkdirSync(path.dirname(originalFilePath), { recursive: true });
fs.writeFileSync(originalFilePath, reconstructedSource);
// Add to the list of recovered files
recoveredFiles.push(originalFilePath);
}
});
};
const handlePath = async (inputPath, currentDepth = 0) => {
const files = fs.readdirSync(inputPath);
// Keep track of how many files we've printed for each folder
let fileCount = 0;
for (const file of files) {
const absolutePath = path.join(inputPath, file);
if (fs.statSync(absolutePath).isDirectory()) {
// Create the directory even if we're not printing it
fs.mkdirSync(absolutePath, { recursive: true });
// Don't process directories if we've hit max depth
if (currentDepth < argv.maxDepth) {
await handlePath(absolutePath, currentDepth + 1);
}
} else {
// Process all files, even if we're not printing them in the tree
await handleFile(absolutePath);
// Only print files if we're not exceeding maxFiles
if (fileCount < argv.maxFiles) {
fileCount++;
}
}
}
};
const printRecoveredFilesTree = (files) => {
const grouped = files.reduce((acc, filePath) => {
const parts = filePath.split(path.sep);
let currentLevel = acc;
parts.forEach((part, index) => {
if (!currentLevel[part]) {
currentLevel[part] = index === parts.length - 1 ? null : {};
}
currentLevel = currentLevel[part];
});
return acc;
}, {});
const printTree = (node, indent = '', depth = 0) => {
if (depth > argv.maxDepth) return;
Object.keys(node).forEach((key, index) => {
const isLast = index === Object.keys(node).length - 1;
if (node[key] === null) {
console.log(`${indent}|-- ${key}`);
} else {
console.log(`${indent}${isLast ? '`-- ' : '|-- '}${key}/`);
printTree(node[key], indent + (isLast ? ' ' : '| '), depth + 1);
}
});
};
printTree(grouped);
};
const main = async () => {
// Handle directory or file based on input type
if (fs.statSync(argv.input).isDirectory()) {
await handlePath(argv.input);
printRecoveredFilesTree(recoveredFiles);
} else {
await handleFile(argv.input);
printRecoveredFilesTree(recoveredFiles);
}
};
main().catch(error => {
console.error("An error occurred:", error);
});