-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (47 loc) · 1.62 KB
/
index.js
File metadata and controls
58 lines (47 loc) · 1.62 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
52
53
54
55
56
57
58
const path = require('path')
const fs = require('fs').promises
async function spit(filepath, data) {
const dirpath = path.dirname(filepath)
await fs.mkdir(dirpath, {recursive: true})
return fs.writeFile(filepath, data)
}
/**
Write sourceContent to <dirpath>/<sourceRoot>/<source>.
Returns (a Promise of) the written filepath (on success).
*/
async function dumpSource(source, sourceContent, sourceRoot, dirpath) {
const safeSource = source.replace(/[\x00-\x1f\x80-\x9f\?<>\\:\*\|"]/g, '')
const sourcePath = path.normalize(safeSource)
const sourceFilepath = path.join(dirpath, sourceRoot, sourcePath)
await spit(sourceFilepath, sourceContent)
return sourceFilepath
}
/**
Traverse a JavaScript Source Map, writing output into <dirpath>.
Returns (a Promise of) a list of strings of written filepaths.
*/
function dumpSourceMap({sources, sourcesContent, sourceRoot}, dirpath) {
const sourcePromises = sources.map((source, i) => {
const sourceContent = sourcesContent[i]
return dumpSource(source, sourceContent, sourceRoot, dirpath)
})
return Promise.all(sourcePromises)
}
/**
Read a JavaScript Source Map from <filepath>, overriding the "sourceRoot" field
with <sourceRoot> if provided, and write output into <dirpath>.
Returns (a Promise of) a list of strings of written filepaths.
*/
async function dumpFile(filepath, dirpath, sourceRoot) {
const sourceMapData = await fs.readFile(filepath)
const sourceMap = JSON.parse(sourceMapData)
if (sourceRoot) {
Object.assign(sourceMap, {sourceRoot})
}
return dumpSourceMap(sourceMap, dirpath)
}
module.exports = {
dumpSource,
dumpSourceMap,
dumpFile,
}