-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.go
More file actions
430 lines (346 loc) · 12.4 KB
/
utils.go
File metadata and controls
430 lines (346 loc) · 12.4 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"os"
"strings"
log "github.com/sirupsen/logrus"
)
// flags to make sure a message is only logged once
var didPrintNodeModulesMsg bool = false
var didPrintTestsMsg bool = false
var didPrintDefaultTestExtensionsMsg bool = false
var didPrintDefaultTestFoldersMsg bool = false
var didPrintStylesheetsMsg bool = false
var didPrintImagesMsg bool = false
var didPrintDocumentsMsg bool = false
var didPrintFontsMsg bool = false
var didPrintIdesMsg bool = false
var didPrintBuildMsg bool = false
var didPrintPublicMsg bool = false
var didPrintDistMsg bool = false
var didPrintDbsMsg bool = false
var didPrintAngularFolderMsg bool = false
var didPrintGitFolderMsg bool = false
var didPrintBowerComponentsMsg bool = false
var didPrintVideoMsg bool = false
var didPringIsMinified bool = false
var didPrintArchiveMsg bool = false
// check for the `package-lock.json`, `yarn.lock` or `bower.json` (required for SCA)
func CheckIfSCAFileExists(path string) bool {
// we don't want to look for `package-lock.json` and `yarn.lock` within `bower_components`
if !strings.Contains(path, "bower_components") {
packageManagerFiles := [2]string{"package-lock.json", "yarn.lock"}
for _, element := range packageManagerFiles {
if strings.HasSuffix(path, element) {
return true
}
}
}
// NOTE: It looks like the `bower.json` file would be in `bower_components`? (tbh, I am not 100% sure how Bower
// works exactly, but it's been depreacted like forever and I can't really be bothered looking into how exactly it works)
bowerFile := "bower.json"
return strings.HasSuffix(path, bowerFile)
}
// check for the `node_modules` folder
func IsNodeModules(path string) bool {
if strings.Contains(path, string(os.PathSeparator)+"node_modules") {
if !didPrintNodeModulesMsg {
log.Info("\tIgnoring the entire `node_modules` folder")
didPrintNodeModulesMsg = true
}
return true
}
return false
}
// check for the `bower_components` folder
func IsBowerComponents(path string) bool {
// NOTE: Turns out, we don't want to omit the `bower_components` folder since it's required for Bower.
// Thus, we don't do anything here for now (this may change in the future)
// if strings.Contains(path, string(os.PathSeparator)+"bower_components") {
// if !didPrintBowerComponentsMsg {
// log.Info("\tIgnoring the entire `bower_components` folder")
// didPrintBowerComponentsMsg = true
// }
// return true
// }
return false
}
// check if it is a `test` path (i.e., a file that e.g. contains unit tests)
func IsInTestFolder(path string, testsPath string) bool {
// Test folders are treated as follows:
// - if `-tests` is provided, then only the provided path will be treated as a test directory (and thus, excluded)
// - if `-tests` is not provided, then `IsCommonTest()` will be called to exclude common test folders
if testsPath == "" {
return IsCommonTestFolder(path)
}
// At this point, `testsPath` may have a value like this: "sample-node-project/test".
// Thus, we want to do 2 things:
// - Exclude this folder itself, i.e. check for a path that ends with "sample-node-project/test"
// - Exclude any file in it, i.e. check for path that contains "sample-node-project/test/"
fileInTestFolderPath := testsPath + string(os.PathSeparator)
if strings.HasSuffix(path, testsPath) || strings.Contains(path, fileInTestFolderPath) {
if !didPrintTestsMsg {
log.Info("\tIgnoring the entire content of the `" + testsPath + "` folder (contains test files)")
didPrintTestsMsg = true
}
return true
}
return false
}
func IsCommonTestFolder(path string) bool {
testPaths := [4]string{"test", "tests", "e2e", "__tests__"}
for _, testPath := range testPaths {
// Here, we want to do 2 things:
// - Exclude the `testPath` itself, i.e. check for a path that ends e.g. with "/e2e"
// - Exclude any file in it, i.e. check for path that contains e.g. "/e2e/"
testFolderPath := string(os.PathSeparator) + testPath
fileInTestFolderPath := testFolderPath + string(os.PathSeparator)
if strings.HasSuffix(path, testFolderPath) || strings.Contains(path, fileInTestFolderPath) {
if !didPrintDefaultTestFoldersMsg {
log.Info("\tIgnoring common test folders (such as `e2e`)")
didPrintDefaultTestFoldersMsg = true
}
return true
}
}
return false
}
// check for common test files (like .spec.js)
func IsTestFile(path string) bool {
testExtensions := []string{".spec.ts", ".spec.tsx", ".test.ts", ".test.tsx", ".spec.js", ".spec.jsx", ".test.js", ".test.jsx"}
for _, element := range testExtensions {
if strings.HasSuffix(path, element) {
if !didPrintDefaultTestExtensionsMsg {
log.Info("\tIgnoring common test extensions (such as `.spec.ts`)")
didPrintDefaultTestExtensionsMsg = true
}
return true
}
}
return false
}
// check for style sheets (like .css and .scss)
func IsStyleSheet(path string) bool {
if strings.HasSuffix(path, ".css") || strings.HasSuffix(path, ".scss") {
if !didPrintStylesheetsMsg {
log.Info("\tIgnoring style sheets (such as `.css`)")
didPrintStylesheetsMsg = true
}
return true
}
return false
}
// check for images (like .jpg, .png, .jpeg)
func IsImage(path string) bool {
imageExtensions := [8]string{".jpg", ".png", ".jpeg", ".gif", ".svg", ".bmp", ".ico", ".icns"}
for _, element := range imageExtensions {
if strings.HasSuffix(path, element) {
if !didPrintImagesMsg {
log.Info("\tIgnoring images (such as `.jpg`)")
didPrintImagesMsg = true
}
return true
}
}
return false
}
// check for documents (like .pdf, .md)
func IsDocument(path string) bool {
// inspired by https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions (and additionally `.md`)
documentExtensions := [38]string{
".pdf",
".md",
".doc", ".dot", ".wbk", ".docx", ".docm", ".dotx", ".dotm", ".docb", ".wll", ".wwl",
".xls", ".xlt", ".xlm", ".xll_", ".xla_", ".xla5", ".xla8",
".xlsx", ".xlsm", ".xltx", ".xltm",
".ppt", ".pot", ".pps", ".pptx", ".pptm", ".potx", ".potm",
".one", ".ecf",
".ACCDA", ".ACCDB", ".ACCDE", ".ACCDT", ".MDA", ".MDE",
}
for _, element := range documentExtensions {
if strings.HasSuffix(path, element) {
if !didPrintDocumentsMsg {
log.Info("\tIgnoring documents (such as `.pdf`, `.docx`, `.md`)")
didPrintDocumentsMsg = true
}
return true
}
}
return false
}
// check for video files
func IsVideo(path string) bool {
// inspired by this list: https://en.wikipedia.org/wiki/Video_file_format
videoExtensions := [18]string{
".mp4", ".webm", ".mkv", ".flv", ".vob", ".ogv", ".drc", ".gifv", ".mng", ".avi", ".mov", ".qt", ".mts", ".wmv", ".amv",
".svi", ".m4v", ".mpg",
}
for _, element := range videoExtensions {
if strings.HasSuffix(path, element) {
if !didPrintVideoMsg {
log.Info("\tIgnoring videos (such as `.mp4`)")
didPrintVideoMsg = true
}
return true
}
}
return false
}
// check for fonts (like .woff)
func IsFont(path string) bool {
fontExtensions := [4]string{".ttf", ".otf", ".woff", ".woff2"}
for _, element := range fontExtensions {
if strings.HasSuffix(path, element) {
if !didPrintFontsMsg {
log.Info("\tIgnoring fonts (such as `.woff`)")
didPrintFontsMsg = true
}
return true
}
}
return false
}
// check for the `.angular` folder
func IsAngularCacheFolder(path string) bool {
// checks for the ".angular" folder itself, i.e. for a path that ends with `/.angular`
// ... or for files within the ".angular" folder, i.e. for a path that contains `/.angular/`
angularFolderPath := string(os.PathSeparator) + ".angular"
fileInAngularFolderPath := angularFolderPath + string(os.PathSeparator)
if strings.HasSuffix(path, angularFolderPath) || strings.Contains(path, fileInAngularFolderPath) {
if !didPrintAngularFolderMsg {
log.Info("\tIgnoring `.angular`")
didPrintAngularFolderMsg = true
}
return true
}
return false
}
// check for the `.git` folder
func IsGitFolder(path string) bool {
// checks for the ".git" folder itself, i.e. for a path that ends with `/.git`
// ... or for files within the ".git" folder, i.e. for a path that contains `/.git/`
gitFolderPath := string(os.PathSeparator) + ".git"
fileInGitFolderPath := gitFolderPath + string(os.PathSeparator)
if strings.HasSuffix(path, gitFolderPath) || strings.Contains(path, fileInGitFolderPath) {
if !didPrintGitFolderMsg {
log.Info("\tIgnoring `.git`")
didPrintGitFolderMsg = true
}
return true
}
return false
}
// check for the dbs (like .db, .sqlite3)
func IsDb(path string) bool {
documentExtensions := [6]string{".db", ".db3", ".sdb", ".sqlite", ".sqlite2", ".sqlite3"}
for _, element := range documentExtensions {
if strings.HasSuffix(path, element) {
if !didPrintDbsMsg {
log.Info("\tIgnoring dbs (such as `.sqlite3`)")
didPrintDbsMsg = true
}
return true
}
}
return false
}
// check for the `build` folder
func IsBuildFolder(path string) bool {
// checks for the "build" folder itself, i.e. for a path that ends with `/build`
// ... or for files within the "build" folder, i.e. for a path that contains `/build/`
buildFolderPath := string(os.PathSeparator) + "build"
fileInBuildFolderPath := buildFolderPath + string(os.PathSeparator)
if strings.HasSuffix(path, buildFolderPath) || strings.Contains(path, fileInBuildFolderPath) {
if !didPrintBuildMsg {
log.Info("\tIgnoring `build` folder")
didPrintBuildMsg = true
}
return true
}
return false
}
// check for the `dist` folder
func IsDistFolder(path string) bool {
// checks for the "dist" folder itself, i.e. for a path that ends with `/dist`
// ... or for files within the "dist" folder, i.e. for a path that contains `/dist/`
distFolderPath := string(os.PathSeparator) + "dist"
fileInDistFolderPath := distFolderPath + string(os.PathSeparator)
if strings.HasSuffix(path, distFolderPath) || strings.Contains(path, fileInDistFolderPath) {
if !didPrintDistMsg {
log.Info("\tIgnoring `dist` folder")
didPrintDistMsg = true
}
return true
}
return false
}
// check for the `public` folder
func IsPublicFolder(path string) bool {
// checks for the "dist" folder itself, i.e. for a path that ends with `/dist`
// ... or for files within the "dist" folder, i.e. for a path that contains `/dist/`
publicFolderPath := string(os.PathSeparator) + "public"
fileInPublicFolderPath := publicFolderPath + string(os.PathSeparator)
if strings.HasSuffix(path, publicFolderPath) || strings.Contains(path, fileInPublicFolderPath) {
if !didPrintPublicMsg {
log.Info("\tIgnoring `public` folder")
didPrintPublicMsg = true
}
return true
}
return false
}
// check for IDE folder (like .code, .idea)
func IsIdeFolder(path string) bool {
idePaths := [2]string{".vscode", ".idea"}
for _, element := range idePaths {
// NOTE: This check should be fine using "Contains" because I don't expect these IDE folder names to be used for
// anything useful (i.e., I don't anticipate a folder name with ".idea" in its name to contain anything useful)
if strings.Contains(path, string(os.PathSeparator)+element) {
if !didPrintIdesMsg {
log.Info("\tIgnoring IDE folder (such as .code, .idea)")
didPrintIdesMsg = true
}
return true
}
}
return false
}
// check for minified JS
func IsMinified(path string) bool {
if strings.HasSuffix(path, ".js.map") || strings.HasSuffix(path, ".min.js") {
if !didPringIsMinified {
log.Info("\tDropping minified JS (i.e., `.js.map` and `.min.js` files)")
didPringIsMinified = true
}
return true
}
return false
}
// check for archives
func IsArchive(path string) bool {
archiveExtensions := []string{".zip", ".zipx", ".gz", ".tar", ".gzip", ".7z", ".rar"}
for _, element := range archiveExtensions {
if strings.HasSuffix(path, element) {
if !didPrintArchiveMsg {
log.Info("\tIgnoring nested archives (such as `.zip`)")
didPrintArchiveMsg = true
}
return true
}
}
return false
}
// check for the "misc" not required stuff
func IsMiscNotRequiredFile(path string) bool {
notRequiredSuffices := []string{
".DS_Store", "__MACOSX", ".gitignore", ".gitkeep", ".gitattributes", ".npmignore", "CNAME", "tsconfig.json",
"tslint.json", "karma.conf.js", "angular.json", ".travis.yml", ".browserslistrc", ".editorconfig",
".d.ts", "protractor.conf.js", ".spec.json", "tsconfig.app.json", "polyfills.ts", "LICENSE", ".bcmap",
}
for _, element := range notRequiredSuffices {
if strings.HasSuffix(path, element) {
// NOTE: At the moment, these "misc" files aren't logged to avoid logging too much
return true
}
}
return false
}