Skip to content

Commit 86994d4

Browse files
committed
Implement update command for typescript node clients
1 parent ec08832 commit 86994d4

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func Build() error {
11+
panic("not implemented")
1112
config, err := cgfile.LoadCodeGameFile("")
1213
if err != nil {
1314
return err

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func main() {
3636
os.Exit(1)
3737
}
3838
case "update":
39-
err = Update()
39+
err = Update(projectName)
4040
case "run":
4141
err = Run()
4242
case "build":

new_client.go

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

33
import (
44
_ "embed"
5+
"os"
56
"path/filepath"
67
"strings"
78

@@ -117,7 +118,12 @@ func createClientTemplate(projectName string, info server.GameInfo, eventNames,
117118

118119
func execClientTemplate(projectName string, info server.GameInfo, eventNames, commandNames []string, node, typescript, update bool) error {
119120
if update {
120-
panic("not implemented")
121+
cli.Warn("This action will ERASE and regenerate ALL files in '%s/'.\nYou will have to manually update your code to work with the new version.", info.Name)
122+
ok, err := cli.YesNo("Continue?", false)
123+
if err != nil || !ok {
124+
return cli.ErrCanceled
125+
}
126+
os.RemoveAll(info.Name)
121127
} else {
122128
cli.Warn("DO NOT EDIT the `%s/` directory inside of the project. ALL CHANGES WILL BE LOST when running `codegame update`.", info.Name)
123129
}

templates/new/client/game.ts.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export class Game {
278278
{{ range .Commands }}
279279
/**
280280
* Sends a `{{.Name}}` command.
281-
* @param data Optional options to go along with your event.
281+
* @param data Optional options to go along with your command.
282282
*/
283283
public send{{.PascalName}}(data: {{.PascalName}}Cmd['data']) {
284284
return this.socket.send<{{.PascalName}}Cmd>('{{.Name}}', data);

update.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55

66
"github.com/Bananenpro/cli"
77
"github.com/code-game-project/go-utils/cgfile"
8+
"github.com/code-game-project/go-utils/cggenevents"
9+
"github.com/code-game-project/go-utils/exec"
810
"github.com/code-game-project/go-utils/modules"
911
"github.com/code-game-project/go-utils/server"
1012
)
1113

12-
func Update() error {
14+
func Update(projectName string) error {
1315
config, err := cgfile.LoadCodeGameFile("")
1416
if err != nil {
1517
return err
@@ -21,23 +23,62 @@ func Update() error {
2123
}
2224
switch config.Type {
2325
case "client":
24-
return updateClient(data.LibraryVersion, config)
26+
return updateClient(projectName, data.LibraryVersion, config)
2527
case "server":
2628
return updateServer(data.LibraryVersion)
2729
default:
2830
return fmt.Errorf("Unknown project type: %s", config.Type)
2931
}
3032
}
3133

32-
func updateClient(libraryVersion string, config *cgfile.CodeGameFileData) error {
34+
func updateClient(projectName, libraryVersion string, config *cgfile.CodeGameFileData) error {
35+
api, err := server.NewAPI(config.URL)
36+
if err != nil {
37+
return err
38+
}
39+
info, err := api.FetchGameInfo()
40+
if err != nil {
41+
return err
42+
}
43+
44+
cge, err := api.GetCGEFile()
45+
if err != nil {
46+
return err
47+
}
48+
cgeVersion, err := cggenevents.ParseCGEVersion(cge)
49+
if err != nil {
50+
return err
51+
}
52+
53+
eventNames, commandNames, err := cggenevents.GetEventNames(api.BaseURL(), cgeVersion)
54+
if err != nil {
55+
return err
56+
}
57+
58+
err = updateClientTemplate(projectName, info, eventNames, commandNames, config.LangConfig["runtime"] == "node", config.Lang == "ts")
59+
if err != nil {
60+
return err
61+
}
62+
63+
cli.BeginLoading("Updating dependencies...")
64+
_, err = exec.Execute(true, "npm", "install", "@code-game-project/client"+"@"+libraryVersion)
65+
if err != nil {
66+
return err
67+
}
68+
_, err = exec.Execute(true, "npm", "update")
69+
if err != nil {
70+
return err
71+
}
72+
cli.FinishLoading()
3373
return nil
3474
}
3575

36-
func updateClientTemplate() error {
37-
return execClientTemplate("", server.GameInfo{}, nil, nil, false, false, true)
76+
func updateClientTemplate(projectName string, info server.GameInfo, eventNames, commandNames []string, node, typescript bool) error {
77+
return execClientTemplate(projectName, info, eventNames, commandNames, node, typescript, true)
3878
}
3979

4080
func updateServer(libraryVersion string) error {
81+
panic("not implemented")
4182
cli.Warn("This update might include breaking changes. You will have to manually update your code to work with the new version.")
4283
ok, err := cli.YesNo("Continue?", false)
4384
if err != nil || !ok {

0 commit comments

Comments
 (0)