-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapJs.go
More file actions
79 lines (68 loc) · 1.88 KB
/
MapJs.go
File metadata and controls
79 lines (68 loc) · 1.88 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// SourceMap represents the structure of a source map file
type SourceMap struct {
Version int `json:"version"`
File string `json:"file"`
Sources []string `json:"sources"`
SourcesContent []string `json:"sourcesContent"`
Mappings string `json:"mappings"`
}
func extractJSFromSourceMap(mapData []byte) (string, error) {
// Parse the source map file
var sourceMap SourceMap
err := json.Unmarshal(mapData, &sourceMap)
if err != nil {
return "", fmt.Errorf("error parsing source map: %v", err)
}
// Check if we have sourcesContent (the original JS content)
if len(sourceMap.SourcesContent) == 0 {
return "", fmt.Errorf("no sourcesContent found in the source map")
}
// Concatenate all JavaScript sources into one string
var fullJS string
for _, content := range sourceMap.SourcesContent {
fullJS += content + "\n"
}
return fullJS, nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: extract_js <input_map_file> [output_js_file]")
return
}
inputFile := os.Args[1]
outputFile := ""
if len(os.Args) >= 3 {
outputFile = os.Args[2]
}
// Read the input source map file
mapData, err := ioutil.ReadFile(inputFile)
if err != nil {
fmt.Printf("Error reading source map file: %v\n", err)
return
}
// Extract the JavaScript code from the source map
jsCode, err := extractJSFromSourceMap(mapData)
if err != nil {
fmt.Printf("Error extracting JS: %v\n", err)
return
}
// Check if an output file was provided, otherwise print to stdout
if outputFile != "" {
err = ioutil.WriteFile(outputFile, []byte(jsCode), 0644)
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
} else {
fmt.Printf("JavaScript code extracted and written to %s\n", outputFile)
}
} else {
// Print to stdout if no output file is provided
fmt.Println(jsCode)
}
}