Skip to content

Commit

Permalink
feat: add npm package bin paths as bin.*_path entries (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored Apr 17, 2023
1 parent 88b2ed2 commit 0726e21
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion npm/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,10 @@ def _impl(rctx):
bin_struct_fields.append(
""" {bin_name} = lambda name, **kwargs: _{bin_name}_internal(name, link_root_name = link_root_name, **kwargs),
{bin_name}_test = lambda name, **kwargs: _{bin_name}_test_internal(name, link_root_name = link_root_name, **kwargs),
{bin_name}_binary = lambda name, **kwargs: _{bin_name}_binary_internal(name, link_root_name = link_root_name, **kwargs),""".format(
{bin_name}_binary = lambda name, **kwargs: _{bin_name}_binary_internal(name, link_root_name = link_root_name, **kwargs),
{bin_name}_path = "{bin_path}",""".format(
bin_name = sanitized_bin_name,
bin_path = bins[bin_name],
),
)

Expand Down
13 changes: 12 additions & 1 deletion npm/private/test/generated_pkg_json_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def _get_functions_under_test_and_their_args():
},
}

def _get_properties_under_test():
return {
"rollup_path": "dist/bin/rollup",
}

def _target_names_used_for_test():
return [kwargs["name"] for _, kwargs in _get_functions_under_test_and_their_args().items()]

Expand All @@ -36,11 +41,17 @@ def test_only_expected_bin_struct_methods(env, bin_struct):
a new example usecase or removed.
"""

relevant_methods = [m for m in sorted(dir(bin_struct)) if m.startswith("rollup")]
relevant_methods = [m for m in sorted(dir(bin_struct)) if m.startswith("rollup") and not m.endswith("_path")]
relavent_properties = {
k: getattr(bin_struct, k)
for k in sorted(dir(bin_struct))
if k.endswith("_path")
}

# If new generated methods are added to the package_json.bzl file, then the tests here will need
# to be updated to call those macros.
loadingtest.equals(env, "only_expected_methods", sorted(_get_functions_under_test_and_their_args().keys()), sorted(relevant_methods))
loadingtest.equals(env, "only_expected_properties", _get_properties_under_test(), relavent_properties)

# buildifier: disable=function-docstring-args
def test_intermediate_targets_tagged_manual(env):
Expand Down
1 change: 1 addition & 0 deletions npm/private/test/package_json_checked.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def bin_factory(link_root_name):
rollup = lambda name, **kwargs: _rollup_internal(name, link_root_name = link_root_name, **kwargs),
rollup_test = lambda name, **kwargs: _rollup_test_internal(name, link_root_name = link_root_name, **kwargs),
rollup_binary = lambda name, **kwargs: _rollup_binary_internal(name, link_root_name = link_root_name, **kwargs),
rollup_path = "dist/bin/rollup",
)

bin = bin_factory("node_modules")
1 change: 1 addition & 0 deletions npm/private/test/package_json_with_dashes_checked.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def bin_factory(link_root_name):
webpack_bundle_analyzer = lambda name, **kwargs: _webpack_bundle_analyzer_internal(name, link_root_name = link_root_name, **kwargs),
webpack_bundle_analyzer_test = lambda name, **kwargs: _webpack_bundle_analyzer_test_internal(name, link_root_name = link_root_name, **kwargs),
webpack_bundle_analyzer_binary = lambda name, **kwargs: _webpack_bundle_analyzer_binary_internal(name, link_root_name = link_root_name, **kwargs),
webpack_bundle_analyzer_path = "lib/bin/analyzer.js",
)

bin = bin_factory("node_modules")

0 comments on commit 0726e21

Please sign in to comment.