Skip to content

Commit

Permalink
Merge pull request #15 from paketo-buildpacks/no-package-json
Browse files Browse the repository at this point in the history
Support No package.json
  • Loading branch information
ekcasey authored Oct 16, 2020
2 parents 9c09826 + 0e02444 commit 2bd382b
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 2bd382b

Please sign in to comment.