Skip to content

Commit 94bd5ff

Browse files
committed
Include manually installed dependencies in browser build output
1 parent 771274e commit 94bd5ff

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

build.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -93,9 +94,13 @@ func buildClient(gameName, output, url string, typescript bool, runtime string)
9394
return err
9495
}
9596
} else if runtime == "browser" {
97+
dependencies, err := npmDependencies()
98+
if err != nil {
99+
return err
100+
}
96101
err = cp.Copy(".", output, cp.Options{
97102
Skip: func(src string) (bool, error) {
98-
return src == filepath.Clean(output) || (src != "node_modules" && strings.HasPrefix(src, "node_modules") && !strings.Contains(src, "@code-game-project")) || src == ".codegame.json" || src == "package.json" || src == "package-lock.json", nil
103+
return src == filepath.Clean(output) || (src != "node_modules" && strings.HasPrefix(src, "node_modules") && !containsAny(src, dependencies)) || src == ".codegame.json" || src == "package.json" || src == "package-lock.json", nil
99104
},
100105
})
101106
if err != nil {
@@ -118,6 +123,36 @@ func buildClient(gameName, output, url string, typescript bool, runtime string)
118123
return nil
119124
}
120125

126+
func npmDependencies() ([]string, error) {
127+
type pkgJSON struct {
128+
Dependencies map[string]string `json:"dependencies"`
129+
}
130+
file, err := os.Open("package.json")
131+
if err != nil {
132+
return nil, fmt.Errorf("failed to open package.json: %w", err)
133+
}
134+
defer file.Close()
135+
var data pkgJSON
136+
err = json.NewDecoder(file).Decode(&data)
137+
if err != nil {
138+
return nil, fmt.Errorf("failed to parse package.json: %w", err)
139+
}
140+
dependencies := make([]string, 0, len(data.Dependencies))
141+
for dep := range data.Dependencies {
142+
dependencies = append(dependencies, strings.Split(dep, "/")[0])
143+
}
144+
return dependencies, nil
145+
}
146+
147+
func containsAny(s string, substrs []string) bool {
148+
for _, sub := range substrs {
149+
if strings.Contains(s, sub) {
150+
return true
151+
}
152+
}
153+
return false
154+
}
155+
121156
func replaceInFile(filename, old, new string) error {
122157
content, err := os.ReadFile(filename)
123158
if err != nil {

new_client.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ func CreateNewClient(projectName string) error {
103103
cli.FinishLoading()
104104

105105
cli.BeginLoading("Installing dependencies...")
106-
_, err = exec.Execute(true, "npm", "install", "commander")
107-
if err != nil {
108-
return err
106+
if runtime == "node" {
107+
_, err = exec.Execute(true, "npm", "install", "commander")
108+
if err != nil {
109+
return err
110+
}
109111
}
110112
if typescript {
111113
_, err = exec.Execute(true, "npm", "install", "--save-dev", "typescript", "@types/node")

0 commit comments

Comments
 (0)