From daff2f2d368c40c5cab930ab1102c290663d2106 Mon Sep 17 00:00:00 2001 From: Michael Dawson Date: Fri, 9 Aug 2024 09:49:07 -0400 Subject: [PATCH] feat: add support for mjs files when finding apps (#31) Refs: https://github.com/paketo-buildpacks/node-start/issues/225 Signed-off-by: Michael Dawson --- find_node_application.go | 3 +- find_node_application_test.go | 72 ++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/find_node_application.go b/find_node_application.go index b0c827c..681abbd 100644 --- a/find_node_application.go +++ b/find_node_application.go @@ -28,7 +28,8 @@ func FindNodeApplication(workingDir string) (string, error) { return filepath.Clean(launchpoint), nil } - files := []string{"server.js", "app.js", "main.js", "index.js"} + files := []string{"server.js", "server.mjs", "app.js", "app.mjs", + "main.js", "main.mjs", "index.js", "index.mjs"} for _, file := range files { _, err := os.Stat(filepath.Join(projectPath, file)) if err != nil { diff --git a/find_node_application_test.go b/find_node_application_test.go index 4440743..14923ea 100644 --- a/find_node_application_test.go +++ b/find_node_application_test.go @@ -33,6 +33,10 @@ func testFindNodeApplication(t *testing.T, context spec.G, it spec.S) { Expect(os.WriteFile(filepath.Join(workingDir, "app.js"), nil, 0600)).To(Succeed()) Expect(os.WriteFile(filepath.Join(workingDir, "main.js"), nil, 0600)).To(Succeed()) Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "server.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "app.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) }) it("finds the server.js application entrypoint successfully", func() { @@ -42,11 +46,32 @@ func testFindNodeApplication(t *testing.T, context spec.G, it spec.S) { }) }) + context("finds the server.mjs application entrypoint", func() { + it.Before(func() { + Expect(os.WriteFile(filepath.Join(workingDir, "app.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "server.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "app.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) + }) + + it("finds the server.js application entrypoint successfully", func() { + file, err := libnodejs.FindNodeApplication(workingDir) + Expect(err).NotTo(HaveOccurred()) + Expect(file).To(Equal(filepath.Join("server.mjs"))) + }) + }) + context("finds the app.js application entrypoint", func() { it.Before(func() { Expect(os.WriteFile(filepath.Join(workingDir, "app.js"), nil, 0600)).To(Succeed()) Expect(os.WriteFile(filepath.Join(workingDir, "main.js"), nil, 0600)).To(Succeed()) Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "app.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) }) it("finds the app.js application entrypoint successfully", func() { @@ -56,10 +81,28 @@ func testFindNodeApplication(t *testing.T, context spec.G, it spec.S) { }) }) + context("finds the app.mjs application entrypoint", func() { + it.Before(func() { + Expect(os.WriteFile(filepath.Join(workingDir, "main.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "app.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) + }) + + it("finds the app.js application entrypoint successfully", func() { + file, err := libnodejs.FindNodeApplication(workingDir) + Expect(err).NotTo(HaveOccurred()) + Expect(file).To(Equal(filepath.Join("app.mjs"))) + }) + }) + context("finds the main.js application entrypoint", func() { it.Before(func() { Expect(os.WriteFile(filepath.Join(workingDir, "main.js"), nil, 0600)).To(Succeed()) Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) }) it("finds the main.js application entrypoint successfully", func() { @@ -69,9 +112,24 @@ func testFindNodeApplication(t *testing.T, context spec.G, it spec.S) { }) }) + context("finds the main.mjs application entrypoint", func() { + it.Before(func() { + Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "main.mjs"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) + }) + + it("finds the main.js application entrypoint successfully", func() { + file, err := libnodejs.FindNodeApplication(workingDir) + Expect(err).NotTo(HaveOccurred()) + Expect(file).To(Equal(filepath.Join("main.mjs"))) + }) + }) + context("finds the index.js application entrypoint", func() { it.Before(func() { Expect(os.WriteFile(filepath.Join(workingDir, "index.js"), nil, 0600)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) }) it("finds the index.js application entrypoint", func() { @@ -81,6 +139,18 @@ func testFindNodeApplication(t *testing.T, context spec.G, it spec.S) { }) }) + context("finds the index.mjs application entrypoint", func() { + it.Before(func() { + Expect(os.WriteFile(filepath.Join(workingDir, "index.mjs"), nil, 0600)).To(Succeed()) + }) + + it("finds the index.js application entrypoint", func() { + file, err := libnodejs.FindNodeApplication(workingDir) + Expect(err).NotTo(HaveOccurred()) + Expect(file).To(Equal(filepath.Join("index.mjs"))) + }) + }) + context("when there is a launchpoint", func() { it.Before(func() { Expect(os.Mkdir(filepath.Join(workingDir, "src"), os.ModePerm)).To(Succeed()) @@ -132,7 +202,7 @@ func testFindNodeApplication(t *testing.T, context spec.G, it spec.S) { context("when no application can be found", func() { it("returns that application could not be found", func() { _, err := libnodejs.FindNodeApplication(workingDir) - Expect(err).To(MatchError(fmt.Errorf("could not find app in %s: expected one of server.js | app.js | main.js | index.js", workingDir))) + Expect(err).To(MatchError(fmt.Errorf("could not find app in %s: expected one of server.js | server.mjs | app.js | app.mjs | main.js | main.mjs | index.js | index.mjs", workingDir))) }) })