aboutsummaryrefslogtreecommitdiff
path: root/scanner.go
blob: 9c983b21f0de259fdf6c0ca2e7e09a1628209885 (plain) (blame)
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
package main

import (
	"io/fs"
	"os"
	"path/filepath"
	"runtime"
	"sync"
)

var knownDependencyDirectories = map[string]bool{
	"node_modules": true,
	"target":       true,
	".next":        true,
	".nuxt":        true,
	"__pycache__":  true,
	".venv":        true,
	"venv":         true,
	".gradle":      true,
	"Pods":         true,
	".zig-cache":   true,
	"zig-cache":    true,
	"zig-out":      true,
	"_build":       true,
	".dart_tool":   true,
}
var ignoredDirectories = map[string]bool{
	".git": true,
	".hg":  true,
	".svn": true,
}

type DependencyDirectory struct {
	Path          string
	RelativePath  string
	SizeBytes     int64
	DirectoryType string
}

func scanForDependencies(rootPath string) ([]DependencyDirectory, error) {
	var foundDirectories []DependencyDirectory

	walkError := filepath.WalkDir(rootPath, func(currentPath string, entry fs.DirEntry, accessError error) error {
		if accessError != nil {
			return filepath.SkipDir
		}

		if !entry.IsDir() {
			return nil
		}

		if ignoredDirectories[entry.Name()] {
			return filepath.SkipDir
		}

		if knownDependencyDirectories[entry.Name()] {
			relativePath, _ := filepath.Rel(rootPath, currentPath)
			foundDirectories = append(foundDirectories, DependencyDirectory{
				Path:          currentPath,
				RelativePath:  relativePath,
				DirectoryType: entry.Name(),
			})

			return filepath.SkipDir
		}

		return nil
	})

	if walkError != nil {
		return nil, walkError
	}

	var waitGroup sync.WaitGroup

	semaphore := make(chan struct{}, runtime.NumCPU())

	for directoryIndex := range foundDirectories {
		waitGroup.Add(1)

		go func(targetIndex int) {
			defer waitGroup.Done()

			semaphore <- struct{}{}

			defer func() { <-semaphore }()

			foundDirectories[targetIndex].SizeBytes = calculateDirectorySize(foundDirectories[targetIndex].Path)
		}(directoryIndex)
	}

	waitGroup.Wait()

	return foundDirectories, nil
}

func calculateDirectorySize(directoryPath string) int64 {
	var totalSize int64

	_ = filepath.WalkDir(directoryPath, func(_ string, entry fs.DirEntry, walkError error) error {
		if walkError != nil || entry.IsDir() {
			return nil
		}

		fileInformation, informationError := entry.Info()

		if informationError == nil {
			totalSize += fileInformation.Size()
		}

		return nil
	})

	return totalSize
}

func deleteDependencies(dependencies []DependencyDirectory, selected map[int]bool) (int64, int) {
	var freedBytes int64
	var deletedCount int

	for dependencyIndex, dependency := range dependencies {
		if !selected[dependencyIndex] {
			continue
		}

		if removeError := os.RemoveAll(dependency.Path); removeError == nil {
			freedBytes += dependency.SizeBytes

			deletedCount++
		}
	}

	return freedBytes, deletedCount
}