1717from pre_commit .prefix import Prefix
1818from pre_commit .util import cmd_output
1919from pre_commit .util import cmd_output_b
20- from pre_commit .util import rmtree
2120
2221ENVIRONMENT_DIR = 'node_env'
2322run_hook = lang_base .basic_run_hook
@@ -77,7 +76,18 @@ def health_check(prefix: Prefix, version: str) -> str | None:
7776def install_environment (
7877 prefix : Prefix , version : str , additional_dependencies : Sequence [str ],
7978) -> None :
80- assert prefix .exists ('package.json' )
79+ if prefix .exists ('.git' ) and prefix .exists ('package.json' ):
80+ # this requires a new-enough npm (2019ish?)
81+ pkgs = (f'git+file://{ prefix .prefix_dir } ' , * additional_dependencies )
82+ else :
83+ pkgs = (* additional_dependencies ,)
84+
85+ if not pkgs :
86+ raise AssertionError (
87+ '`language: node` must have package.json or '
88+ 'additional_dependencies' ,
89+ )
90+
8191 envdir = lang_base .environment_dir (prefix , ENVIRONMENT_DIR , version )
8292
8393 # https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
@@ -89,22 +99,5 @@ def install_environment(
8999 cmd_output_b (* cmd )
90100
91101 with in_env (prefix , version ):
92- # https://npm.community/t/npm-install-g-git-vs-git-clone-cd-npm-install-g/5449
93- # install as if we installed from git
94-
95- local_install_cmd = (
96- 'npm' , 'install' , '--include=dev' , '--include=prod' ,
97- '--ignore-prepublish' , '--no-progress' , '--no-save' ,
98- )
99- lang_base .setup_cmd (prefix , local_install_cmd )
100-
101- _ , pkg , _ = cmd_output ('npm' , 'pack' , cwd = prefix .prefix_dir )
102- pkg = prefix .path (pkg .strip ())
103-
104- install = ('npm' , 'install' , '-g' , pkg , * additional_dependencies )
102+ install = ('npm' , 'install' , '--allow-git=root' , '-g' , * pkgs )
105103 lang_base .setup_cmd (prefix , install )
106-
107- # clean these up after installation
108- if prefix .exists ('node_modules' ): # pragma: win32 no cover
109- rmtree (prefix .path ('node_modules' ))
110- os .remove (pkg )
0 commit comments