Skip to content

Commit 720576b

Browse files
committed
Make node/tools/npm/install work with npm 5
Fixes #12 npm 5 won't install from a shrinkwrap file if there's no accompanying package.json. Create a valid package.json file in node/tools/npm/install.py before doing the install.
1 parent 74d8aeb commit 720576b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

node/tools/npm/install.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import shutil
1010
from tempfile import mkdtemp
11+
import json
1112

1213
from node.tools.npm.utils import (
1314
run_npm,
@@ -17,6 +18,31 @@
1718
def npm_install(shrinkwrap_path, output):
1819
shutil.copyfile(shrinkwrap_path, os.path.join(output, SHRINKWRAP))
1920

21+
# For npm 5+, npm won't install from a shrinkwrap file if there's
22+
# no package.json file. Create a dummy package.json file in the
23+
# output directory to work around that.
24+
25+
# First, read the version and name from the shrinkwrap.
26+
with open(os.path.join(output, SHRINKWRAP)) as f:
27+
shrinkwrap = json.load(f)
28+
29+
# Get the dependency name and version
30+
encoded_dep_name = shrinkwrap['name']
31+
dep_name = encoded_dep_name[len('npm-gen-'):]
32+
dep_version = shrinkwrap['version']
33+
34+
# Write out the package.json file.
35+
with open(os.path.join(output, "package.json"), "w") as out:
36+
json.dump({
37+
'name': encoded_dep_name,
38+
'version': dep_version,
39+
'dependencies': {
40+
dep_name: dep_version,
41+
},
42+
}, out)
43+
44+
# Now finish the install.
45+
2046
tmpdir = mkdtemp(suffix='npm_install')
2147
env = {
2248
# Change the npm cache to a tmp directory so that this can run

0 commit comments

Comments
 (0)