-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
121 lines (97 loc) · 2.87 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path"
"time"
"github.com/jpmorganchase/depcom/parse"
"github.com/mattn/go-zglob"
)
type DepcomResult struct {
Time string
parse.Imports
}
type ArrayFlags []string
func (i *ArrayFlags) String() string {
return ""
}
func (i *ArrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func main() {
var directoryPath string
var includePattern string
var excludePattern ArrayFlags
var parsedImports *parse.Imports
var help bool
flag.StringVar(&directoryPath, "d", "./", "Base directory path")
flag.StringVar(&includePattern, "a", "/**/*.{tsx,jsx,mjs,cjs,ts,js,css}", "Glob pattern of files to analyze")
flag.Var(&excludePattern, "x", "Glob pattern of files to exclude from analysis")
flag.BoolVar(&help, "h", false, "Display help")
flag.Parse()
tail := flag.Args()
cleanBaseDirectory := path.Clean(directoryPath)
start := time.Now()
if help {
flag.PrintDefaults()
os.Exit(1)
} else if !isFlagPassed("d") && !isFlagPassed("a") && !isFlagPassed("x") && len(tail) > 0 {
parsedImports = parse.FromFiles(tail)
} else {
parsedImports = globMatches(cleanBaseDirectory, includePattern, excludePattern)
}
elapsed := time.Since(start)
jsonResult, _ := json.Marshal(DepcomResult{Time: elapsed.String(), Imports: *parsedImports})
fmt.Println(string(jsonResult))
}
func globMatches(baseDirectory string, includePattern string, excludePatterns []string) *parse.Imports {
exclusions := [][]string{}
includedFiles, err := zglob.Glob(baseDirectory + "/" + includePattern)
if err != nil {
return &parse.Imports{Logs: parse.LogMap{Err: []string{fmt.Sprintf("Error globbing files: %s. Error message is: %s", includePattern, err.Error())}}}
}
for _, excludePattern := range excludePatterns {
if excludePattern != "" {
exclusionMatches, err := zglob.Glob(baseDirectory + "/" + excludePattern)
if err != nil {
return &parse.Imports{Logs: parse.LogMap{Err: []string{fmt.Sprintf("Error globbing excluded files: %s. Error message is: %s", excludePattern, err.Error())}}}
}
exclusions = append(exclusions, exclusionMatches)
}
}
fileList := multipleArrayDifference(includedFiles, exclusions)
if len(fileList) == 0 {
return &parse.Imports{Logs: parse.LogMap{Err: []string{"No matches found"}}}
} else {
return parse.FromFiles(fileList)
}
}
func multipleArrayDifference(a []string, b [][]string) []string {
mb := make(map[string]struct{}, len(b))
// Accumulate all the exclusions in a map
for _, x := range b {
for _, y := range x {
mb[y] = struct{}{}
}
}
var diff []string
// Everything that's not in the accumulator gets added
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}