Skip to content

Commit

Permalink
feat: add support for mjs and cjs files (#570)
Browse files Browse the repository at this point in the history
Fixes: #225

- add support for mjs and cjs files when looking for applications

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson authored Sep 2, 2024
1 parent b165381 commit 81ed87b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
59 changes: 58 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,63 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(buffer.String()).To(ContainSubstring("node server.js"))
})

context("sniff test that files that end something other than js work", func() {
it.Before(func() {
layersDir = t.TempDir()
cnbDir = t.TempDir()
workingDir = t.TempDir()

Expect(os.WriteFile(filepath.Join(workingDir, "app.mjs"), nil, 0600)).To(Succeed())

reloader = &fakes.Reloader{}

buffer = bytes.NewBuffer(nil)
logger := scribe.NewEmitter(buffer)

buildContext = packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Stack: "some-stack",
BuildpackInfo: packit.BuildpackInfo{
Name: "Some Buildpack",
Version: "some-version",
},
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{},
},
Layers: packit.Layers{Path: layersDir},
}
build = nodestart.Build(logger, reloader)
})

it("returns a result that provides a node start command", func() {
result, err := build(buildContext)
Expect(err).NotTo(HaveOccurred())

Expect(result).To(Equal(packit.BuildResult{
Plan: packit.BuildpackPlan{
Entries: nil,
},
Layers: nil,
Launch: packit.LaunchMetadata{
Processes: []packit.Process{
{
Type: "web",
Command: "node",
Args: []string{"app.mjs"},
Default: true,
Direct: true,
},
},
},
}))

Expect(buffer.String()).To(ContainSubstring("Some Buildpack some-version"))
Expect(buffer.String()).To(ContainSubstring("Assigning launch processes"))
Expect(buffer.String()).To(ContainSubstring("node app.mjs"))
})
})

context("when live reload is enabled", func() {
it.Before(func() {
reloader.ShouldEnableLiveReloadCall.Returns.Bool = true
Expand Down Expand Up @@ -141,7 +198,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

it("returns an error", func() {
_, err := build(buildContext)
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.cjs | server.mjs | app.js | app.cjs | app.mjs | main.js | main.cjs | main.mjs | index.js | index.cjs | index.mjs", workingDir)))
})
})

Expand Down
27 changes: 26 additions & 1 deletion detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
})
}, spec.Sequential())

context("sniff test of when an application is detected in the working dir and does not have a js extension", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(workingDir, "src"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, "server.mjs"), nil, 0600)).To(Succeed())
})

it("detects", func() {
result, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Plan).To(Equal(packit.BuildPlan{
Requires: []packit.BuildPlanRequirement{
{
Name: "node",
Metadata: map[string]interface{}{
"launch": true,
},
},
},
}))
})

})

context("when a package.json is detected in the working dir", func() {
it.Before(func() {
t.Setenv("BP_NODE_PROJECT_PATH", "./src")
Expand Down Expand Up @@ -122,7 +147,7 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
_, err := detect(packit.DetectContext{
WorkingDir: 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.cjs | server.mjs | app.js | app.cjs | app.mjs | main.js | main.cjs | main.mjs | index.js | index.cjs | index.mjs", workingDir)))
})
})

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
require (
github.com/BurntSushi/toml v1.4.0
github.com/onsi/gomega v1.34.1
github.com/paketo-buildpacks/libnodejs v0.2.0
github.com/paketo-buildpacks/libnodejs v0.3.0
github.com/paketo-buildpacks/libreload-packit v0.0.1
github.com/paketo-buildpacks/occam v0.18.7
github.com/paketo-buildpacks/packit/v2 v2.14.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,8 @@ github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/paketo-buildpacks/libnodejs v0.2.0 h1:y7G6BQIeeMUnIlzREiAipQlAcaXJ82VMPldkTTiv9o4=
github.com/paketo-buildpacks/libnodejs v0.2.0/go.mod h1:JLs6tu5TD6la8p1IKo3UYO1C93/ywGesa74FpM82wPw=
github.com/paketo-buildpacks/libnodejs v0.3.0 h1:vh2fDuUt/XPPy0ENY3mmRHlb127wVD8d782liVKa9WM=
github.com/paketo-buildpacks/libnodejs v0.3.0/go.mod h1:V8bTCbjjpthcdrDK2g2KL7c0mHQeZJAfN+hQtPpmGuQ=
github.com/paketo-buildpacks/libreload-packit v0.0.1 h1:K1HhNAqBSzRpefwGOcvdchZwyeNTgNJL9SC7V4paYt8=
github.com/paketo-buildpacks/libreload-packit v0.0.1/go.mod h1:ZWE3U94Z18yJk8Pc1mP852l9phQsrsZ+An2U98g0rWw=
github.com/paketo-buildpacks/occam v0.18.7 h1:L5tl/JnzhSKecJ78ggR4SBz9AHqoVKCBJ0EoT8t6y84=
Expand Down

0 comments on commit 81ed87b

Please sign in to comment.