Skip to content

Commit 29f3e67

Browse files
committed
improve node install by using npm pack
1 parent 2779bde commit 29f3e67

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

pre_commit/languages/node.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pre_commit.util import clean_path_on_failure
2020
from pre_commit.util import cmd_output
2121
from pre_commit.util import cmd_output_b
22+
from pre_commit.util import rmtree
2223

2324
ENVIRONMENT_DIR = 'node_env'
2425

@@ -99,11 +100,23 @@ def install_environment(
99100
with in_env(prefix, version):
100101
# https://npm.community/t/npm-install-g-git-vs-git-clone-cd-npm-install-g/5449
101102
# install as if we installed from git
102-
helpers.run_setup_cmd(prefix, ('npm', 'install'))
103-
helpers.run_setup_cmd(
104-
prefix,
105-
('npm', 'install', '-g', '.', *additional_dependencies),
103+
104+
local_install_cmd = (
105+
'npm', 'install', '--dev', '--prod',
106+
'--ignore-prepublish', '--no-progress', '--no-save',
106107
)
108+
helpers.run_setup_cmd(prefix, local_install_cmd)
109+
110+
_, pkg, _ = cmd_output('npm', 'pack', cwd=prefix.prefix_dir)
111+
pkg = prefix.path(pkg.strip())
112+
113+
install = ('npm', 'install', '-g', pkg, *additional_dependencies)
114+
helpers.run_setup_cmd(prefix, install)
115+
116+
# clean these up after installation
117+
if prefix.exists('node_modules'): # pragma: win32 no cover
118+
rmtree(prefix.path('node_modules'))
119+
os.remove(pkg)
107120

108121

109122
def run_hook(

tests/languages/node_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import shutil
34
import sys
@@ -10,6 +11,7 @@
1011
from pre_commit import parse_shebang
1112
from pre_commit.languages import node
1213
from pre_commit.prefix import Prefix
14+
from pre_commit.util import cmd_output
1315
from testing.util import xfailif_windows
1416

1517

@@ -78,3 +80,29 @@ def test_unhealthy_if_system_node_goes_missing(tmpdir):
7880

7981
node_bin.remove()
8082
assert not node.healthy(prefix, 'system')
83+
84+
85+
@xfailif_windows # pragma: win32 no cover
86+
def test_installs_without_links_outside_env(tmpdir):
87+
tmpdir.join('bin/main.js').ensure().write(
88+
'#!/usr/bin/env node\n'
89+
'_ = require("lodash"); console.log("success!")\n',
90+
)
91+
tmpdir.join('package.json').write(
92+
json.dumps({
93+
'name': 'foo',
94+
'version': '0.0.1',
95+
'bin': {'foo': './bin/main.js'},
96+
'dependencies': {'lodash': '*'},
97+
}),
98+
)
99+
100+
prefix = Prefix(str(tmpdir))
101+
node.install_environment(prefix, 'system', ())
102+
assert node.healthy(prefix, 'system')
103+
104+
# this directory shouldn't exist, make sure we succeed without it existing
105+
cmd_output('rm', '-rf', str(tmpdir.join('node_modules')))
106+
107+
with node.in_env(prefix, 'system'):
108+
assert cmd_output('foo')[1] == 'success!\n'

0 commit comments

Comments
 (0)