Skip to content

Commit

Permalink
Symlink node_modules/.cache to tempdir
Browse files Browse the repository at this point in the history
  • Loading branch information
c0d1ngm0nk3y committed Apr 26, 2024
1 parent 73e2b75 commit aad42ad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
19 changes: 19 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/paketo-buildpacks/packit/v2/scribe"
)

const NODE_MODULES_CACHE = "node_modules_cache"

//go:generate faux --interface BuildManager --output fakes/build_manager.go
type BuildManager interface {
Resolve(workingDir string) (BuildProcess, bool, error)
Expand Down Expand Up @@ -134,6 +136,23 @@ func Build(entryResolver EntryResolver,
return packit.BuildResult{}, err
}

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

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

err = os.Symlink(cacheFolder, linkName)
if err != nil {
return packit.BuildResult{}, err
}

err = linker.Link(filepath.Join(projectPath, "node_modules"), filepath.Join(layer.Path, "node_modules"))
if err != nil {
return packit.BuildResult{}, err
Expand Down
11 changes: 11 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
cnbDir, err = os.MkdirTemp("", "cnb")
Expect(err).NotTo(HaveOccurred())

tempDir := t.TempDir()
t.Setenv("TMPDIR", tempDir)

t.Setenv("BP_NODE_PROJECT_PATH", "")

buildProcess = &fakes.BuildProcess{}
Expand Down Expand Up @@ -117,6 +120,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
environment,
symlinkResolver,
)

})

it.After(func() {
Expand Down Expand Up @@ -154,6 +158,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
buildLayer := result.Layers[0]
Expect(buildLayer.Name).To(Equal("build-modules"))
Expect(buildLayer.Path).To(Equal(filepath.Join(layersDir, "build-modules")))

nodeModuleCache := filepath.Join(layersDir, "build-modules", "node_modules", ".cache")
_, err = os.Stat(nodeModuleCache)
Expect(err).NotTo(HaveOccurred())
// FIXME: Why does this not work?
//Expect(info.Mode()&os.ModeSymlink == os.ModeSymlink).To(BeTrue())

Expect(buildLayer.SharedEnv).To(Equal(packit.Environment{}))
Expect(buildLayer.BuildEnv).To(Equal(packit.Environment{
"PATH.append": filepath.Join(layersDir, "build-modules", "node_modules", ".bin"),
Expand Down
8 changes: 7 additions & 1 deletion cmd/setup-symlinks/internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ func Run(executablePath, appDir string, symlinkResolver npminstall.SymlinkResolv
return err
}

return createSymlink(filepath.Join(layerPath, "node_modules"), linkPath)
err = createSymlink(filepath.Join(layerPath, "node_modules"), linkPath)
if err != nil {
return err
}

cacheFolder := filepath.Join(os.TempDir(), npminstall.NODE_MODULES_CACHE)
return os.Mkdir(cacheFolder, os.ModePerm)
}

func resolveWorkspaceModules(symlinkResolver npminstall.SymlinkResolver, appDir, layerPath string) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/setup-symlinks/internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func testRun(t *testing.T, context spec.G, it spec.S) {

tmpDir, err = os.MkdirTemp("", "tmp")
Expect(err).NotTo(HaveOccurred())
t.Setenv("TMPDIR", tmpDir)

Expect(os.Symlink(filepath.Join(tmpDir, "node_modules"), filepath.Join(appDir, "node_modules"))).To(Succeed())

Expand Down

0 comments on commit aad42ad

Please sign in to comment.