Skip to content

Commit

Permalink
Add function to get version from package.json (#10)
Browse files Browse the repository at this point in the history
* Add function to get version from package.json

package_json_parser needs to parse version to be
re-used in npm-install buildpack.

Signed-off-by: Michael Dawson <[email protected]>

* squash: add cleanup of workingDir

Signed-off-by: Michael Dawson <[email protected]>

---------

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson authored Jun 29, 2023
1 parent 4b5928b commit 8bac97c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

// PackageJSON represents the contents of a package.json file.
type PackageJSON struct {
Engines struct {
Node string `json:"node"`
} `json:"engines"`
Scripts struct {
PostStart string `json:"poststart"`
PreStart string `json:"prestart"`
Expand Down Expand Up @@ -38,3 +41,7 @@ func ParsePackageJSON(path string) (PackageJSON, error) {
func (pj PackageJSON) HasStartScript() bool {
return pj.Scripts.Start != ""
}

func (pj PackageJSON) GetVersion() string {
return pj.Engines.Node
}
50 changes: 50 additions & 0 deletions package_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {
}`), 0600)).To(Succeed())
})

it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})

context("when parsing a valid package.json with start scripts", func() {
it("successfully extracts the scripts information", func() {
pkg, err := libnodejs.ParsePackageJSON(path)
Expand Down Expand Up @@ -88,4 +92,50 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {
})
})
})

context("ParseVersion", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{
"engines": {
"node": "1.2.3"
}
}`), 0600)).To(Succeed())
})

it("parses the node engine version from a package.json file", func() {
pkg, err := libnodejs.ParsePackageJSON(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(pkg.GetVersion()).To(Equal("1.2.3"))
})

context("Engines, but no Node version", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{
"engines": {
}
}`), 0600)).To(Succeed())
})

it("parses the node engine version from a package.json file when no version is specified", func() {

pkg, err := libnodejs.ParsePackageJSON(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(pkg.GetVersion()).To(Equal(""))
})
})

context("No Engines", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{
}`), 0600)).To(Succeed())
})

it("parses the node engine version from a package.json file when no version is specified", func() {

pkg, err := libnodejs.ParsePackageJSON(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(pkg.GetVersion()).To(Equal(""))
})
})
})
}

0 comments on commit 8bac97c

Please sign in to comment.