Skip to content

Commit e89705b

Browse files
committed
node: support lifecycle scripts
The `npm pack` runs lifecycle scripts which can spoil the standard output of the command and prevent correct parsing of the created tarball. For details see: https://docs.npmjs.com/cli/v8/using-npm/scripts#life-cycle-scripts Include a simple fix to parse out the tarball from the last line and cover the fix with a simple test. Signed-off-by: Miroslav Vadkerti <mvadkert@redhat.com>
1 parent faa6f8c commit e89705b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pre_commit/languages/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def install_environment(
9999
lang_base.setup_cmd(prefix, local_install_cmd)
100100

101101
_, pkg, _ = cmd_output('npm', 'pack', cwd=prefix.prefix_dir)
102-
pkg = prefix.path(pkg.strip())
102+
pkg = prefix.path(pkg.strip().split()[-1])
103103

104104
install = ('npm', 'install', '-g', pkg, *additional_dependencies)
105105
lang_base.setup_cmd(prefix, install)

tests/languages/node_test.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def test_installs_without_links_outside_env(tmpdir):
113113
assert cmd_output('foo')[1] == 'success!\n'
114114

115115

116-
def _make_hello_world(tmp_path):
117-
package_json = '''\
116+
def _make_hello_world(tmp_path, package_json=None):
117+
package_json = package_json or '''\
118118
{"name": "t", "version": "0.0.1", "bin": {"node-hello": "./bin/main.js"}}
119119
'''
120120
tmp_path.joinpath('package.json').write_text(package_json)
@@ -132,6 +132,20 @@ def test_node_hook_system(tmp_path):
132132
assert ret == (0, b'Hello World\n')
133133

134134

135+
def test_node_with_prepare_script(tmp_path):
136+
package_json = '''
137+
{
138+
"name": "t",
139+
"version": "0.0.1",
140+
"bin": {"node-hello": "./bin/main.js"},
141+
"scripts": {"prepare": "echo prepare"}
142+
}
143+
'''
144+
_make_hello_world(tmp_path, package_json)
145+
ret = run_language(tmp_path, node, 'node-hello')
146+
assert ret == (0, b'Hello World\n')
147+
148+
135149
def test_node_with_user_config_set(tmp_path):
136150
cfg = tmp_path.joinpath('cfg')
137151
cfg.write_text('cache=/dne\n')

0 commit comments

Comments
 (0)