mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 09:04:41 +04:00
Merge pull request #1667 from pre-commit/improve_node_install
improve node install by using npm pack
This commit is contained in:
commit
0c339e0647
2 changed files with 45 additions and 4 deletions
|
|
@ -19,6 +19,7 @@ from pre_commit.prefix import Prefix
|
||||||
from pre_commit.util import clean_path_on_failure
|
from pre_commit.util import clean_path_on_failure
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
|
from pre_commit.util import rmtree
|
||||||
|
|
||||||
ENVIRONMENT_DIR = 'node_env'
|
ENVIRONMENT_DIR = 'node_env'
|
||||||
|
|
||||||
|
|
@ -99,11 +100,23 @@ def install_environment(
|
||||||
with in_env(prefix, version):
|
with in_env(prefix, version):
|
||||||
# https://npm.community/t/npm-install-g-git-vs-git-clone-cd-npm-install-g/5449
|
# https://npm.community/t/npm-install-g-git-vs-git-clone-cd-npm-install-g/5449
|
||||||
# install as if we installed from git
|
# install as if we installed from git
|
||||||
helpers.run_setup_cmd(prefix, ('npm', 'install'))
|
|
||||||
helpers.run_setup_cmd(
|
local_install_cmd = (
|
||||||
prefix,
|
'npm', 'install', '--dev', '--prod',
|
||||||
('npm', 'install', '-g', '.', *additional_dependencies),
|
'--ignore-prepublish', '--no-progress', '--no-save',
|
||||||
)
|
)
|
||||||
|
helpers.run_setup_cmd(prefix, local_install_cmd)
|
||||||
|
|
||||||
|
_, pkg, _ = cmd_output('npm', 'pack', cwd=prefix.prefix_dir)
|
||||||
|
pkg = prefix.path(pkg.strip())
|
||||||
|
|
||||||
|
install = ('npm', 'install', '-g', pkg, *additional_dependencies)
|
||||||
|
helpers.run_setup_cmd(prefix, install)
|
||||||
|
|
||||||
|
# clean these up after installation
|
||||||
|
if prefix.exists('node_modules'): # pragma: win32 no cover
|
||||||
|
rmtree(prefix.path('node_modules'))
|
||||||
|
os.remove(pkg)
|
||||||
|
|
||||||
|
|
||||||
def run_hook(
|
def run_hook(
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
|
@ -10,6 +11,7 @@ from pre_commit import envcontext
|
||||||
from pre_commit import parse_shebang
|
from pre_commit import parse_shebang
|
||||||
from pre_commit.languages import node
|
from pre_commit.languages import node
|
||||||
from pre_commit.prefix import Prefix
|
from pre_commit.prefix import Prefix
|
||||||
|
from pre_commit.util import cmd_output
|
||||||
from testing.util import xfailif_windows
|
from testing.util import xfailif_windows
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -78,3 +80,29 @@ def test_unhealthy_if_system_node_goes_missing(tmpdir):
|
||||||
|
|
||||||
node_bin.remove()
|
node_bin.remove()
|
||||||
assert not node.healthy(prefix, 'system')
|
assert not node.healthy(prefix, 'system')
|
||||||
|
|
||||||
|
|
||||||
|
@xfailif_windows # pragma: win32 no cover
|
||||||
|
def test_installs_without_links_outside_env(tmpdir):
|
||||||
|
tmpdir.join('bin/main.js').ensure().write(
|
||||||
|
'#!/usr/bin/env node\n'
|
||||||
|
'_ = require("lodash"); console.log("success!")\n',
|
||||||
|
)
|
||||||
|
tmpdir.join('package.json').write(
|
||||||
|
json.dumps({
|
||||||
|
'name': 'foo',
|
||||||
|
'version': '0.0.1',
|
||||||
|
'bin': {'foo': './bin/main.js'},
|
||||||
|
'dependencies': {'lodash': '*'},
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
prefix = Prefix(str(tmpdir))
|
||||||
|
node.install_environment(prefix, 'system', ())
|
||||||
|
assert node.healthy(prefix, 'system')
|
||||||
|
|
||||||
|
# this directory shouldn't exist, make sure we succeed without it existing
|
||||||
|
cmd_output('rm', '-rf', str(tmpdir.join('node_modules')))
|
||||||
|
|
||||||
|
with node.in_env(prefix, 'system'):
|
||||||
|
assert cmd_output('foo')[1] == 'success!\n'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue