Skip to content

Commit 511de4f

Browse files
committed
Support hooks with custom node version.
1 parent 4b98b39 commit 511de4f

File tree

8 files changed

+62
-32
lines changed

8 files changed

+62
-32
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
sha: ca93f6834f2afc8a8f7de46c0e02076419077c7a
33
hooks:
44
- id: trailing-whitespace
5-
files: \.(py|sh|yaml)$
5+
files: \.(js|py|sh|yaml)$
66
- id: end-of-file-fixer
7-
files: \.(py|sh|yaml)$
7+
files: \.(js|py|sh|yaml)$
88
- id: check-yaml
99
files: \.(yaml|yml)$
1010
- id: debug-statements

pre_commit/languages/node.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import contextlib
22

33
from pre_commit.languages import helpers
4-
from pre_commit.languages import python
54
from pre_commit.prefixed_command_runner import CalledProcessError
65
from pre_commit.util import clean_path_on_failure
76

87

98
ENVIRONMENT_DIR = 'node_env'
109

1110

12-
class NodeEnv(python.PythonEnv):
11+
class NodeEnv(helpers.Environment):
1312
@property
1413
def env_prefix(self):
15-
base = super(NodeEnv, self).env_prefix
16-
return ' '.join([
17-
base,
18-
'. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)]
19-
)
14+
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
2015

2116

2217
@contextlib.contextmanager
@@ -27,29 +22,31 @@ def in_env(repo_cmd_runner):
2722
def install_environment(repo_cmd_runner, version='default'):
2823
assert repo_cmd_runner.exists('package.json')
2924

30-
with clean_path_on_failure(repo_cmd_runner.path(python.ENVIRONMENT_DIR)):
31-
repo_cmd_runner.run(
32-
['virtualenv', '{{prefix}}{0}'.format(python.ENVIRONMENT_DIR)],
33-
)
34-
35-
with python.in_env(repo_cmd_runner) as python_env:
36-
python_env.run('pip install nodeenv')
37-
38-
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
39-
# Try and use the system level node executable first
40-
try:
41-
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
42-
python_env.run(
43-
'nodeenv -n system {{prefix}}{0}'.format(ENVIRONMENT_DIR),
44-
)
45-
except CalledProcessError:
46-
# TODO: log failure here
47-
python_env.run(
48-
'nodeenv --jobs 4 {{prefix}}{0}'.format(ENVIRONMENT_DIR),
49-
)
50-
51-
with in_env(repo_cmd_runner) as node_env:
52-
node_env.run('cd {prefix} && npm install -g')
25+
env_dir = repo_cmd_runner.path(ENVIRONMENT_DIR)
26+
with clean_path_on_failure(env_dir):
27+
if version == 'default':
28+
# In the default case we attempt to install system node and if that
29+
# doesn't work we use --prebuilt
30+
try:
31+
with clean_path_on_failure(env_dir):
32+
repo_cmd_runner.run([
33+
'nodeenv', '-n', 'system',
34+
'{{prefix}}{0}'.format(ENVIRONMENT_DIR),
35+
])
36+
except CalledProcessError:
37+
# TODO: log failure here
38+
repo_cmd_runner.run([
39+
'nodeenv', '--prebuilt',
40+
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
41+
])
42+
else:
43+
repo_cmd_runner.run([
44+
'nodeenv', '--prebuilt', '-n', version,
45+
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
46+
])
47+
48+
with in_env(repo_cmd_runner) as node_env:
49+
node_env.run('cd {prefix} && npm install -g')
5350

5451

5552
def run_hook(repo_cmd_runner, hook, file_args):

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'asottile.ordereddict',
3434
'asottile.yaml',
3535
'jsonschema',
36+
'nodeenv>=0.9.4',
3637
'plumbum',
3738
'pyyaml',
3839
'simplejson',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
3+
console.log(process.version);
4+
console.log('Hello World');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- id: node-11-8-hook
2+
name: Node 0.11.8 hook
3+
entry: node-11-8-hook
4+
language: node
5+
language_version: 0.11.8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "node-11-8-hook",
3+
"version": "0.0.1",
4+
"bin": {"node-11-8-hook": "./bin/main.js"}
5+
}

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def node_hooks_repo(dummy_git_repo):
8282
yield _make_repo(dummy_git_repo, 'node_hooks_repo')
8383

8484

85+
@pytest.yield_fixture
86+
def node_0_11_8_hooks_repo(dummy_git_repo):
87+
yield _make_repo(dummy_git_repo, 'node_0_11_8_hooks_repo')
88+
89+
8590
@pytest.yield_fixture
8691
def ruby_hooks_repo(dummy_git_repo):
8792
yield _make_repo(dummy_git_repo, 'ruby_hooks_repo')
@@ -128,6 +133,11 @@ def config_for_node_hooks_repo(node_hooks_repo):
128133
yield _make_config(node_hooks_repo, 'foo', '\\.js$')
129134

130135

136+
@pytest.yield_fixture
137+
def config_for_node_0_11_8_hooks_repo(node_0_11_8_hooks_repo):
138+
yield _make_config(node_0_11_8_hooks_repo, 'node-11-8-hook', '\\.js$')
139+
140+
131141
@pytest.yield_fixture
132142
def config_for_ruby_hooks_repo(ruby_hooks_repo):
133143
yield _make_config(ruby_hooks_repo, 'ruby_hook', '\\.rb$')

tests/repository_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ def test_run_versioned_hook(config_for_python3_hooks_repo, store):
3232
assert ret[1] == "3.3\n['/dev/null']\nHello World\n"
3333

3434

35+
@pytest.mark.integration
36+
def test_run_versioned_node_hook(config_for_node_0_11_8_hooks_repo, store):
37+
repo = Repository.create(config_for_node_0_11_8_hooks_repo, store)
38+
ret = repo.run_hook('node-11-8-hook', ['/dev/null'])
39+
assert ret[0] == 0
40+
assert ret[1] == 'v0.11.8\nHello World\n'
41+
42+
3543
@pytest.mark.integration
3644
def test_lots_of_files(config_for_python_hooks_repo, store):
3745
repo = Repository.create(config_for_python_hooks_repo, store)

0 commit comments

Comments
 (0)