forked from DrakiaXYZ/crankshaft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
156 lines (130 loc) · 3.97 KB
/
build.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
package build
import (
"bytes"
_ "embed"
"fmt"
"io/ioutil"
"log"
"runtime"
"text/template"
"git.sr.ht/~avery/crankshaft/cdp"
"github.com/evanw/esbuild/pkg/api"
)
const VERSION = "0.2.4"
const Target = api.ES2020
type csGlobals struct {
Version string
InjectedScript string
ServerPort string
UIMode cdp.UIMode
SteamDir string
AuthToken string
PluginsDir string
Platform string
}
// Steam is using Chrome 84
var Engines = []api.Engine{
{
Name: api.EngineChrome,
Version: "84",
},
}
func BundleScripts() error {
log.Println("Bundling scripts to inject...")
// TODO: get sourcemaps working
// I tried inline sourcemaps, and aside from being massive, they were broken
// and linked everything to the same line. Tried external maps served from
// the static server, it seems like Chrome was refusing to load them and
// throwing an error.
// (not a huge deal since the bundle is small and isn't minified or anything)
res := api.Build(api.BuildOptions{
EntryPoints: []string{
"injected/src/entrypoints/library.ts",
"injected/src/entrypoints/keyboard.ts",
"injected/src/entrypoints/menu.ts",
"injected/src/entrypoints/quick-access.ts",
"injected/src/entrypoints/app-properties.ts",
},
Bundle: true,
Format: api.FormatIIFE,
Target: Target,
Engines: Engines,
JSXFactory: "h",
JSXFragment: "DocumentFragment",
Inject: []string{"injected/preact-shim.js"},
Loader: map[string]api.Loader{
".svg": api.LoaderDataURL,
// CSS will be loaded directly from javascript
".css": api.LoaderText,
},
Define: map[string]string{
"process": `{"env":{"NODE_ENV":"development"}}`,
},
Plugins: []api.Plugin{DomChefPlugin()},
Outdir: ".build",
Write: true,
})
if err := checkErrors(res.Errors); err != nil {
return err
}
checkWarnings(res.Warnings)
return nil
}
func BundleSharedScripts() (string, error) {
res := api.Build(api.BuildOptions{
EntryPoints: []string{
"injected/src/entrypoints/shared.ts",
},
Bundle: true,
Format: api.FormatIIFE,
Target: Target,
Engines: Engines,
JSXFactory: "h",
JSXFragment: "DocumentFragment",
Inject: []string{"injected/preact-shim.js"},
GlobalName: "smmShared",
Outdir: ".build",
Write: true,
})
if err := checkErrors(res.Errors); err != nil {
return "", err
}
checkWarnings(res.Warnings)
script, err := ioutil.ReadFile(res.OutputFiles[0].Path)
if err != nil {
return "", fmt.Errorf("Failed to read shared scripts output: %v", err)
}
return string(script), nil
}
//go:embed eval.template.js
var evalScriptTemplate string
// BuildEvalScriptFromFile gets a script from a file and builds an eval script with it.
func BuildEvalScriptFromFile(serverPort string, uiMode cdp.UIMode, scriptPath, steamPath, authToken, pluginsDir string) (string, error) {
scriptBytes, err := ioutil.ReadFile(scriptPath)
if err != nil {
return "", fmt.Errorf(`Failed to read injected script at "%s": %w`, scriptPath, err)
}
script := string(scriptBytes)
return BuildEvalScript(serverPort, uiMode, script, steamPath, authToken, pluginsDir)
}
// BuildEvalScript builds a script to be evaluated in the Steam target context.
func BuildEvalScript(serverPort string, uiMode cdp.UIMode, script, steamPath, authToken, pluginsDir string) (string, error) {
evalTmpl := template.Must(template.New("eval").Parse(evalScriptTemplate))
var evalScript bytes.Buffer
// Create globals struct for script execution
globals := csGlobals{
Version: VERSION,
InjectedScript: script,
ServerPort: serverPort,
UIMode: uiMode,
SteamDir: steamPath,
AuthToken: authToken,
PluginsDir: pluginsDir,
Platform: runtime.GOOS,
}
if err := evalTmpl.Execute(&evalScript, globals); err != nil {
return "", fmt.Errorf("Failed to execute eval script template: %w", err)
}
_ = ioutil.WriteFile(".build/evalScript.js", []byte(evalScript.String()), 0644)
return evalScript.String(), nil
}