Skip to content

Commit 79a5051

Browse files
committed
Introduce BP_KEEP_NODE_BUILD_CACHE to safeguard existing behaviour
1 parent 42e13f8 commit 79a5051

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ file that looks like the following:
4040

4141
## Configuration
4242

43-
| Environment Variable | Description |
44-
|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
45-
| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. |
43+
| Environment Variable | Description |
44+
|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
45+
| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. |
46+
| `$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. |
4647

4748
## Usage
4849

build.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,19 @@ func Build(entryResolver EntryResolver,
284284
return packit.BuildResult{}, err
285285
}
286286

287-
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
288-
err = os.RemoveAll(linkName)
289-
if err != nil {
290-
return packit.BuildResult{}, err
291-
}
287+
keepBuildCache, _ := environment.Lookup("BP_KEEP_NODE_BUILD_CACHE")
288+
if keepBuildCache != "true" {
289+
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
290+
err = os.RemoveAll(linkName)
291+
if err != nil {
292+
return packit.BuildResult{}, err
293+
}
292294

293-
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
294-
err = os.Symlink(cacheFolder, linkName)
295-
if err != nil {
296-
return packit.BuildResult{}, err
295+
cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
296+
err = os.Symlink(cacheFolder, linkName)
297+
if err != nil {
298+
return packit.BuildResult{}, err
299+
}
297300
}
298301

299302
if build {

buildpack.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ api = "0.7"
3232
name = "BP_NPM_VERSION"
3333
description = "configures a custom npm version"
3434

35+
[[metadata.configurations]]
36+
name = "BP_KEEP_NODE_BUILD_CACHE"
37+
default = "false"
38+
description = "keep the 'node_modules/.cache' folder after the build (will be readonly at runtime)'"
39+
3540
[[metadata.configurations]]
3641
name = "NODE_HOME"
3742
description = "path the Node.js installation"
@@ -45,5 +50,6 @@ api = "0.7"
4550
default = "error"
4651
description = "configures npm output verbosity"
4752

53+
4854
[[stacks]]
4955
id = "*"

0 commit comments

Comments
 (0)