diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 02c07989..1fc358a5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,3 +6,11 @@ - id: pyflakes files: '*.py' + +- + repo: git@github.com:pre-commit/jshint + sha: 191734354d1191e3771c004c3e905a94728d0349 + hooks: + - + id: jshint + files: '*.js' \ No newline at end of file diff --git a/pre_commit/languages/helpers.py b/pre_commit/languages/helpers.py new file mode 100644 index 00000000..8397e9a8 --- /dev/null +++ b/pre_commit/languages/helpers.py @@ -0,0 +1,6 @@ + +def run_hook(env, hook, file_args): + return env.run( + ' '.join([hook['entry']] + hook.get('args', []) + list(file_args)), + retcode=None, + ) \ No newline at end of file diff --git a/pre_commit/languages/node.py b/pre_commit/languages/node.py index e2e40da4..bc4fa007 100644 --- a/pre_commit/languages/node.py +++ b/pre_commit/languages/node.py @@ -1,7 +1,44 @@ +import contextlib +from plumbum import local + +from pre_commit.languages import helpers +from pre_commit.languages import python + + +NODE_ENV = 'node_env' + + +class NodeEnv(object): + def __init__(self, py_env): + self.py_env = py_env + self.env_prefix = '. {0}/bin/activate &&'.format(NODE_ENV) + + def run(self, cmd, **kwargs): + return self.py_env.run(' '.join([self.env_prefix, cmd]), **kwargs) + + +@contextlib.contextmanager +def in_env(py_env): + yield NodeEnv(py_env) + def install_environment(): - raise NotImplementedError + assert local.path('package.json').exists() + + if local.path('node_env').exists(): + return + + local['virtualenv'][python.PY_ENV]() + + with python.in_env() as python_env: + python_env.run('pip install nodeenv') + python_env.run('nodeenv {0}'.format(NODE_ENV)) + + with in_env(python_env) as node_env: + node_env.run('npm install -g') def run_hook(hook, file_args): - raise NotImplementedError \ No newline at end of file + with python.in_env() as py_env: + with in_env(py_env) as node_env: + return helpers.run_hook(node_env, hook, file_args) \ No newline at end of file diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 08e1c790..850ad660 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,6 +1,7 @@ -import contexlib +import contextlib from plumbum import local +from pre_commit.languages import helpers PY_ENV = 'py_env' @@ -9,22 +10,11 @@ class PythonEnv(object): def __init__(self): self.env_prefix = '. {0}/bin/activate &&'.format(PY_ENV) - def run(self, cmd): - return local['bash']['-c', ' '.join([self.env_prefix, cmd])]() + def run(self, cmd, **kwargs): + return local['bash']['-c', ' '.join([self.env_prefix, cmd])].run(**kwargs) -NODE_ENV = 'node_env' - -class NodeEnv(object): - def __init__(self, py_env): - self.py_env = py_env - self.env_prefix = '. {0}/bin/activate &&'.format(NODE_ENV) - - def run(self, cmd): - return self.py_env.run(' '.join(self.env_prefix, cmd)) - - -@contexlib.contextmanager +@contextlib.contextmanager def in_env(): yield PythonEnv() @@ -44,6 +34,4 @@ def install_environment(): def run_hook(hook, file_args): # TODO: batch filenames with in_env() as env: - env = env - # MAGIC - pass + return helpers.run_hook(env, hook, file_args)