Skip to content

Commit

Permalink
Support No package.json
Browse files Browse the repository at this point in the history
Previously the NodeJSMainModule function would return an error if a NodeJS
application did not have a package.json file.  This was incorrect as it is
possible to have a legal NodeJS application without one and the Paketo NodeJS
buildpack supports this style of application.

This change updates the detection logic to return server.js in cases where
there is no package.json or the package.json does not explicitly identify a
main, in line with the behavior of the Paketo NodeJS buildpack.

Signed-off-by: Ben Hale <bhale@vmware.com>
  • Loading branch information
nebhale committed Oct 16, 2020
1 parent 9c09826 commit 0e02444
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
10 changes: 7 additions & 3 deletions sherpa/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import (
"path/filepath"
)

// NodeJSMainModule returns the name of the main module as defined in <path>/package.json.
// NodeJSMainModule returns the name of the main module as defined in <path>/package.json. If no package.json exists,
// or the package.json does not include a main entry, value defaults to server.js in line with the behavior of the
// Paketo NodeJS buildpack.
func NodeJSMainModule(path string) (string, error) {
file := filepath.Join(path, "package.json")
in, err := os.Open(file)
if err != nil {
if os.IsNotExist(err) {
return "server.js", nil
} else if err != nil {
return "", fmt.Errorf("unable to open %s\n%w", file, err)
}
defer in.Close()
Expand All @@ -39,7 +43,7 @@ func NodeJSMainModule(path string) (string, error) {

m, ok := raw["main"].(string)
if !ok {
return "", fmt.Errorf("no main module defined in %s: %+v", file, raw)
return "server.js", nil
}

return m, nil
Expand Down
24 changes: 12 additions & 12 deletions sherpa/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package sherpa_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -47,19 +46,20 @@ func testNodeJS(t *testing.T, context spec.G, it spec.S) {
Expect(os.RemoveAll(path)).To(Succeed())
})

context("NodeJSMainModule", func() {
it("returns main module", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{ "main": "test-main" }`), 0644)).
To(Succeed())
it("returns server.js if no package.json exists", func() {
Expect(sherpa.NodeJSMainModule(path)).To(Equal("server.js"))
})

it("returns server.js if package.json does not have a main entry", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{}`), 0644)).To(Succeed())

Expect(sherpa.NodeJSMainModule(path)).To(Equal("test-main"))
})
Expect(sherpa.NodeJSMainModule(path)).To(Equal("server.js"))
})

it("returns error if no main module defined", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{}`), 0644)).To(Succeed())
it("returns main module", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "package.json"), []byte(`{ "main": "test-main" }`), 0644)).
To(Succeed())

_, err := sherpa.NodeJSMainModule(path)
Expect(err).To(MatchError(fmt.Errorf("no main module defined in %s: map[]", filepath.Join(path, "package.json"))))
})
Expect(sherpa.NodeJSMainModule(path)).To(Equal("test-main"))
})
}

0 comments on commit 0e02444

Please sign in to comment.