Skip to content

Commit

Permalink
feat: add support for mjs files when finding apps (#31)
Browse files Browse the repository at this point in the history
Refs: paketo-buildpacks/node-start#225

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson authored Aug 9, 2024
1 parent c6e737a commit daff2f2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion find_node_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
72 changes: 71 additions & 1 deletion find_node_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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())
Expand Down Expand Up @@ -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)))
})
})

Expand Down

0 comments on commit daff2f2

Please sign in to comment.