Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
Handle More Node Cases
Browse files Browse the repository at this point in the history
Previously, this implementation handle the straight forward riff.toml with a
.js file listed case, but it did not handle the implicit cases where a
package.json file existed, but no artifact was explicitly defined.  This
change updates the implementation to include that functionality.

Signed-off-by: Ben Hale <[email protected]>
  • Loading branch information
nebhale committed May 27, 2020
1 parent 85eb152 commit 83dbccc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
2 changes: 2 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ id = "org.cloudfoundry.stacks.cflinuxfs3"
[[metadata.configurations]]
name = "RIFF"
description = "whether this is a riff function without a riff.toml file"
build = true

[[metadata.configurations]]
name = "RIFF_ARTIFACT"
description = "the artifact to invoke"
build = true

[[metadata.dependencies]]
id = "invoker"
Expand Down
28 changes: 21 additions & 7 deletions node/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package node

import (
"fmt"
"os"
"path/filepath"

"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak"
Expand All @@ -35,8 +37,7 @@ func (Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error)
{Name: "riff-node"},
},
Requires: []libcnb.BuildPlanRequire{
{Name: "node"},
{Name: "node_modules", Metadata: map[string]interface{}{"build": true, "launch": true}},
{Name: "node", Metadata: map[string]interface{}{"build": true}},
{Name: "streaming-http-adapter"},
},
},
Expand All @@ -50,12 +51,25 @@ func (Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error)

if ok, err := libfnbuildpack.IsRiff(context.Application.Path, cr); err != nil {
return libcnb.DetectResult{}, fmt.Errorf("unable to determine if application is riff\n%w", err)
} else if ok {
metadata, err := libfnbuildpack.Metadata(context.Application.Path, cr)
if err != nil {
return libcnb.DetectResult{}, fmt.Errorf("uanble to read riff metadata\n%w", err)
}
} else if !ok {
return result, nil
}

metadata, err := libfnbuildpack.Metadata(context.Application.Path, cr)
if err != nil {
return libcnb.DetectResult{}, fmt.Errorf("unable to read riff metadata\n%w", err)
}

if s, ok := metadata["artifact"].(string); ok && filepath.Ext(s) == ".js" {
result.Plans[0].Requires = append(result.Plans[0].Requires, libcnb.BuildPlanRequire{
Name: "riff-node",
Metadata: metadata,
})
}

if _, err := os.Stat(filepath.Join(context.Application.Path, "package.json")); err != nil && !os.IsNotExist(err) {
return libcnb.DetectResult{}, fmt.Errorf("unable to stat package.json\n%w", err)
} else if err == nil {
result.Plans[0].Requires = append(result.Plans[0].Requires, libcnb.BuildPlanRequire{
Name: "riff-node",
Metadata: metadata,
Expand Down
52 changes: 46 additions & 6 deletions node/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
{Name: "riff-node"},
},
Requires: []libcnb.BuildPlanRequire{
{Name: "node"},
{Name: "node_modules", Metadata: map[string]interface{}{"build": true, "launch": true}},
{Name: "node", Metadata: map[string]interface{}{"build": true}},
{Name: "streaming-http-adapter"},
},
},
},
}))
})

it("passes with with riff.toml", func() {
it("passes with riff.toml and non-.js artifact", func() {
Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "riff.toml"), []byte(`
artifact = "test-artifact"
`), 0644))
Expand All @@ -78,10 +77,51 @@ artifact = "test-artifact"
{Name: "riff-node"},
},
Requires: []libcnb.BuildPlanRequire{
{Name: "node"},
{Name: "node_modules", Metadata: map[string]interface{}{"build": true, "launch": true}},
{Name: "node", Metadata: map[string]interface{}{"build": true}},
{Name: "streaming-http-adapter"},
{Name: "riff-node", Metadata: map[string]interface{}{"artifact": "test-artifact"}},
},
},
},
}))
})

it("passes with with riff.toml and .js artifact", func() {
Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "riff.toml"), []byte(`
artifact = "test-artifact.js"
`), 0644))

Expect(detect.Detect(ctx)).To(Equal(libcnb.DetectResult{
Pass: true,
Plans: []libcnb.BuildPlan{
{
Provides: []libcnb.BuildPlanProvide{
{Name: "riff-node"},
},
Requires: []libcnb.BuildPlanRequire{
{Name: "node", Metadata: map[string]interface{}{"build": true}},
{Name: "streaming-http-adapter"},
{Name: "riff-node", Metadata: map[string]interface{}{"artifact": "test-artifact.js"}},
},
},
},
}))
})

it("passes with with riff.toml and package.json", func() {
Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "riff.toml"), []byte{}, 0644))
Expect(ioutil.WriteFile(filepath.Join(ctx.Application.Path, "package.json"), []byte{}, 0644))

Expect(detect.Detect(ctx)).To(Equal(libcnb.DetectResult{
Pass: true,
Plans: []libcnb.BuildPlan{
{
Provides: []libcnb.BuildPlanProvide{
{Name: "riff-node"},
},
Requires: []libcnb.BuildPlanRequire{
{Name: "node", Metadata: map[string]interface{}{"build": true}},
{Name: "streaming-http-adapter"},
{Name: "riff-node", Metadata: map[string]interface{}{}},
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion node/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/bard"
"github.com/projectriff/libfnbuildpack"
)

type Function struct {
Expand All @@ -32,7 +33,7 @@ type Function struct {

func NewFunction(applicationPath string, artifactPath string) (Function, error) {
return Function{
LayerContributor: libpak.NewLayerContributor(bard.FormatIdentity("NodeJS", artifactPath),
LayerContributor: libpak.NewLayerContributor(libfnbuildpack.FormatFunction("NodeJS", artifactPath),
map[string]interface{}{"artifact": artifactPath}),
Path: filepath.Join(applicationPath, artifactPath),
}, nil
Expand Down

0 comments on commit 83dbccc

Please sign in to comment.