-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (73 loc) · 2.02 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
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
"time"
)
var wg = sync.WaitGroup{}
func main() {
srcPath, fileToReplacePath := getCmdParams()
runDuplicator(srcPath, fileToReplacePath)
}
func runDuplicator(srcPath string, outPutFileName string) {
start := time.Now()
paths := make([]string, 0)
fmt.Println("Program started...")
f, err := os.Open(srcPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
txt := scanner.Text()
// "from" to make sure that there are no multiline import issues
res, err := regexp.Compile("from")
if matches := res.MatchString(txt); err != nil || !matches {
continue
}
// splits the line by the "from" keyword, and removes it
split := res.Split(txt, -1)
path, err := getPath(split)
if err != nil {
continue
}
importPath := getImportPathWithExtension(path)
realPath := getRealPath(srcPath, importPath)
fileName := getFileName(path)
nBytesChnl := make(chan int64)
copyErrChnl := make(chan error)
wg.Add(1)
go copy(realPath, "./out/"+fileName, nBytesChnl, copyErrChnl)
nBytes, copyErr := <-nBytesChnl, <-copyErrChnl
if copyErr != nil {
log.Fatal(copyErrChnl, " Error copying file")
}
// remove file extension from import path
importPath = strings.Replace(importPath, ".ts", "", -1)
paths = append(paths, importPath)
fmt.Printf("Copied %v bytes from %s to %s\n", nBytes, realPath, "./out/"+fileName)
}
if err := scanner.Err(); err != nil {
log.Fatal(err, "Error while reading file")
}
fmt.Printf("Updating %s imports in %s...\n", srcPath, outPutFileName)
updatePaths(paths, srcPath, outPutFileName)
elapsed := time.Since(start)
log.Printf("Execution time was %s", elapsed)
wg.Wait()
}
func getCmdParams() (string, string) {
if len(os.Args) != 3 {
log.Fatal("Usage: go run . <source_path> <output_file_name>")
}
if strings.Contains(os.Args[2], "/") {
log.Fatal("<output_file_name> must be a file name, not a path")
}
return os.Args[1], os.Args[2]
}