Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/nodejs/supply/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/nodejs/supply/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type NPM interface {

type Yarn interface {
Build(buildDir, cacheDir string, isBerry bool) error
Rebuild(buildDir string) error
}

type PNPM interface {
Expand Down Expand Up @@ -340,6 +341,12 @@ func (s *Supplier) BuildDependencies() error {
}

switch {
case s.UseYarn && s.IsVendored:
s.Log.Info("Prebuild detected (node_modules already exists)")
if err := s.Yarn.Rebuild(s.Stager.BuildDir()); err != nil {
return err
}

case s.UseYarn:
if err := s.Yarn.Build(s.Stager.BuildDir(), s.Stager.CacheDir(), s.UsesYarnBerry); err != nil {
return err
Expand Down
29 changes: 29 additions & 0 deletions src/nodejs/supply/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,35 @@ var _ = Describe("Supply", func() {
})
})

Context("using yarn with vendored node_modules", func() {
BeforeEach(func() {
supplier.UseYarn = true
supplier.IsVendored = true
})

It("runs yarn rebuild instead of yarn build", func() {
mockYarn.EXPECT().Rebuild(buildDir).Return(nil)
Expect(supplier.BuildDependencies()).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("Prebuild detected (node_modules already exists)"))
})

It("runs prebuild script when specified", func() {
supplier.PreBuild = "prescriptive"
mockYarn.EXPECT().Rebuild(buildDir).Return(nil)
mockCommand.EXPECT().Execute(buildDir, gomock.Any(), gomock.Any(), "yarn", "run", "heroku-prebuild")
Expect(supplier.BuildDependencies()).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("Running heroku-prebuild (yarn)"))
})

It("runs postbuild script when specified", func() {
supplier.PostBuild = "descriptive"
mockYarn.EXPECT().Rebuild(buildDir).Return(nil)
mockCommand.EXPECT().Execute(buildDir, gomock.Any(), gomock.Any(), "yarn", "run", "heroku-postbuild")
Expect(supplier.BuildDependencies()).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("Running heroku-postbuild (yarn)"))
})
})

Describe("using npm", func() {
BeforeEach(func() {
supplier.UseYarn = false
Expand Down
10 changes: 10 additions & 0 deletions src/nodejs/yarn/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,13 @@ func (y *Yarn) buildClassic(buildDir, cacheDir string) error {

return nil
}

func (y *Yarn) Rebuild(buildDir string) error {
y.Log.Info("Rebuilding any native modules (yarn)")
cmd := exec.Command("yarn", "rebuild")
cmd.Dir = buildDir
cmd.Stdout = y.Log.Output()
cmd.Stderr = y.Log.Output()
cmd.Env = append(os.Environ(), "npm_config_nodedir="+os.Getenv("NODE_HOME"))
return y.Command.Run(cmd)
}
30 changes: 30 additions & 0 deletions src/nodejs/yarn/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,34 @@ var _ = Describe("Yarn", func() {
})
})
})

Describe("Rebuild", func() {
var oldNodeHome string

BeforeEach(func() {
oldNodeHome = os.Getenv("NODE_HOME")
Expect(os.Setenv("NODE_HOME", "test_node_home")).To(Succeed())
})

AfterEach(func() {
Expect(os.Setenv("NODE_HOME", oldNodeHome)).To(Succeed())
})

It("runs yarn rebuild with NODE_HOME set", func() {
mockCommand.EXPECT().Run(gomock.Any()).Do(func(cmd *exec.Cmd) {
Expect(cmd.Args).To(Equal([]string{"yarn", "rebuild"}))
Expect(cmd.Dir).To(Equal(buildDir))
Expect(cmd.Env).To(ContainElement("npm_config_nodedir=test_node_home"))
})

Expect(y.Rebuild(buildDir)).To(Succeed())
Expect(buffer.String()).To(ContainSubstring("Rebuilding any native modules (yarn)"))
})

It("propagates command errors", func() {
mockCommand.EXPECT().Run(gomock.Any()).Return(os.ErrNotExist)

Expect(y.Rebuild(buildDir)).To(Equal(os.ErrNotExist))
})
})
})
Loading