diff --git a/go/buildstep/launchers.go b/go/buildstep/launchers.go new file mode 100644 index 0000000..4b59f5c --- /dev/null +++ b/go/buildstep/launchers.go @@ -0,0 +1,24 @@ +package buildstep + +import "strings" + +// LauncherSuffix is what a TypeScript entrypoint's launcher is called beside it: index.js is started as +// index.serve.mjs (novox/hq ADR 0193), an ES module by its own extension whatever the bundle's package.json says. +const LauncherSuffix = ".serve.mjs" + +// Launchers are the launchers a bundle's build writes, by entrypoint, relative to the bundle's root: for TypeScript +// every entrypoint `x.js` is started by `x.serve.mjs` beside it; an entrypoint that is not JavaScript and every other +// language, whose build is already executable, get none. The one rule mesh-build-node, build-agent and the controller +// share (R4e), so the name a build writes is the name a reader expects. +func Launchers(language string, entrypoints []string) map[string]string { + out := map[string]string{} + if language != "typescript" { + return out + } + for _, entry := range entrypoints { + if strings.HasSuffix(entry, ".js") { + out[entry] = strings.TrimSuffix(entry, ".js") + LauncherSuffix + } + } + return out +} diff --git a/go/buildstep/launchers_test.go b/go/buildstep/launchers_test.go new file mode 100644 index 0000000..ddea9a0 --- /dev/null +++ b/go/buildstep/launchers_test.go @@ -0,0 +1,22 @@ +package buildstep + +import ( + "reflect" + "testing" +) + +// A TypeScript bundle's entrypoint x.js is started as x.serve.mjs beside it (novox/hq ADR 0193), the rule mesh-build-node +// and build-agent both apply; an entrypoint that is not .js gets none, and another language gets none. +func TestATypeScriptEntrypointIsStartedByTheLauncherBesideIt(t *testing.T) { + got := Launchers("typescript", []string{"index.js", "tools/x.js", "data.json"}) + want := map[string]string{"index.js": "index.serve.mjs", "tools/x.js": "tools/x.serve.mjs"} + if !reflect.DeepEqual(got, want) { + t.Fatalf("Launchers(typescript) = %v, want %v", got, want) + } + if got := Launchers("go", []string{"index.js"}); len(got) != 0 { + t.Fatalf("a Go bundle got launchers: %v", got) + } + if got := Launchers("typescript", nil); len(got) != 0 { + t.Fatalf("a bundle with no entrypoint got launchers: %v", got) + } +}