Skip to content

Commit

Permalink
Allow custom version of npm
Browse files Browse the repository at this point in the history
Co-authored-by: Ralf Pannemans <[email protected]>
  • Loading branch information
nicolasbender and c0d1ngm0nk3y committed May 17, 2024
1 parent 5a6c88e commit 8b49ded
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ file that looks like the following:
launch = true
```

## Configuration

| Environment Variable | Description |
|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. |

## Usage

To package this buildpack for consumption:
Expand Down
19 changes: 19 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package npminstall

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

"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/sbom"

"github.com/paketo-buildpacks/packit/v2"
Expand Down Expand Up @@ -71,6 +74,22 @@ func Build(entryResolver EntryResolver,
if err != nil {
return packit.BuildResult{}, err
}
logger.Process("Checking custom npm version")
npmVersion, found := environment.Lookup("BP_NPM_VERSION")
if found {
args := []string{"install", "-g", fmt.Sprintf("npm@%s", npmVersion)}
logger.Subprocess("Running 'npm %s'", strings.Join(args, " "))

executable := pexec.NewExecutable("npm")
err = executable.Execute(pexec.Execution{
Args: args,
Stdout: logger.ActionWriter,
Stderr: logger.ActionWriter,
})
if err != nil {
return packit.BuildResult{}, fmt.Errorf("npm update failed: %w", err)
}
}

logger.Process("Resolving installation process")

Expand Down
5 changes: 5 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ api = "0.7"

[buildpack]
homepage = "https://github.com/paketo-buildpacks/npm-install"
version = "1.1.1"
id = "paketo-buildpacks/npm-install"
name = "Paketo Buildpack for NPM Install"
sbom-formats = ["application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"]
Expand All @@ -28,6 +29,10 @@ api = "0.7"
name = "BP_NODE_PROJECT_PATH"
description = "path to the root of the Node.js project"

[[metadata.configurations]]
name = "BP_NPM_VERSION"
description = "configures a custom npm version"

[[metadata.configurations]]
name = "NODE_HOME"
description = "path the Node.js installation"
Expand Down

0 comments on commit 8b49ded

Please sign in to comment.