-
Notifications
You must be signed in to change notification settings - Fork 300
/
js_sdk.go
200 lines (180 loc) · 5.42 KB
/
js_sdk.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
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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ttnmage
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
)
// JsSDK namespace.
type JsSDK mg.Namespace
func (k JsSDK) runYarnV(args ...string) error {
return runYarnV(append([]string{yarnWorkingDirectoryArg("sdk", "js")}, args...)...)
}
func (k JsSDK) runYarnCommandV(cmd string, args ...string) error {
return k.runYarnV(append([]string{"run", cmd}, args...)...)
}
// Deps installs the javascript SDK dependencies.
func (k JsSDK) Deps() error {
ok, err := target.Dir(
filepath.Join("sdk", "js", "node_modules"),
filepath.Join("sdk", "js", "package.json"),
filepath.Join("sdk", "js", "yarn.lock"),
)
if err != nil {
return targetError(err)
}
if !ok {
return nil
}
if mg.Verbose() {
fmt.Println("Installing JS SDK dependencies")
}
mg.Deps(Js.deps)
// On initial installs, the dependency installation will cause the SDK itself
// to be installed in an unbuilt state. In that case we need to remove the
// module altogether so it can be properly reinstalled later.
if _, err := os.Stat(filepath.Join("node_modules", "ttn-lw", "dist")); os.IsNotExist(err) {
sh.Rm(filepath.Join("node_modules", "ttn-lw"))
}
return k.runYarnV("install", "--no-progress", "--production=false")
}
// Build builds the source files and output into 'dist'.
func (k JsSDK) Build() error {
ok, err := target.Dir(
filepath.Join("sdk", "js", "dist"),
filepath.Join("sdk", "js", "src"),
filepath.Join("sdk", "js", "generated"),
)
if err != nil {
return targetError(err)
}
if !ok {
return nil
}
mg.Deps(k.Deps, k.Definitions)
if mg.Verbose() {
fmt.Println("Building JS SDK files")
}
return k.runYarnCommandV("build")
}
// Watch builds the source files in watch mode.
func (k JsSDK) Watch() error {
mg.Deps(JsSDK.Deps, JsSDK.Definitions)
if mg.Verbose() {
fmt.Println("Building and watching JS SDK files")
}
return k.runYarnCommandV("build:watch")
}
// Fmt formats all js files.
func (k JsSDK) Fmt() error {
mg.Deps(JsSDK.Deps)
if mg.Verbose() {
fmt.Println("Running prettier on JS SDK .js files")
}
return k.runYarnCommandV("fmt")
}
// Lint runs eslint over sdk js files.
func (k JsSDK) Lint() error {
mg.Deps(JsSDK.Deps, JsSDK.Definitions)
if mg.Verbose() {
fmt.Println("Running eslint on JS SDK .js files")
}
return k.runYarnCommandV("lint")
}
// Test runs jest unit tests.
func (k JsSDK) Test() error {
mg.Deps(JsSDK.Deps)
if mg.Verbose() {
fmt.Println("Running JS SDK tests")
}
return k.runYarnCommandV("test")
}
// TestWatch runs jest unit tests in watch mode.
func (k JsSDK) TestWatch() error {
mg.Deps(JsSDK.Deps)
if mg.Verbose() {
fmt.Println("Running JS SDK tests in watch mode")
}
return k.runYarnCommandV("test:watch")
}
// Clean clears all transpiled files.
func (k JsSDK) Clean() {
mg.Deps(JsSDK.DefinitionsClean)
sh.Rm(filepath.Join("sdk", "js", "dist"))
}
// CleanDeps removes all installed node packages (rm -rf node_modules).
func (JsSDK) CleanDeps() {
sh.Rm(filepath.Join("sdk", "js", "node_modules"))
}
// Definitions extracts the api-definition.json from the proto generated api.json.
func (k JsSDK) Definitions() error {
mg.Deps(Proto.JsSDK, JsSDK.AllowedFieldMaskPaths)
ok, err := target.Path(
filepath.Join("sdk", "js", "generated", "api-definition.json"),
filepath.Join("sdk", "js", "generated", "api.json"),
filepath.Join("sdk", "js", "generated", "allowed-field-mask-paths.json"),
)
if err != nil {
return targetError(err)
}
if !ok {
return nil
}
if mg.Verbose() {
fmt.Println("Extracting api definitions from protos")
}
return k.runYarnCommandV("definitions")
}
// DefinitionsClean removes the generated api-definition.json.
func (k JsSDK) DefinitionsClean(context.Context) error {
err := sh.Rm(filepath.Join("sdk", "js", "generated", "allowed-field-mask-paths.json"))
if err != nil {
return err
}
return sh.Rm(filepath.Join("sdk", "js", "generated", "api-definition.json"))
}
// AllowedFieldMaskPaths builds the allowed field masks file based on the ttnpb package.
func (k JsSDK) AllowedFieldMaskPaths() error {
ok, err := target.Path(
filepath.Join("sdk", "js", "generated", "allowed-field-mask-paths.json"),
filepath.Join("pkg", "ttnpb", "field_mask_validation.go"),
filepath.Join("tools", "generate_allowed_field_mask_paths.go"),
)
if err != nil {
return targetError(err)
}
if !ok {
return nil
}
return runGoTool("generate_allowed_field_mask_paths.go")
}
// DeviceFieldMasks generates end device entity map.
func (k JsSDK) DeviceFieldMasks() error {
ok, err := target.Path(
filepath.Join("sdk", "js", "generated", "device-entity-map.json"),
filepath.Join("sdk", "js", "generated", "allowed-field-mask-paths.json"),
)
if err != nil {
return targetError(err)
}
if !ok {
return nil
}
return sh.Run("node", "sdk/js/util/device-field-mask-mapper.js")
}