-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (77 loc) · 1.84 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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/gookit/color"
"github.com/manifoldco/promptui"
)
func dirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
func main() {
var startPath string
fmt.Print("Enter the path to start scanning from (leave blank for current directory): ")
fmt.Scanln(&startPath)
if startPath == "" {
startPath = "."
}
fmt.Println("Scanning for node_modules directories...")
var nodeModulesDirs []string
var nodeModulesSizes []int64
err := filepath.WalkDir(startPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && d.Name() == "node_modules" {
nodeModulesDirs = append(nodeModulesDirs, path)
size, err := dirSize(path)
if err != nil {
return err
}
size = size / 1048576
path = strings.TrimPrefix(path, startPath)
nodeModulesSizes = append(nodeModulesSizes, size)
fmt.Printf("%s (%d MB)\n", path, size)
return filepath.SkipDir
}
return nil
})
if err != nil {
panic(err)
}
if len(nodeModulesDirs) == 0 {
color.Red.Println("No node_modules directories found.")
return
}
prompt := promptui.Select{
Label: "Do you want to delete all found node_modules directories?",
Items: []string{"Yes", "No"},
}
_, result, err := prompt.Run()
if err != nil {
panic(err)
}
if result == "No" {
color.Yellow.Println("No directories were deleted.")
return
}
for _, dir := range nodeModulesDirs {
fmt.Printf("Deleting %s...\n", dir)
err := os.RemoveAll(dir)
if err != nil {
panic(err)
}
}
color.Green.Println("All node_modules directories were deleted.")
color.Cyan.Println("Made with love by cajereta using Go!")
}