Skip to content

Commit 6c37494

Browse files
committed
Implement client commands for JS with Node
1 parent 86994d4 commit 6c37494

File tree

13 files changed

+365
-29
lines changed

13 files changed

+365
-29
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
module github.com/code-game-project/codegame-cli-go
1+
module github.com/code-game-project/codegame-cli-js
22

33
go 1.18
44

55
require (
66
github.com/Bananenpro/cli v0.2.2
7-
github.com/code-game-project/go-utils v0.2.8
7+
github.com/code-game-project/go-utils v0.2.9
88
)
99

1010
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
88
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
99
github.com/code-game-project/go-utils v0.2.8 h1:f6ZvORWmlf8uZnCSAhLJ23MdA5dGvcAPkijTLfbv+B4=
1010
github.com/code-game-project/go-utils v0.2.8/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
11+
github.com/code-game-project/go-utils v0.2.9 h1:ezaxxLBAQRwFnIXso+zKPL7AJnuAMIcMwrhLruoyOa8=
12+
github.com/code-game-project/go-utils v0.2.9/go.mod h1:kQ6kH9XDzdM2pnJUI1lw61Gp8XOams/E2dKABa1mBI8=
1113
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
1214
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
1315
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

new_client.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ import (
1717
//go:embed templates/new/client/package.json.tmpl
1818
var clientPackageJSONTemplate string
1919

20-
//go:embed templates/new/client/index.js.tmpl
20+
//go:embed templates/new/client/js/app.js.tmpl
2121
var clientJSIndexTemplate string
2222

23-
//go:embed templates/new/client/game.js.tmpl
23+
//go:embed templates/new/client/js/game.js.tmpl
2424
var clientJSGameTemplate string
2525

26-
//go:embed templates/new/client/tsconfig.json.tmpl
26+
//go:embed templates/new/client/ts/tsconfig.json.tmpl
2727
var clientTSConfigTemplate string
2828

29-
//go:embed templates/new/client/index.ts.tmpl
29+
//go:embed templates/new/client/ts/index.ts.tmpl
3030
var clientTSIndexTemplate string
3131

32-
//go:embed templates/new/client/game.ts.tmpl
32+
//go:embed templates/new/client/ts/game.ts.tmpl
3333
var clientTSGameTemplate string
3434

35+
//go:embed templates/new/client/js/index.html.tmpl
36+
var clientIndexHTMLTemplate string
37+
3538
func CreateNewClient(projectName string) error {
3639
data, err := modules.ReadCommandConfig[modules.NewClientData]()
3740
if err != nil {
@@ -102,9 +105,11 @@ func CreateNewClient(projectName string) error {
102105
if err != nil {
103106
return err
104107
}
105-
_, err = exec.Execute(true, "npm", "install", "--save-dev", "typescript", "@types/node")
106-
if err != nil {
107-
return err
108+
if typescript {
109+
_, err = exec.Execute(true, "npm", "install", "--save-dev", "typescript", "@types/node")
110+
if err != nil {
111+
return err
112+
}
108113
}
109114
cli.FinishLoading()
110115
}
@@ -190,15 +195,21 @@ func execClientTemplate(projectName string, info server.GameInfo, eventNames, co
190195
}
191196
} else {
192197
if !update {
193-
err := ExecTemplate(clientJSIndexTemplate, "src/index.js", data)
198+
err := ExecTemplate(clientJSIndexTemplate, "app.js", data)
194199
if err != nil {
195200
return err
196201
}
197202
}
198-
err := ExecTemplate(clientJSGameTemplate, filepath.Join("src", info.Name, "game.js"), data)
203+
err := ExecTemplate(clientJSGameTemplate, filepath.Join(info.Name, "game.js"), data)
199204
if err != nil {
200205
return err
201206
}
207+
if !node {
208+
err := ExecTemplate(clientIndexHTMLTemplate, "index.html", data)
209+
if err != nil {
210+
return err
211+
}
212+
}
202213
}
203214

204215
if !update && node {

run.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Run() error {
2727
runtime := config.LangConfig["runtime"]
2828
node := runtime == "node"
2929

30-
if !typescript || !node {
30+
if !node {
3131
panic("not implemented")
3232
}
3333

@@ -51,7 +51,12 @@ func runClient(url string, args []string, typescript, node bool) error {
5151
}
5252
}
5353

54-
cmdArgs := []string{"dist/index.js"}
54+
path := "app.js"
55+
if typescript {
56+
path = "dist/index.js"
57+
}
58+
59+
cmdArgs := []string{path}
5560
cmdArgs = append(cmdArgs, args...)
5661

5762
env := []string{"CG_GAME_URL=" + url}

templates/new/client/game.js.tmpl

Lines changed: 0 additions & 8 deletions
This file was deleted.

templates/new/client/index.js.tmpl

Lines changed: 0 additions & 1 deletion
This file was deleted.

templates/new/client/js/app.js.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Game, Verbosity } from './{{.GameName}}/game.js';
2+
3+
const { game } = await Game.fromArgv({}, Verbosity.DEBUG);

0 commit comments

Comments
 (0)