Skip to content

Commit 8e30147

Browse files
committed
fix #1
1 parent 59abe62 commit 8e30147

9 files changed

Lines changed: 55 additions & 41 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sourcerer is a cli tool to get a website's source code from its source maps writ
55
# usage
66

77
```
8-
sourcerer [name] ...[urls]
8+
sourcerer [dir] [...urls]
99
```
1010

1111
the url can either be html, css, javascript, or a source map json

css.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
)
99

1010
func fromCss(ctx Context, res *http.Response) {
11-
info(ctx.Depth, "Processing CSS...")
11+
Info(ctx.Depth, "Processing CSS...")
1212

1313
var sourceMapUrl url.URL
1414

1515
str, err := ioutil.ReadAll(res.Body)
1616
if err != nil {
17-
error(ctx.Depth, "Failed to read css response body:", err)
17+
Error(ctx.Depth, "Failed to read css response body:", err)
1818
return
1919
}
2020

@@ -32,7 +32,7 @@ func fromCss(ctx Context, res *http.Response) {
3232

3333
ref, err := url.Parse(after)
3434
if err != nil {
35-
error(ctx.Depth, "Failed to parse CSS source map url:", err)
35+
Error(ctx.Depth, "Failed to parse CSS source map url:", err)
3636
return
3737
}
3838

@@ -49,4 +49,4 @@ func fromCss(ctx Context, res *http.Response) {
4949
}
5050

5151
fromSourceMap(sourceMapCtx, sourceMap)
52-
}
52+
}

fetch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ func fetch(ctx Context) (*http.Response, bool) {
4040
ctx.Cache[ctx.Url.String()] = struct{}{}
4141
}
4242

43-
info(ctx.Depth, "Fetching URL:", ctx.Url.String())
43+
Info(ctx.Depth, "Fetching URL:", ctx.Url.String())
4444
res, err := http.Get(ctx.Url.String())
4545
if err != nil {
46-
error(ctx.Depth, "Failed to fetch URL:", err)
46+
Error(ctx.Depth, "Failed to fetch URL:", err)
4747
return nil, false
4848
}
4949

5050
if res.StatusCode < 200 || res.StatusCode >= 300 {
51-
warn(ctx.Depth, "URL responded with status:", res.Status)
51+
Warn(ctx.Depth, "URL responded with status:", res.Status)
5252
return nil, false
5353
}
5454

55-
success(ctx.Depth, "URL responded with status:", res.Status)
55+
Success(ctx.Depth, "URL responded with status:", res.Status)
5656
return res, true
5757
}

html.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
)
99

1010
func fromHtml(ctx Context, res *http.Response) {
11-
info(ctx.Depth, "Processing HTML...")
11+
Info(ctx.Depth, "Processing HTML...")
1212

1313
parsedHtml, err := html.Parse(res.Body)
1414
if err != nil {
15-
error(ctx.Depth, "Failed to parse HTML:", err)
15+
Error(ctx.Depth, "Failed to parse HTML:", err)
1616
return
1717
}
1818

@@ -73,7 +73,7 @@ func fromHtml(ctx Context, res *http.Response) {
7373
if isScript && script != "" {
7474
ref, err := ctx.Url.Parse(script)
7575
if err != nil {
76-
error(ctx.Depth, "Failed to parse JavaScript url:", err)
76+
Error(ctx.Depth, "Failed to parse JavaScript url:", err)
7777
} else {
7878
jsCtx := ctx
7979
jsCtx.Url = *ctx.Url.ResolveReference(ref)
@@ -89,7 +89,7 @@ func fromHtml(ctx Context, res *http.Response) {
8989
if isStyle && style != "" {
9090
ref, err := url.Parse(style)
9191
if err != nil {
92-
error(ctx.Depth, "Failed to parse CSS url:", err)
92+
Error(ctx.Depth, "Failed to parse CSS url:", err)
9393
} else {
9494
cssCtx := ctx
9595
cssCtx.Url = *ctx.Url.ResolveReference(ref)
@@ -108,4 +108,4 @@ func fromHtml(ctx Context, res *http.Response) {
108108
}
109109

110110
walk(parsedHtml)
111-
}
111+
}

js.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
)
99

1010
func fromJs(ctx Context, res *http.Response) {
11-
info(ctx.Depth, "Processing JavaScript...")
11+
Info(ctx.Depth, "Processing JavaScript...")
1212

1313
var sourceMapUrl url.URL
1414

1515
str, err := ioutil.ReadAll(res.Body)
1616
if err != nil {
17-
error(ctx.Depth, "Failed to read body:", err)
17+
Error(ctx.Depth, "Failed to read body:", err)
1818
return
1919
}
2020

@@ -27,7 +27,7 @@ func fromJs(ctx Context, res *http.Response) {
2727
after := string(str[startIndex+len("//# sourceMappingURL="):])
2828
ref, err := url.Parse(after)
2929
if err != nil {
30-
error(ctx.Depth, "Failed to parse JavaScript source map url:", err)
30+
Error(ctx.Depth, "Failed to parse JavaScript source map url:", err)
3131
return
3232
}
3333

@@ -44,4 +44,4 @@ func fromJs(ctx Context, res *http.Response) {
4444
}
4545

4646
fromSourceMap(sourceMapCtx, sourceMap)
47-
}
47+
}

log.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func getIndent(depth int) string {
1111
return strings.Repeat(" ", depth)
1212
}
1313

14-
func info(depth int, args ...interface{}) {
14+
func Info(depth int, args ...interface{}) {
1515
fmt.Print(getIndent(depth))
1616
fmt.Print(chalk.BlueLight().Bold())
1717
fmt.Print("[INFO]")
@@ -20,7 +20,7 @@ func info(depth int, args ...interface{}) {
2020
fmt.Println(args...)
2121
}
2222

23-
func warn(depth int, args ...interface{}) {
23+
func Warn(depth int, args ...interface{}) {
2424
fmt.Print(getIndent(depth))
2525
fmt.Print(chalk.YellowLight().Bold())
2626
fmt.Print("[WARN]")
@@ -29,7 +29,7 @@ func warn(depth int, args ...interface{}) {
2929
fmt.Println(args...)
3030
}
3131

32-
func success(depth int, args ...interface{}) {
32+
func Success(depth int, args ...interface{}) {
3333
fmt.Print(getIndent(depth))
3434
fmt.Print(chalk.GreenLight().Bold())
3535
fmt.Print("[SUCCESS]")
@@ -38,11 +38,11 @@ func success(depth int, args ...interface{}) {
3838
fmt.Println(args...)
3939
}
4040

41-
func error(depth int, args ...interface{}) {
41+
func Error(depth int, args ...interface{}) {
4242
fmt.Print(getIndent(depth))
4343
fmt.Print(chalk.RedLight().Bold())
4444
fmt.Print("[ERROR]")
4545
fmt.Print(chalk.Reset())
4646
fmt.Print(" ")
4747
fmt.Println(args...)
48-
}
48+
}

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func main() {
2424

2525
cwd, err := os.Getwd()
2626
if err != nil {
27-
error(0, "Failed to get current path", err)
27+
Error(0, "Failed to get current path", err)
2828
return
2929
}
3030

3131
name := args[0]
3232
dir := filepath.Join(cwd, name)
3333

3434
if err := os.Mkdir(dir, os.ModePerm); err != nil {
35-
error(0, "Failed to create output directory", err)
35+
Error(0, "Failed to create output directory", err)
3636
return
3737
}
3838

@@ -54,4 +54,4 @@ func main() {
5454
fromUrl(ctx)
5555
}
5656
}
57-
}
57+
}

sourceMap.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,34 @@ type SourceMap struct {
1515
SourcesContent []string `json:"sourcesContent"`
1616
}
1717

18+
func filenamifyOpts(options *filenamify.Options) {
19+
options.Replacement = "_"
20+
}
21+
22+
func pathify(path string) (string, error) {
23+
var result string
24+
for _, part := range strings.Split(filepath.Clean(path), string(os.PathSeparator)) {
25+
filename, err := filenamify.FilenamifyV2(part, filenamifyOpts)
26+
if err != nil {
27+
return "", err
28+
}
29+
30+
result = filepath.Join(result, filename)
31+
}
32+
33+
return result, nil
34+
}
35+
1836
func fromSourceMap(ctx Context, res *http.Response) {
19-
info(ctx.Depth, "Processing source map...")
37+
Info(ctx.Depth, "Processing source map...")
2038

2139
var sourceMap SourceMap
2240
if err := json.NewDecoder(res.Body).Decode(&sourceMap); err != nil {
23-
error(ctx.Depth, "Failed to parse source map:", err)
41+
Error(ctx.Depth, "Failed to parse source map:", err)
2442
return
2543
}
2644

27-
info(ctx.Depth, "Unpacking", len(sourceMap.Sources), "sources...")
45+
Info(ctx.Depth, "Unpacking", len(sourceMap.Sources), "sources...")
2846
ctx.Depth++
2947

3048
for index, source := range sourceMap.Sources {
@@ -45,26 +63,22 @@ func fromSourceMap(ctx Context, res *http.Response) {
4563
}
4664

4765
// remove reserved characters
48-
source, err := filenamify.FilenamifyV2(source, func(options *filenamify.Options) {
49-
options.Replacement = "_"
50-
})
51-
66+
relPath, err := pathify(source)
5267
if err != nil {
53-
error(ctx.Depth, "Failed to filenamify source:", err)
68+
Error(ctx.Depth, "Failed to normalize pathname:", err)
5469
continue
5570
}
5671

57-
path := filepath.Join(filepath.Clean(ctx.Dir), source)
72+
path := filepath.Join(ctx.Dir, relPath)
5873
dir := filepath.Dir(path)
59-
6074
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
61-
error(ctx.Depth, "Failed to create source directory:", err)
75+
Error(ctx.Depth, "Failed to create source directory:", err)
6276
continue
6377
}
6478

6579
content := sourceMap.SourcesContent[index]
6680
if err := os.WriteFile(path, []byte(content), os.ModePerm); err != nil {
67-
error(ctx.Depth, "Failed to write source file:", err)
81+
Error(ctx.Depth, "Failed to write source file:", err)
6882
continue
6983
}
7084
}

url.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func fromUrl(ctx Context) {
1212

1313
contentType := res.Header.Get("Content-Type")
1414
if contentType == "" {
15-
warn(ctx.Depth, "URL responded with no content type:", contentType)
15+
Warn(ctx.Depth, "URL responded with no content type:", contentType)
1616
return
1717
}
1818

@@ -36,6 +36,6 @@ func fromUrl(ctx Context) {
3636
fromSourceMap(ctx, res)
3737

3838
default:
39-
error(ctx.Depth, "URL responded with unknown content type:", contentType)
39+
Error(ctx.Depth, "URL responded with unknown content type:", contentType)
4040
}
41-
}
41+
}

0 commit comments

Comments
 (0)