-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathrun.go
163 lines (138 loc) · 4.33 KB
/
run.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
package build
import (
"compress/gzip"
"errors"
"fmt"
"log"
"os"
"slices"
"strings"
"time"
"github.com/delaneyj/toolbelt"
"github.com/evanw/esbuild/pkg/api"
"github.com/valyala/bytebufferpool"
)
func Build() error {
version, err := extractVersion()
if err != nil {
return fmt.Errorf("error extracting version: %w", err)
}
if err := errors.Join(
// createPluginManifest(),
createBundles(version),
writeOutConsts(version),
); err != nil {
return fmt.Errorf("error creating bundles: %w", err)
}
return nil
}
func extractVersion() (string, error) {
versionFilepath := "VERSION"
versionBytes, err := os.ReadFile(versionFilepath)
if err != nil {
return "", fmt.Errorf("error reading package.json: %w", err)
}
version := strings.TrimSpace(string(versionBytes))
// Write out the version to the version file.
versionPath := "library/src/engine/version.ts"
versionContents := fmt.Sprintf("export const VERSION = '%s';\n", version)
if err := os.WriteFile(versionPath, []byte(versionContents), 0o644); err != nil {
return "", fmt.Errorf("error writing version file: %w", err)
}
return version, nil
}
func createBundles(version string) error {
log.Print("Creating bundles...")
defer log.Print("Bundles created!")
outDir := "./bundles"
os.RemoveAll(outDir)
result := api.Build(api.BuildOptions{
EntryPoints: []string{
"library/src/bundles/datastar-core.ts",
"library/src/bundles/datastar.ts",
},
Banner: map[string]string{
"js": "// Datastar v" + version,
},
Outdir: outDir,
Bundle: true,
Write: true,
LogLevel: api.LogLevelInfo,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
Format: api.FormatESModule,
Sourcemap: api.SourceMapLinked,
Target: api.ES2023,
})
if len(result.Errors) > 0 {
errs := make([]error, len(result.Errors))
for i, err := range result.Errors {
errs[i] = errors.New(err.Text)
}
return errors.Join(errs...)
}
return nil
}
func writeOutConsts(version string) error {
log.Print("Extracting version...")
Consts.Version = version
build, err := os.ReadFile("bundles/datastar.js")
if err != nil {
return fmt.Errorf("error reading datastar.js: %w", err)
}
Consts.VersionClientByteSize = len(build)
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
w, err := gzip.NewWriterLevel(buf, gzip.BestCompression)
if err != nil {
panic(err)
}
if _, err := w.Write(build); err != nil {
panic(err)
}
w.Close()
Consts.VersionClientByteSizeGzip = buf.Len()
var zeroCased toolbelt.CasedString
// Make sure all enums are set up.
for _, enum := range Consts.Enums {
for _, value := range enum.Values {
if value.Name == zeroCased {
value.Name = toolbelt.ToCasedString(value.Value)
}
}
if enum.DefaultIndex >= 0 {
enum.Default = enum.Values[enum.DefaultIndex]
}
}
slices.SortFunc(Consts.SDKLanguages, func(a, b Language) int {
return strings.Compare(a.Name, b.Name)
})
templates := map[string]func(data *ConstTemplateData) string{
"README.md": datastarREADME,
"library/README.md": datastarREADME,
"library/src/engine/consts.ts": datastarClientConsts,
"library/package.json": datastarClientPackageJSON,
"sdk/go/consts.go": goConsts,
"sdk/dotnet/src/Consts.fs": dotnetConsts,
"sdk/php/src/Consts.php": phpConsts,
"sdk/php/src/enums/EventType.php": phpEventType,
"sdk/php/src/enums/FragmentMergeMode.php": phpFragmentMergeMode,
"sdk/java/core/src/main/java/starfederation/datastar/Consts.java": javaConsts,
"sdk/java/core/src/main/java/starfederation/datastar/enums/EventType.java": javaEventType,
"sdk/java/core/src/main/java/starfederation/datastar/enums/FragmentMergeMode.java": javaFragmentMergeMode,
"sdk/python/src/datastar_py/consts.py": pythonConsts,
"sdk/typescript/src/consts.ts": typescriptConsts,
}
for path, tmplFn := range templates {
log.Printf("Writing %s...", path)
contents := strings.TrimSpace(tmplFn(Consts))
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
return fmt.Errorf("error writing version file: %w", err)
}
}
return nil
}
func durationToMs(d time.Duration) int {
return int(d.Milliseconds())
}