add a script to build a npm bin

This commit is contained in:
Brian Hicks 2023-05-02 05:46:59 -05:00
parent a2befe32b3
commit ce93ff9ecd
No known key found for this signature in database
GPG Key ID: C4F324B9CAAB0D50
3 changed files with 72 additions and 0 deletions

View File

@ -39,3 +39,8 @@ export_file(
name = "build_node_modules.py",
visibility = ["PUBLIC"],
)
export_file(
name = "build_npm_bin.py",
visibility = ["PUBLIC"],
)

View File

@ -0,0 +1,62 @@
#!/usr/bin/env python3
"""
Make a independently-runnable npm binary.
"""
import argparse
import os
import stat
import sys
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"node_modules",
help="The path to the node_modules you want to get binaries from.",
)
parser.add_argument(
"bin",
help="The binary to pull from node_modules",
)
parser.add_argument("out", help="Where you want the resulting binary.")
parser.add_argument(
"--bin-dir",
help="Path to a node installation's binary directory. If present, will be treated as an extra entry in PATH for the duration of this command.",
)
args = parser.parse_args()
if not os.path.exists(args.node_modules):
print("The given node_modules does not exist!")
sys.exit(1)
lines = [
"#!/usr/bin/env bash",
"set -e",
]
bins = os.path.join(os.path.abspath(args.node_modules), ".bin")
path = [bins]
if args.bin_dir:
path.append(os.path.abspath(args.bin_dir))
lines.append("export PATH={}:$PATH".format(":".join(path)))
bin = os.path.join(bins, args.bin)
if not os.path.exists(bin):
print("The given binary does not exist!")
sys.exit(1)
lines.append(f"exec {bin} $@")
with open(args.out, "w") as fh:
fh.write("\n".join(lines))
os.chmod(
args.out,
stat.S_IRUSR
| stat.S_IXUSR
| stat.S_IRGRP
| stat.S_IXGRP
| stat.S_IROTH
| stat.S_IXOTH,
)

View File

@ -2,6 +2,7 @@ NodeToolchainInfo = provider(fields=[
"bin_dir",
"node",
"build_node_modules",
"build_npm_bin",
])
def _node_toolchain_impl(ctx) -> [[DefaultInfo.type, NodeToolchainInfo.type]]:
@ -15,6 +16,7 @@ def _node_toolchain_impl(ctx) -> [[DefaultInfo.type, NodeToolchainInfo.type]]:
bin_dir = ctx.attrs.bin_dir,
node = ctx.attrs.node[RunInfo],
build_node_modules = ctx.attrs._build_node_modules,
build_npm_bin = ctx.attrs._build_npm_bin,
),
]
@ -31,6 +33,9 @@ node_toolchain = rule(
"_build_node_modules": attrs.dep(
default="prelude-nri//node:build_node_modules.py",
),
"_build_npm_bin": attrs.dep(
default="prelude-nri//node:build_npm_bin.py",
),
},
is_toolchain_rule = True,
)