Skip to content

Commit

Permalink
Hook to run command after node_modules installed (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silic0nS0ldier authored Jul 18, 2024
1 parent 954a6b5 commit d74ba96
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/npm_install/npm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "_".
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit d74ba96

Please sign in to comment.