From d74ba966813d242e32ee1932ae84befb8f846d4d Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 18 Jul 2024 10:28:09 +1000 Subject: [PATCH] Hook to run command after `node_modules` installed (#55) --- internal/npm_install/npm_install.bzl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/npm_install/npm_install.bzl b/internal/npm_install/npm_install.bzl index e03325bf..a3ace4e5 100755 --- a/internal/npm_install/npm_install.bzl +++ b/internal/npm_install/npm_install.bzl @@ -315,6 +315,13 @@ data attribute. First argument must be an exectuable, CWD will be that of package directory. """, ), + "postinstall_cmd": attr.string_list( + doc = """ + Command to run after installing `node_modules`. + + First argument must be an exectuable, CWD will be that of package directory. + """, + ), "_inputs": attr.label_list( default = [ "//internal/npm_install:pre_process_package_json.js", @@ -353,6 +360,19 @@ def _run_preinstall_cmd(repository_ctx, env): if result.return_code: fail("preinstall command failed: %s (%s)" % (result.stdout, result.stderr)) +def _run_postinstall_cmd(repository_ctx, env): + if len(repository_ctx.attr.preinstall_cmd) > 0: + repository_ctx.report_progress("Running postinstall command") + result = repository_ctx.execute( + repository_ctx.attr.postinstall_cmd, + timeout = repository_ctx.attr.timeout, + quiet = repository_ctx.attr.quiet, + environment = env, + working_directory = str(repository_ctx.path(_rerooted_workspace_package_json_dir(repository_ctx))), + ) + if result.return_code: + fail("postinstall command failed: %s (%s)" % (result.stdout, result.stderr)) + def _apply_pre_install_patches(repository_ctx): if len(repository_ctx.attr.pre_install_patches) == 0: return @@ -721,6 +741,8 @@ cd /D "{root}" && "{npm}" {npm_args} if result.return_code: fail("npm_install failed: %s (%s)" % (result.stdout, result.stderr)) + _run_postinstall_cmd(repository_ctx, env) + # removeNPMAbsolutePaths is run on node_modules after npm install as the package.json files # generated by npm are non-deterministic. They contain absolute install paths and other private # information fields starting with "_". removeNPMAbsolutePaths removes all fields starting with "_". @@ -886,6 +908,8 @@ cd /D "{root}" && "{yarn}" {yarn_args} if result.return_code: fail("yarn_install failed: %s (%s)" % (result.stdout, result.stderr)) + _run_postinstall_cmd(repository_ctx, env) + _symlink_node_modules(repository_ctx) _apply_post_install_patches(repository_ctx)