forked from alexkuz/restore-source-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (71 loc) · 2.29 KB
/
index.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
import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
import { SourceMapConsumer } from 'source-map';
import { Command } from 'commander';
const WEBPACK_PREFIX = 'webpack:///';
const WEBPACK_FOOTER = '/** WEBPACK FOOTER **';
const program = new Command('restore-source-tree')
.version('0.1.1')
.usage('[options] <file>')
.description('Restores file structure from source map')
.option('-o, --out-dir [dir]', 'Output directory (\'output\' by default)', 'output')
.option('-n, --include-node-modules', 'Include source files in node_modules')
.parse(process.argv);
if (program.args.length === 0) {
program.outputHelp();
process.exit(1);
}
const readJson = filename => {
try {
return JSON.parse(fs.readFileSync(filename, 'utf8'));
} catch(e) {
console.error(`Parsing file '${filename}' failed: ${e.message}`);
process.exit(1);
}
}
const getSourceList = smc => {
let sources = smc.sources
.filter(src => src.startsWith(WEBPACK_PREFIX))
.map(src => [src.replace(WEBPACK_PREFIX, ''), src])
.filter(([filePath]) => !filePath.startsWith('(webpack)'));
if (!program.includeNodeModules) {
sources = sources.filter(([filePath]) => !filePath.startsWith('~/'));
}
return sources;
}
const trimFooter = str => str.substr(0, str.indexOf(WEBPACK_FOOTER)).trimRight() + '\n';
const saveSourceContent = (smc, filePath, src) => {
const content = trimFooter(smc.sourceContentFor(src));
const outPath = path.join(program.outDir, filePath);
const dir = path.dirname(outPath);
if (content.length < 2) return;
mkdirp(dir, err => {
if (err) {
console.error('Failed creating directory', dir);
process.exit(1);
} else {
fs.writeFile(outPath, content, err => {
if (err) {
console.error('Failed writing file', outPath);
process.exit(1);
}
});
}
})
}
function processFile(filename) {
const json = readJson(filename);
const smc = new SourceMapConsumer(json);
const sources = getSourceList(smc);
sources.forEach(([filePath, src]) => saveSourceContent(smc, filePath, src));
console.log(`Processed ${sources.length} files`);
}
const filename = program.args[0];
fs.access(filename, err => {
if (err) {
console.error(err.message);
process.exit(1);
}
processFile(filename);
});