Skip to content

Commit

Permalink
Request cpython if there is a node-gyp dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d1ngm0nk3y authored and mhdawson committed Sep 16, 2024
1 parent 73106bf commit 3a8ea2c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
NodeModules = "node_modules"
Node = "node"
Npm = "npm"
Cpython = "cpython"

LayerNameNodeModules = "modules"
LayerNameCache = "npm-cache"
Expand Down
35 changes: 27 additions & 8 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func Detect() packit.DetectFunc {
}
version := pkg.GetVersion()

_, nodeGypInDep := pkg.Dependencies["node-gyp"]
_, nodeGypInDevDep := pkg.DevDependencies["node-gyp"]
pythonNeeded := nodeGypInDep || nodeGypInDevDep

nodeDependency := packit.BuildPlanRequirement{
Name: Node,
Metadata: BuildPlanMetadata{
Expand All @@ -54,21 +58,36 @@ func Detect() packit.DetectFunc {
}
}

return packit.DetectResult{
npmDependency := packit.BuildPlanRequirement{
Name: Npm,
Metadata: BuildPlanMetadata{
Build: true,
},
}

cPythonDependency := packit.BuildPlanRequirement{
Name: Cpython,
Metadata: BuildPlanMetadata{
Build: true,
},
}

result := packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: NodeModules},
},
Requires: []packit.BuildPlanRequirement{
nodeDependency,
{
Name: Npm,
Metadata: BuildPlanMetadata{
Build: true,
},
},
npmDependency,
},
},
}, nil
}

if pythonNeeded {
result.Plan.Requires = append(result.Plan.Requires, cPythonDependency)
}

return result, nil
}
}
41 changes: 41 additions & 0 deletions detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,47 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
}))

})
context("when the package.json declares a node-gyp dependency", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{
"dependencies": {
"node-gyp": "1.2.3"
}
}`), 0600)).To(Succeed())
})

it("returns a plan requesting cpython", func() {
result, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).NotTo(HaveOccurred())
Expect(result.Plan).To(Equal(packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: npminstall.NodeModules},
},
Requires: []packit.BuildPlanRequirement{
{
Name: npminstall.Node,
Metadata: npminstall.BuildPlanMetadata{
Build: true,
},
},
{
Name: npminstall.Npm,
Metadata: npminstall.BuildPlanMetadata{
Build: true,
},
},
{
Name: npminstall.Cpython,
Metadata: npminstall.BuildPlanMetadata{
Build: true,
},
},
},
}))
})
})

context("when the package.json does not declare a node engine version", func() {
it.Before(func() {
Expand Down

0 comments on commit 3a8ea2c

Please sign in to comment.