-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
60 lines (46 loc) · 1.68 KB
/
node.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
package node
import (
"os"
"path/filepath"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/samber/lo"
"github.com/teamkeel/keel/proto"
)
// IsEnabled returns true if the provided dir contains any tests or if the provided
// schema contains any functions.
func IsEnabled(dir string, s *proto.Schema) bool {
return HasFunctions(s) || HasTests(dir)
}
// HasFunctions returns true if the schema contains any custom functions or jobs.
func HasFunctions(sch *proto.Schema) bool {
var actions []*proto.Action
for _, model := range sch.Models {
actions = append(actions, model.Actions...)
}
hasCustomFunctions := lo.SomeBy(actions, func(o *proto.Action) bool {
return o.Implementation == proto.ActionImplementation_ACTION_IMPLEMENTATION_CUSTOM
})
hasJobs := sch.Jobs != nil && len(sch.Jobs) > 0
hasSubscribers := sch.Subscribers != nil && len(sch.Subscribers) > 0
return hasCustomFunctions || hasJobs || hasSubscribers
}
// HasTests returns true if there any TypeScript test files in dir or any of it's
// subdirectories.
func HasTests(dir string) bool {
fs := os.DirFS(dir)
// the only potential error returned from glob here is bad pattern,
// which we know not to be true
testFiles, _ := doublestar.Glob(fs, "**/*.test.ts")
// there could be other *.test.ts files unrelated to the Keel testing framework,
// so for each test, we do a naive check that the file contents includes a match
// for the string "@teamkeel/testing"
return lo.SomeBy(testFiles, func(path string) bool {
b, err := os.ReadFile(filepath.Join(dir, path))
if err != nil {
return false
}
// todo: improve this check as its pretty naive
return strings.Contains(string(b), "@teamkeel/testing")
})
}