diff --git a/README.md b/README.md index b025c66..9cc3224 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ go install github.com/wux1an/wxapkg@latest - [x] scan more information (need networking) - [x] json beautify -- [ ] javascript beautify +- [x] javascript beautify - [x] html beautify - [ ] auto export uri in files diff --git a/cmd/unpack.go b/cmd/unpack.go index 01649b2..3a9569f 100644 --- a/cmd/unpack.go +++ b/cmd/unpack.go @@ -8,6 +8,7 @@ import ( "encoding/binary" "errors" "fmt" + "github.com/ditashi/jsbeautifier-go/jsbeautifier" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/tidwall/pretty" @@ -198,8 +199,15 @@ func fileBeautify(name string, data []byte) (result []byte) { switch ext { case ".json": result = pretty.Pretty(data) - case ".html": - result = gohtml.FormatBytes(data) + case ".html": // todo beautify js code in html + result = gohtml.FormatBytes(bytes.TrimSpace(data)) // remove leading whitespace + case ".js": + var code = string(bytes.TrimSpace(data)) // remove leading whitespace + options := jsbeautifier.DefaultOptions() + beautify, err := jsbeautifier.Beautify(&code, options) + if err == nil { + result = []byte(beautify) + } } return result diff --git a/go.mod b/go.mod index d814a01..8df33c2 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/charmbracelet/harmonica v0.2.0 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect + github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index 0880142..74167da 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkX github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c h1:+Zo5Ca9GH0RoeVZQKzFJcTLoAixx5s5Gq3pTIS+n354= +github.com/ditashi/jsbeautifier-go v0.0.0-20141206144643-2520a8026a9c/go.mod h1:HJGU9ULdREjOcVGZVPB5s6zYmHi1RxzT71l2wQyLmnE= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=