forked from gen0cide/gscript
/
tidy.go
65 lines (57 loc) · 1.49 KB
/
tidy.go
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/ditashi/jsbeautifier-go/jsbeautifier"
"github.com/urfave/cli/v2"
)
var (
tidyCommand = &cli.Command{
Name: "tidy",
Usage: "tidy the syntax of a provided gscript",
UsageText: "gscript tidy GSCRIPT",
Action: tidyScriptCommand,
}
defaultTidyOptions = map[string]interface{}{
"indent_size": 2,
"indent_char": " ",
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"space_in_paren": false,
"space_in_empty_paren": false,
"e4x": false,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 0,
"break_chained_methods": false,
"end_with_newline": true,
}
)
func tidyScriptCommand(c *cli.Context) error {
if c.Args().First() == "" {
return errors.New("must supply a gscript to this command")
}
target := c.Args().First()
if _, err := os.Stat(target); err != nil {
return err
}
data, err := ioutil.ReadFile(target)
if err != nil {
return err
}
dataString := string(data)
beautified, err := jsbeautifier.Beautify(&dataString, defaultTidyOptions)
if err != nil {
return err
}
fmt.Println(beautified)
return nil
}