Merge pull request #9048 from dtrodrigues/npm-scripts

language/node: remove unneeded scripts prior to installation
This commit is contained in:
Dustin Rodrigues 2020-11-06 07:07:39 -05:00 committed by GitHub
commit 0f3c4b1781
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -16,6 +16,17 @@ module Language
# 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.
if (package = Pathname("package.json")) && package.exist?
begin
pkg_json = JSON.parse(package.read)
rescue JSON::ParserError
opoo "Could not parse package.json!"
raise
end
prepare_removed = pkg_json["scripts"]&.delete("prepare")
prepack_removed = pkg_json["scripts"]&.delete("prepack")
package.atomic_write(JSON.pretty_generate(pkg_json)) if prepare_removed || prepack_removed
end
output = Utils.popen_read("npm pack --ignore-scripts")
raise "npm failed to pack #{Dir.pwd}" if !$CHILD_STATUS.exitstatus.zero? || output.lines.empty?

View File

@ -24,6 +24,20 @@ describe Language::Node do
end
end
describe "#std_pack_for_installation" do
npm_pack_cmd = "npm pack --ignore-scripts"
it "removes prepare and prepack scripts" do
path = Pathname("package.json")
path.atomic_write("{\"scripts\":{\"prepare\": \"ls\", \"prepack\": \"ls\", \"test\": \"ls\"}}")
allow(Utils).to receive(:popen_read).with(npm_pack_cmd).and_return(`echo pack.tgz`)
subject.pack_for_installation
expect(path.read).not_to include("prepare")
expect(path.read).not_to include("prepack")
expect(path.read).to include("test")
end
end
describe "#std_npm_install_args" do
npm_install_arg = Pathname("libexec")
npm_pack_cmd = "npm pack --ignore-scripts"