Skip to content

Commit

Permalink
Merge pull request #236 from tinybeachthor/fix-nodejs-link-built-binary
Browse files Browse the repository at this point in the history
fix: nodejs: link package bins after buildPhase
  • Loading branch information
DavHau authored Aug 11, 2022
2 parents 600f213 + b944fd6 commit 98e0a1a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/subsystems/nodejs/builders/granular/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@
# script to install (symlink or copy) dependencies.
installDeps = "${./install-deps.py}";

# python script to link bin entries from package.json
linkBins = "${./link-bins.py}";

# costs performance and doesn't seem beneficial in most scenarios
dontStrip = true;

Expand Down Expand Up @@ -465,6 +468,9 @@
installPhase = ''
runHook preInstall
echo "Symlinking bin entries from package.json"
python $linkBins
echo "Symlinking manual pages"
if [ -d "$nodeModules/$packageName/man" ]
then
Expand Down
29 changes: 0 additions & 29 deletions src/subsystems/nodejs/builders/granular/fix-package.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,6 @@
file=sys.stderr
)

# create symlinks for executables (bin entries from package.json)
def symlink_bin(bin_dir, package_json):
if 'bin' in package_json and package_json['bin']:
bin = package_json['bin']

def link(name, relpath):
source = f'{bin_dir}/{name}'
sourceDir = os.path.dirname(source)
# make target executable
os.chmod(relpath, 0o777)
# create parent dir
pathlib.Path(sourceDir).mkdir(parents=True, exist_ok=True)
dest = os.path.relpath(relpath, sourceDir)
print(f"symlinking executable. dest: {dest}; source: {source}")
os.symlink(dest, source)

if isinstance(bin, str):
name = package_json['name'].split('/')[-1]
link(name, bin)

else:
for name, relpath in bin.items():
link(name, relpath)

# symlink current packages executables to $nodeModules/.bin
symlink_bin(f'{out}/lib/node_modules/.bin/', package_json)
# symlink current packages executables to $out/bin
symlink_bin(f'{out}/bin/', package_json)

# write changes to package.json
if changed:
with open('package.json', 'w') as f:
Expand Down
41 changes: 41 additions & 0 deletions src/subsystems/nodejs/builders/granular/link-bins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import os
import pathlib


with open('package.json') as f:
package_json = json.load(f)

out = os.environ.get('out')

# create symlinks for executables (bin entries from package.json)
def symlink_bin(bin_dir, package_json):
if 'bin' in package_json and package_json['bin']:
bin = package_json['bin']

def link(name, relpath):
source = f'{bin_dir}/{name}'
sourceDir = os.path.dirname(source)
# make target executable
os.chmod(relpath, 0o777)
# create parent dir
pathlib.Path(sourceDir).mkdir(parents=True, exist_ok=True)
dest = os.path.relpath(relpath, sourceDir)
print(f"symlinking executable. dest: {dest}; source: {source}")
# if a bin with this name exists, overwrite
if os.path.exists(source):
os.remove(source)
os.symlink(dest, source)

if isinstance(bin, str):
name = package_json['name'].split('/')[-1]
link(name, bin)

else:
for name, relpath in bin.items():
link(name, relpath)

# symlink current packages executables to $nodeModules/.bin
symlink_bin(f'{out}/lib/node_modules/.bin/', package_json)
# symlink current packages executables to $out/bin
symlink_bin(f'{out}/bin/', package_json)

0 comments on commit 98e0a1a

Please sign in to comment.