From 1cee8274a5695a3f654c917ec009a592abdb28e6 Mon Sep 17 00:00:00 2001 From: Miroslav Vadkerti Date: Wed, 17 Jul 2024 21:28:47 +0200 Subject: [PATCH] Support lifecycle scripts for node 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 --- pre_commit/languages/node.py | 2 +- tests/languages/node_test.py | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pre_commit/languages/node.py b/pre_commit/languages/node.py index d49c0e32..796ac902 100644 --- a/pre_commit/languages/node.py +++ b/pre_commit/languages/node.py @@ -99,7 +99,7 @@ def install_environment( lang_base.setup_cmd(prefix, local_install_cmd) _, pkg, _ = cmd_output('npm', 'pack', cwd=prefix.prefix_dir) - pkg = prefix.path(pkg.strip()) + pkg = prefix.path(pkg.strip().split()[-1]) install = ('npm', 'install', '-g', pkg, *additional_dependencies) lang_base.setup_cmd(prefix, install) diff --git a/tests/languages/node_test.py b/tests/languages/node_test.py index 055cb1e9..9dbc2a37 100644 --- a/tests/languages/node_test.py +++ b/tests/languages/node_test.py @@ -113,8 +113,8 @@ def test_installs_without_links_outside_env(tmpdir): assert cmd_output('foo')[1] == 'success!\n' -def _make_hello_world(tmp_path): - package_json = '''\ +def _make_hello_world(tmp_path, package_json=None): + package_json = package_json or '''\ {"name": "t", "version": "0.0.1", "bin": {"node-hello": "./bin/main.js"}} ''' tmp_path.joinpath('package.json').write_text(package_json) @@ -132,6 +132,20 @@ def test_node_hook_system(tmp_path): assert ret == (0, b'Hello World\n') +def test_node_with_prepare_script(tmp_path): + package_json = ''' +{ + "name": "t", + "version": "0.0.1", + "bin": {"node-hello": "./bin/main.js"}, + "scripts": {"prepare": "echo prepare"} +} +''' + _make_hello_world(tmp_path, package_json) + ret = run_language(tmp_path, node, 'node-hello') + assert ret == (0, b'Hello World\n') + + def test_node_with_user_config_set(tmp_path): cfg = tmp_path.joinpath('cfg') cfg.write_text('cache=/dne\n')