forked from segmentio/analytics.js-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.go
145 lines (117 loc) · 3.25 KB
/
integration.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
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
package operations
import (
"encoding/json"
"errors"
"io/ioutil"
"path"
)
// Integration represents an integration contained in the monorepo
type Integration struct {
Name string
Path string
Package Package
monorepo *Monorepo
}
// Package is the representation of package.json
type Package struct {
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Keywords []string `json:"keywords"`
Main string `json:"main"`
Scripts Scripts `json:"scripts"`
Author string `json:"author"`
License string `json:"license"`
Homepage string `json:"homepage"`
Bugs Bugs `json:"bugs"`
Repository Repository `json:"repository"`
Dependencies Dependencies `json:"dependencies"`
DevDependencies Dependencies `json:"devDependencies"`
}
// Bugs from package.json
type Bugs struct {
URL string `json:"url"`
}
// Dependencies is a map of package:version
type Dependencies map[string]string
// Repository from package.json
type Repository struct {
Type string `json:"type"`
URL string `json:"url"`
}
// Scripts from package.json
type Scripts map[string]string
// OpenIntegration retuns a reference to the integration stored in the monorepo.
func (m *Monorepo) OpenIntegration(name string) (*Integration, error) {
folder := path.Join(m.getIntegrationsFolder(), name)
packageJSON := path.Join(folder, "package.json")
folderExists, err := fileExists(folder)
if err != nil {
return nil, err
}
packageExists, err := fileExists(packageJSON)
if err != nil {
return nil, err
}
if !folderExists || !packageExists {
err := errors.New("not found")
LogError(err, "Error opening integration")
return nil, err
}
p, err := DecodePackage(packageJSON)
if err != nil {
return nil, err
}
return &Integration{
Name: name,
Path: folder,
Package: p,
monorepo: m,
}, nil
}
// OpenAllIntegrations scans the integration folder
func (m *Monorepo) OpenAllIntegrations() (map[string]*Integration, error) {
entries, err := ioutil.ReadDir(m.getIntegrationsFolder())
if err != nil {
LogError(err, "Error reading directory")
return nil, err
}
integrations := make(map[string]*Integration)
for _, entry := range entries {
if entry.IsDir() {
integration, err := m.OpenIntegration(entry.Name())
if err == nil {
// Is not an integration
integrations[integration.Name] = integration
}
}
}
return integrations, nil
}
// DecodePackage parses package.json into the struct
func DecodePackage(file string) (Package, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
LogError(err, "Error reading package.json")
return Package{}, err
}
var p Package
if err := json.Unmarshal(data, &p); err != nil {
LogError(err, "Error decoding package.json")
return Package{}, err
}
return p, nil
}
// EncodePackage writes the struct into package.json
func EncodePackage(p Package, file string) error {
data, err := json.MarshalIndent(p, "", " ")
if err != nil {
LogError(err, "Error encoding package")
return err
}
if err := ioutil.WriteFile(file, data, 0644); err != nil {
LogError(err, "Error writing package.json")
return err
}
return nil
}