Skip to content

Commit

Permalink
language/node: fix prepublish scripts
Browse files Browse the repository at this point in the history
This commit adds support for installing node modules with language node,
which are executing one of their dev(dependencies) in their prepublish
script.

Refs: Homebrew/homebrew-core#14827
  • Loading branch information
chrmoritz committed Jun 25, 2017
1 parent 73d81bb commit 43765db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Library/Homebrew/language/node.rb
Expand Up @@ -4,15 +4,23 @@ def self.npm_cache_config
"cache=#{HOMEBREW_CACHE}/npm_cache\n"
end

def self.pack_for_installation
def self.pack_for_installation(prepublish_requires_deps)
# Some packages are requiring (dev)dependencies to be in place when
# running the prepublish script (which is run before pack). In this case
# we have to install all dependencies a first time already before
# executing npm pack (and a second time when doing the actual install).
safe_system "npm", "install" if prepublish_requires_deps

# Homebrew assumes the buildpath/testpath will always be disposable
# and from npm 5.0.0 the logic changed so that when a directory is
# fed to `npm install` only symlinks are created linking back to that
# directory, consequently breaking that assumption. We require a tarball
# because npm install creates a "real" installation when fed a tarball.
output = Utils.popen_read("npm pack").chomp
raise "npm failed to pack #{Dir.pwd}" unless $CHILD_STATUS.exitstatus.zero?
output
output = Utils.popen_read("npm pack")
unless $CHILD_STATUS.exitstatus.zero? && !output.lines.empty?
raise "npm failed to pack #{Dir.pwd}"
end
output.lines.last.chomp
end

def self.setup_npm_environment
Expand All @@ -32,13 +40,13 @@ def self.setup_npm_environment
end
end

def self.std_npm_install_args(libexec)
def self.std_npm_install_args(libexec, prepublish_requires_deps = false)
setup_npm_environment
# tell npm to not install .brew_home by adding it to the .npmignore file
# (or creating a new one if no .npmignore file already exists)
open(".npmignore", "a") { |f| f.write("\n.brew_home\n") }

pack = pack_for_installation
pack = pack_for_installation(prepublish_requires_deps)

# npm install args for global style module format installed into libexec
%W[
Expand Down
6 changes: 6 additions & 0 deletions docs/Node-for-Formula-Authors.md
Expand Up @@ -72,6 +72,12 @@ This will install your Node module in npm's global module style with a custom pr
bin.install_symlink Dir["#{libexec}/bin/*"]
```

*Note:* If your module requires a (dev)dependencies to to present at npm prepublish time, you have to invoke `std_npm_install_args` with `prepublish_requires_deps` set to `true` like:

```ruby
system "npm", "install", *Language::Node.std_npm_install_args(libexec, prepublish_requires_deps = true)
```

### Installing module dependencies locally with `local_npm_install_args`

In your formula's `install` method, do any installation steps which need to be done before the `npm install` step and then `cd` to the top level of the included Node module. Then, use `system` with `Language::Node.local_npm_install_args` to invoke `npm install` like:
Expand Down

0 comments on commit 43765db

Please sign in to comment.