Skip to content

Commit

Permalink
refactor: remove yq as a dependency of translate_pnpm_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed May 16, 2022
1 parent ad54c12 commit b120f85
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
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.

2 changes: 1 addition & 1 deletion js/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ bzl_library(
visibility = ["//js:__subpackages__"],
deps = [
":pnpm_utils",
":repo_toolchains",
":starlark_codegen_utils",
":transitive_closure",
"@bazel_skylib//lib:dicts",
Expand All @@ -92,6 +91,7 @@ bzl_library(
name = "pnpm_utils",
srcs = ["pnpm_utils.bzl"],
visibility = ["//js:__subpackages__"],
deps = [":yaml"],
)

bzl_library(
Expand Down
14 changes: 14 additions & 0 deletions js/private/pnpm_utils.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"Utility functions for npm rules"

load(":yaml.bzl", _parse_yaml = "parse")

def _bazel_name(name, pnpm_version = None):
"Make a bazel friendly name from a package name and (optionally) a version that can be used in repository and target names"
escaped_name = name.replace("@", "at_").replace("/", "_")
Expand Down Expand Up @@ -35,6 +37,17 @@ def _parse_pnpm_name(pnpmName):
fail("unexpected pnpm versioned name " + pnpmName)
return segments

def _parse_pnpm_lock(lockfile_content):
"""Parse a pnpm lock file.
Args:
lockfile_content: yaml lockfile content
Returns:
dict containing parsed lockfile
"""
return _parse_yaml(lockfile_content)

def _assert_lockfile_version(version, testonly = False):
if type(version) != type(1.0):
fail("version should be passed as a float")
Expand Down Expand Up @@ -74,6 +87,7 @@ pnpm_utils = struct(
pnpm_name = _pnpm_name,
assert_lockfile_version = _assert_lockfile_version,
parse_pnpm_name = _parse_pnpm_name,
parse_pnpm_lock = _parse_pnpm_lock,
friendly_name = _friendly_name,
virtual_store_name = _virtual_store_name,
strip_peer_dep_version = _strip_peer_dep_version,
Expand Down
16 changes: 2 additions & 14 deletions js/private/translate_pnpm_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(":pnpm_utils.bzl", "pnpm_utils")
load(":transitive_closure.bzl", "translate_to_transitive_closure")
load(":starlark_codegen_utils.bzl", "starlark_codegen_utils")
load(":repo_toolchains.bzl", "yq_path")

_DOC = """Repository rule to generate npm_import rules from pnpm lock file.
Expand Down Expand Up @@ -139,22 +138,11 @@ _ATTRS = {
doc = """If true, runs preinstall, install and postinstall lifecycle hooks on npm packages if they exist""",
default = True,
),
"yq": attr.label(
doc = """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.""",
default = "@yq//:yq",
),
}

def _process_lockfile(rctx):
json_lockfile_path = rctx.path("pnpm-lock.json")
result = rctx.execute([yq_path(rctx), "-o=json", ".", rctx.path(rctx.attr.pnpm_lock)])
if result.return_code != 0:
fail("failed to convert pnpm lockfile to json: %s" % result.stderr)
rctx.file(json_lockfile_path, result.stdout)

json_lockfile = json.decode(rctx.read(json_lockfile_path))
return translate_to_transitive_closure(json_lockfile, rctx.attr.prod, rctx.attr.dev, rctx.attr.no_optional)
lockfile = pnpm_utils.parse_pnpm_lock(rctx.read(rctx.path(rctx.attr.pnpm_lock)))
return translate_to_transitive_closure(lockfile, rctx.attr.prod, rctx.attr.dev, rctx.attr.no_optional)

_NPM_IMPORT_TMPL = \
""" npm_import(
Expand Down

0 comments on commit b120f85

Please sign in to comment.