Skip to content

Commit

Permalink
feat(builtin): add support for using a local .nvmrc file for providin…
Browse files Browse the repository at this point in the history
…g a node version

Add `use_nvmrc` attribute on `node_repositories` to provide a Label pointing to the
local .nvmrc file in the repository.
  • Loading branch information
josephperrott committed Sep 2, 2021
1 parent 0f17126 commit ed7e479
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 10 additions & 2 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ This is necessary to bootstrap Bazel to run the package manager to download othe

<pre>
node_repositories(<a href="#node_repositories-name">name</a>, <a href="#node_repositories-node_download_auth">node_download_auth</a>, <a href="#node_repositories-node_repositories">node_repositories</a>, <a href="#node_repositories-node_urls">node_urls</a>, <a href="#node_repositories-node_version">node_version</a>,
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-preserve_symlinks">preserve_symlinks</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-vendored_node">vendored_node</a>, <a href="#node_repositories-vendored_yarn">vendored_yarn</a>,
<a href="#node_repositories-yarn_download_auth">yarn_download_auth</a>, <a href="#node_repositories-yarn_repositories">yarn_repositories</a>, <a href="#node_repositories-yarn_urls">yarn_urls</a>, <a href="#node_repositories-yarn_version">yarn_version</a>)
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-preserve_symlinks">preserve_symlinks</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-use_nvmrc">use_nvmrc</a>, <a href="#node_repositories-vendored_node">vendored_node</a>,
<a href="#node_repositories-vendored_yarn">vendored_yarn</a>, <a href="#node_repositories-yarn_download_auth">yarn_download_auth</a>, <a href="#node_repositories-yarn_repositories">yarn_repositories</a>, <a href="#node_repositories-yarn_urls">yarn_urls</a>, <a href="#node_repositories-yarn_version">yarn_version</a>)
</pre>

To be run in user's WORKSPACE to install rules_nodejs dependencies.
Expand Down Expand Up @@ -195,6 +195,14 @@ Defaults to `True`
(*<a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a>, mandatory*): A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).


<h4 id="node_repositories-use_nvmrc">use_nvmrc</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): the local path of the .nvmrc file containing the version of node

If set then also set node_version to the version found in the .nvmrc file.

Defaults to `None`

<h4 id="node_repositories-vendored_node">vendored_node</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): the local path to a pre-installed NodeJS runtime.
Expand Down
17 changes: 17 additions & 0 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ and `{filename}` with the matching entry from the `node_repositories` attribute.
default = _DEFAULT_NODE_VERSION,
doc = "the specific version of NodeJS to install or, if vendored_node is specified, the vendored version of node",
),
"use_nvmrc": attr.label(
allow_single_file = True,
default = None,
doc = """the local path of the .nvmrc file containing the version of node
If set then also set node_version to the version found in the .nvmrc file.""",
),
"package_json": attr.label_list(
doc = """(ADVANCED, not recommended)
a list of labels, which indicate the package.json files that will be installed
Expand Down Expand Up @@ -281,6 +288,11 @@ def _download_node(repository_ctx):

node_version = repository_ctx.attr.node_version

if repository_ctx.attr.use_nvmrc:
node_version = str(repository_ctx.read(repository_ctx.attr.use_nvmrc)).strip()

_verify_version_is_valid(node_version)

# Skip the download if we know it will fail
if not node_exists_for_os(node_version, host_os):
return
Expand Down Expand Up @@ -813,3 +825,8 @@ def node_repositories(**kwargs):
def _maybe(repo_rule, name, **kwargs):
if name not in native.existing_rules():
repo_rule(name = name, **kwargs)

def _verify_version_is_valid(version):
major, minor, patch = (version.split('.') + [None, None, None])[:3]
if not major.isdigit() or not minor.isdigit() or not patch.isdigit():
fail("Invalid node version: %s" % version)

0 comments on commit ed7e479

Please sign in to comment.