mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Support hooks with custom node version.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import contextlib
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.prefixed_command_runner import CalledProcessError
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
|
||||
@@ -9,14 +8,10 @@ from pre_commit.util import clean_path_on_failure
|
||||
ENVIRONMENT_DIR = 'node_env'
|
||||
|
||||
|
||||
class NodeEnv(python.PythonEnv):
|
||||
class NodeEnv(helpers.Environment):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
base = super(NodeEnv, self).env_prefix
|
||||
return ' '.join([
|
||||
base,
|
||||
'. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)]
|
||||
)
|
||||
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
@@ -27,29 +22,31 @@ def in_env(repo_cmd_runner):
|
||||
def install_environment(repo_cmd_runner, version='default'):
|
||||
assert repo_cmd_runner.exists('package.json')
|
||||
|
||||
with clean_path_on_failure(repo_cmd_runner.path(python.ENVIRONMENT_DIR)):
|
||||
repo_cmd_runner.run(
|
||||
['virtualenv', '{{prefix}}{0}'.format(python.ENVIRONMENT_DIR)],
|
||||
)
|
||||
env_dir = repo_cmd_runner.path(ENVIRONMENT_DIR)
|
||||
with clean_path_on_failure(env_dir):
|
||||
if version == 'default':
|
||||
# In the default case we attempt to install system node and if that
|
||||
# doesn't work we use --prebuilt
|
||||
try:
|
||||
with clean_path_on_failure(env_dir):
|
||||
repo_cmd_runner.run([
|
||||
'nodeenv', '-n', 'system',
|
||||
'{{prefix}}{0}'.format(ENVIRONMENT_DIR),
|
||||
])
|
||||
except CalledProcessError:
|
||||
# TODO: log failure here
|
||||
repo_cmd_runner.run([
|
||||
'nodeenv', '--prebuilt',
|
||||
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
|
||||
])
|
||||
else:
|
||||
repo_cmd_runner.run([
|
||||
'nodeenv', '--prebuilt', '-n', version,
|
||||
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
|
||||
])
|
||||
|
||||
with python.in_env(repo_cmd_runner) as python_env:
|
||||
python_env.run('pip install nodeenv')
|
||||
|
||||
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
|
||||
# Try and use the system level node executable first
|
||||
try:
|
||||
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
|
||||
python_env.run(
|
||||
'nodeenv -n system {{prefix}}{0}'.format(ENVIRONMENT_DIR),
|
||||
)
|
||||
except CalledProcessError:
|
||||
# TODO: log failure here
|
||||
python_env.run(
|
||||
'nodeenv --jobs 4 {{prefix}}{0}'.format(ENVIRONMENT_DIR),
|
||||
)
|
||||
|
||||
with in_env(repo_cmd_runner) as node_env:
|
||||
node_env.run('cd {prefix} && npm install -g')
|
||||
with in_env(repo_cmd_runner) as node_env:
|
||||
node_env.run('cd {prefix} && npm install -g')
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
|
||||
Reference in New Issue
Block a user