-
Notifications
You must be signed in to change notification settings - Fork 559
/
sdk_typescript.go
217 lines (178 loc) · 5.82 KB
/
sdk_typescript.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"context"
"fmt"
"path"
"strings"
"golang.org/x/sync/errgroup"
"github.com/dagger/dagger/ci/build"
"github.com/dagger/dagger/ci/internal/dagger"
"github.com/dagger/dagger/ci/util"
)
// TODO: use dev module (this is just the mage port)
const (
typescriptRuntimeSubdir = "sdk/typescript/runtime"
typescriptGeneratedAPIPath = "sdk/typescript/api/client.gen.ts"
nodeVersionMaintenance = "18"
nodeVersionLTS = "20"
bunVersion = "1.0.27"
)
type TypescriptSDK struct {
Dagger *Dagger // +private
}
// Lint the Typescript SDK
func (t TypescriptSDK) Lint(ctx context.Context) error {
eg, ctx := errgroup.WithContext(ctx)
base := t.nodeJsBase()
eg.Go(func() error {
_, err := base.WithExec([]string{"yarn", "lint"}).Sync(ctx)
return err
})
eg.Go(func() error {
path := "docs/current_docs"
_, err := base.
WithDirectory(
fmt.Sprintf("/%s", path),
t.Dagger.Source.Directory(path),
dagger.ContainerWithDirectoryOpts{
Include: []string{
"**/*.mts",
"**/*.mjs",
"**/*.ts",
"**/*.js",
"*prettier*",
"*eslint*",
},
},
).
WithExec([]string{"yarn", "docs:lint"}).
Sync(ctx)
return err
})
eg.Go(func() error {
return util.DiffDirectoryF(ctx, t.Dagger.Source, t.Generate, typescriptGeneratedAPIPath)
})
eg.Go(func() error {
return lintGoModule(ctx, false, daggerDevelop(t.Dagger.Source, typescriptRuntimeSubdir), []string{typescriptRuntimeSubdir})
})
return eg.Wait()
}
// Test the Typescript SDK
func (t TypescriptSDK) Test(ctx context.Context) error {
installer, err := t.Dagger.installer(ctx, "sdk-typescript-test")
if err != nil {
return err
}
eg, ctx := errgroup.WithContext(ctx)
// Loop over the LTS and Maintenance versions and test them
for _, version := range []string{nodeVersionLTS, nodeVersionMaintenance} {
base := t.nodeJsBaseFromVersion(version).With(installer)
eg.Go(func() error {
_, err := base.
WithExec([]string{"yarn", "test:node"}).
Sync(ctx)
return err
})
}
eg.Go(func() error {
_, err = t.bunJsBase().
With(installer).
WithExec([]string{"bun", "test:bun"}).
Sync(ctx)
return err
})
return eg.Wait()
}
// Regenerate the Typescript SDK API
func (t TypescriptSDK) Generate(ctx context.Context) (*Directory, error) {
installer, err := t.Dagger.installer(ctx, "sdk-typescript-generate")
if err != nil {
return nil, err
}
build, err := build.NewBuilder(ctx, t.Dagger.Source)
if err != nil {
return nil, err
}
generated := t.nodeJsBase().
With(installer).
WithFile("/usr/local/bin/codegen", build.CodegenBinary()).
WithExec([]string{"codegen", "--lang", "typescript", "-o", path.Dir(typescriptGeneratedAPIPath)}).
WithExec([]string{"yarn", "fmt", typescriptGeneratedAPIPath}).
File(typescriptGeneratedAPIPath)
return dag.Directory().WithFile(typescriptGeneratedAPIPath, generated), nil
}
// Publish the Typescript SDK
func (t TypescriptSDK) Publish(
ctx context.Context,
tag string,
// +optional
dryRun bool,
// +optional
npmToken *Secret,
) error {
version := strings.TrimPrefix(tag, "sdk/typescript/v")
build := t.nodeJsBase().
WithExec([]string{"npm", "run", "build"}).
WithExec([]string{"npm", "version", version})
if !dryRun {
plaintext, err := npmToken.Plaintext(ctx)
if err != nil {
return err
}
npmrc := fmt.Sprintf(`//registry.npmjs.org/:_authToken=%s
registry=https://registry.npmjs.org/
always-auth=true`, plaintext)
build = build.WithMountedSecret(".npmrc", dag.SetSecret("npmrc", npmrc))
}
publish := build.WithExec([]string{"npm", "publish", "--access", "public"})
if dryRun {
publish = build.WithExec([]string{"npm", "publish", "--access", "public", "--dry-run"})
}
_, err := publish.Sync(ctx)
return err
}
// Bump the Typescript SDK's Engine dependency
func (t TypescriptSDK) Bump(version string) (*Directory, error) {
// trim leading v from version
version = strings.TrimPrefix(version, "v")
engineReference := fmt.Sprintf("// Code generated by dagger. DO NOT EDIT.\n"+
"export const CLI_VERSION = %q\n", version)
// NOTE: if you change this path, be sure to update .github/workflows/publish.yml so that
// provision tests run whenever this file changes.
return dag.Directory().WithNewFile("sdk/typescript/provisioning/default.ts", engineReference), nil
}
func (t TypescriptSDK) nodeJsBase() *Container {
// Use the LTS version by default
return t.nodeJsBaseFromVersion(nodeVersionMaintenance)
}
func (t TypescriptSDK) nodeJsBaseFromVersion(nodeVersion string) *Container {
appDir := "sdk/typescript"
src := t.Dagger.Source.Directory(appDir)
// Mirror the same dir structure from the repo because of the
// relative paths in eslint (for docs linting).
mountPath := fmt.Sprintf("/%s", appDir)
nodeVersionImage := fmt.Sprintf("node:%s-alpine", nodeVersion)
return dag.Container().
// ⚠️ Keep this in sync with the engine version defined in package.json
From(nodeVersionImage).
WithWorkdir(mountPath).
WithMountedCache("/usr/local/share/.cache/yarn", dag.CacheVolume(fmt.Sprintf("yarn_cache:%s", nodeVersion))).
WithFile(fmt.Sprintf("%s/package.json", mountPath), src.File("package.json")).
WithFile(fmt.Sprintf("%s/yarn.lock", mountPath), src.File("yarn.lock")).
WithExec([]string{"yarn", "install"}).
WithDirectory(mountPath, src)
}
func (t TypescriptSDK) bunJsBase() *Container {
appDir := "sdk/typescript"
src := t.Dagger.Source.Directory(appDir)
// Mirror the same dir structure from the repo because of the
// relative paths in eslint (for docs linting).
mountPath := fmt.Sprintf("/%s", appDir)
return dag.Container().
From("oven/bun:"+bunVersion).
WithWorkdir(mountPath).
WithMountedCache("/root/.bun/install/cache", dag.CacheVolume("bun_cache")).
WithFile(fmt.Sprintf("%s/package.json", mountPath), src.File("package.json")).
WithExec([]string{"bun", "install"}).
WithDirectory(mountPath, src)
}