Skip to content

Commit

Permalink
Introduce BP_KEEP_NODE_BUILD_CACHE to safeguard existing behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d1ngm0nk3y committed Sep 27, 2024
1 parent fdf1e9b commit d2ee774
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ file that looks like the following:

## 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. |
| 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. |
| `$BP_KEEP_NODE_BUILD_CACHE` | If set to `true` (default `false`), the folder `node_modules/.cache` will not be removed after the build, but will be readonly at runtime. |

## Usage

Expand Down
21 changes: 12 additions & 9 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,19 @@ func Build(entryResolver EntryResolver,
return packit.BuildResult{}, err
}

linkName := filepath.Join(layer.Path, "node_modules", ".cache")
err = os.RemoveAll(linkName)
if err != nil {
return packit.BuildResult{}, err
}
keepBuildCache, _ := environment.Lookup("BP_KEEP_NODE_BUILD_CACHE")
if keepBuildCache != "true" {
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
err = os.RemoveAll(linkName)
if err != nil {
return packit.BuildResult{}, err
}

cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
err = os.Symlink(cacheFolder, linkName)
if err != nil {
return packit.BuildResult{}, err
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
err = os.Symlink(cacheFolder, linkName)
if err != nil {
return packit.BuildResult{}, err
}
}

if build {
Expand Down
6 changes: 6 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ api = "0.7"
name = "BP_NPM_VERSION"
description = "configures a custom npm version"

[[metadata.configurations]]
name = "BP_KEEP_NODE_BUILD_CACHE"
default = "false"
description = "keep the 'node_modules/.cache' folder after the build (will be readonly at runtime)'"

[[metadata.configurations]]
name = "NODE_HOME"
description = "path the Node.js installation"
Expand All @@ -45,5 +50,6 @@ api = "0.7"
default = "error"
description = "configures npm output verbosity"


[[stacks]]
id = "*"

0 comments on commit d2ee774

Please sign in to comment.