Skip to content

Commit

Permalink
refactor: remove yq dependency from npm_import
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed May 16, 2022
1 parent b120f85 commit 742fce3
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 85 deletions.
6 changes: 0 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ nodejs_register_toolchains(
node_version = "16.9.0",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "DEFAULT_YQ_VERSION", "register_yq_toolchains")

register_yq_toolchains(
version = DEFAULT_YQ_VERSION,
)

load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")

register_unittest_toolchains()
Expand Down
3 changes: 1 addition & 2 deletions docs/npm_import.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions e2e/link_js_package/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ nodejs_register_toolchains(
node_version = "16.9.0",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "DEFAULT_YQ_VERSION", "register_yq_toolchains")

register_yq_toolchains(
version = DEFAULT_YQ_VERSION,
)

load("@aspect_rules_js//js:npm_import.bzl", "translate_pnpm_lock")

translate_pnpm_lock(
Expand Down
6 changes: 0 additions & 6 deletions e2e/pnpm_workspace/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ nodejs_register_toolchains(
node_version = DEFAULT_NODE_VERSION,
)

load("@aspect_bazel_lib//lib:repositories.bzl", "DEFAULT_YQ_VERSION", "register_yq_toolchains")

register_yq_toolchains(
version = DEFAULT_YQ_VERSION,
)

load("@aspect_rules_js//js:npm_import.bzl", "translate_pnpm_lock")

translate_pnpm_lock(
Expand Down
6 changes: 0 additions & 6 deletions e2e/pnpm_workspace_dot_dot/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ nodejs_register_toolchains(
node_version = "16.9.0",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "DEFAULT_YQ_VERSION", "register_yq_toolchains")

register_yq_toolchains(
version = DEFAULT_YQ_VERSION,
)

load("@aspect_rules_js//js:npm_import.bzl", "translate_pnpm_lock")

translate_pnpm_lock(
Expand Down
6 changes: 0 additions & 6 deletions e2e/rules_foo/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ nodejs_register_toolchains(
node_version = "16.9.0",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "DEFAULT_YQ_VERSION", "register_yq_toolchains")

register_yq_toolchains(
version = DEFAULT_YQ_VERSION,
)

load("@rules_foo//:repositories.bzl", rules_foo_repositories = "repositories")

rules_foo_repositories()
6 changes: 1 addition & 5 deletions js/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def npm_import(
integrity = "",
patch_args = ["-p0"],
patches = [],
custom_postinstall = "",
yq = "@yq//:yq"):
custom_postinstall = ""):
"""Import a single npm package into Bazel.
Normally you'd want to use `translate_pnpm_lock` to import all your packages at once.
Expand Down Expand Up @@ -138,8 +137,6 @@ def npm_import(
patch_args: Arguments to pass to the patch tool.
`-p1` will usually be needed for patches generated by git.
patches: Patch files to apply onto the downloaded npm package.
yq: The label to the yq binary to use. If executing on a windows host, the .exe extension will be appended if
there is no .exe, .bat, or .cmd extension on the label.
"""

# By convention, the `{name}` repository contains the actual npm
Expand All @@ -153,7 +150,6 @@ def npm_import(
patches = patches,
custom_postinstall = custom_postinstall,
run_lifecycle_hooks = run_lifecycle_hooks,
yq = yq,
)

# By convention, the `{name}{pnpm_utils.links_postfix}` repository contains the generated
Expand Down
10 changes: 0 additions & 10 deletions js/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,11 @@ bzl_library(
visibility = ["//js:__subpackages__"],
deps = [
":pnpm_utils",
":repo_toolchains",
":starlark_codegen_utils",
"@aspect_bazel_lib//lib:repo_utils",
],
)

bzl_library(
name = "repo_toolchains",
srcs = ["repo_toolchains.bzl"],
visibility = ["//js:__subpackages__"],
deps = [
"@aspect_bazel_lib//lib:repo_utils",
],
)

bzl_library(
name = "translate_pnpm_lock",
srcs = ["translate_pnpm_lock.bzl"],
Expand Down
28 changes: 10 additions & 18 deletions js/private/npm_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":pnpm_utils.bzl", "pnpm_utils")
load(":starlark_codegen_utils.bzl", "starlark_codegen_utils")
load(":repo_toolchains.bzl", "yq_path")

_LINK_JS_PACKAGE_TMPL = """load("@aspect_rules_js//js:defs.bzl", _js_package = "js_package")
load("@aspect_rules_js//js:run_js_binary.bzl", _run_js_binary = "run_js_binary")
Expand Down Expand Up @@ -414,28 +413,21 @@ _ATTRS = dicts.add(_COMMON_ATTRS, {
"patches": attr.label_list(),
"run_lifecycle_hooks": attr.bool(),
"custom_postinstall": attr.string(),
"yq": attr.label(default = "@yq//:yq"),
})

def _inject_run_lifecycle_hooks(rctx, pkg_json_path):
rctx.execute([
yq_path(rctx),
"-P",
"-o=json",
"--inplace",
".scripts._rules_js_run_lifecycle_hooks=\"1\"",
pkg_json_path,
], quiet = False)
package_json = json.decode(rctx.read(pkg_json_path))
package_json.setdefault("scripts", {})["_rules_js_run_lifecycle_hooks"] = "1"

# TODO: The order of fields in package.json is not preserved making it harder to read
rctx.file(pkg_json_path, json.encode_indent(package_json, indent = " "))

def _inject_custom_postinstall(rctx, pkg_json_path, custom_postinstall):
rctx.execute([
yq_path(rctx),
"-P",
"-o=json",
"--inplace",
".scripts._rules_js_custom_postinstall=\"%s\"" % custom_postinstall,
pkg_json_path,
], quiet = False)
package_json = json.decode(rctx.read(pkg_json_path))
package_json.setdefault("scripts", {})["_rules_js_custom_postinstall"] = custom_postinstall

# TODO: The order of fields in package.json is not preserved making it harder to read
rctx.file(pkg_json_path, json.encode_indent(package_json, indent = " "))

def _get_bin_entries(pkg_json, package):
# https://docs.npmjs.com/cli/v7/configuring-npm/package-json#bin
Expand Down
20 changes: 0 additions & 20 deletions js/private/repo_toolchains.bzl

This file was deleted.

0 comments on commit 742fce3

Please sign in to comment.