This repository has been archived by the owner on Oct 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbarrel.go
156 lines (128 loc) · 3.49 KB
/
fbarrel.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"os"
"github.com/jessevdk/go-flags"
"io/ioutil"
"bufio"
"path"
"sort"
"strings"
"regexp"
"github.com/deckarep/golang-set"
)
type opts struct {
Path string `short:"p" long:"path" description:"Path to typescript folder where barrel should be created" required:"true"`
Recursive bool `short:"r" long:"recursive" description:"Scans given directory recursively" required:"false"`
}
func toFullPath(path string, files []os.FileInfo) []string {
var result []string;
for _,file := range files {
result = append(result, path + file.Name());
}
return result;
}
func fatal(err error){
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func main() {
var opts = opts{}
_, err := flags.Parse(&opts)
fatal(err)
files,err := listFiles(opts.Path, opts.Recursive)
fatal(err)
dir, err := os.Getwd()
fatal(err)
err = writeBarrel(dir, opts.Path, files)
fatal(err)
os.Exit(0)
}
func writeBarrel(out_path string, ts_path string, files []string) error {
fd, err := os.Create(path.Join(out_path, "barrel.ts")); if err != nil {
return err
}
defer fd.Close()
w := bufio.NewWriter(fd)
for _, f := range files {
var name = f
if(! strings.HasSuffix(name, ".tsx") || strings.Contains(name, "barrel.ts") ){ continue }
content, err := ioutil.ReadFile(f)
if err != nil {
return err;
}
exports := extractExports(string(content[:]));
sortedExports := setToSortedArray(exports)
exportList := ""
for i,ex := range sortedExports {
if i==0 {
exportList = ex
} else {
exportList = exportList + ", " + ex;
}
}
if exportList != "" {
name_without_ext := name[0:strings.LastIndex(name, ".tsx")]
fmt.Printf("Writing to barrel for %s (%s)\n", name_without_ext, name)
_, err = w.WriteString(fmt.Sprintf("export { %s } from '%s';\n", exportList, name_without_ext)); if err != nil {
return err
}
}
}
w.Flush()
return nil
}
func setToSortedArray(set mapset.Set) []string {
var setArray []string
for item := range set.Iterator().C {
setArray = append(setArray, item.(string));
}
sort.Strings(setArray)
return setArray;
}
func listFiles(ts_path string, recursive bool) ([]string, error) {
var result []string;
files, err := ioutil.ReadDir(ts_path)
if err != nil {
return nil,err
}
if recursive {
for _, file := range files {
if(file.IsDir()) {
fmt.Printf("Scanning folder %s\n", file.Name())
recResult, recError := listFiles(ts_path + "/" + file.Name(), true);
if recError != nil {
fatal(recError);
}
result = append(result, recResult...)
} else {
result = append(result, ts_path +"/"+ file.Name())
}
}
} else {
result = toFullPath(ts_path, files);
}
return result,nil
}
func extractExports(content string) mapset.Set {
exports := mapset.NewSet();
defaultExportName := ""
defaultResult := regexp.MustCompile(`export default (class|interface|type )?(\w+)`).FindAllStringSubmatch(content, -1)
for _, value := range defaultResult {
defaultExportName = value[2]
exports.Add("default as " + value[2])
}
regularResult := regexp.MustCompile(`export (class|interface|type) (\w+)`).FindAllStringSubmatch(content, -1)
for _, value := range regularResult {
if !exports.Contains(value[2]) && value[2] != defaultExportName {
if value[2] != "Props" && value[2] != "State" {
exports.Add(value[2])
} else if (value[2] == "Props" || value[2] == "State") && defaultExportName != "" {
exports.Add(value[2] + " as " + defaultExportName+value[2])
}
}
}
return exports
}