-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #48. This generates a binary which publishes the given package to NPM. It just merges the given directory with the root `.npmrc` file (which contains authentication information) and calls `npm publish`. Also made publish binaries for `rules_prerender` and `@rules_prerender/declarative_shadow_dom`. I tried to add a trap which deletes the staging directory after the binary has run, but I got permission errors and couldn't easily figure out what was going on. Fortunately `mktemp` still creates files under `$TMP`, so they should be cleaned up automatically, just a little less agressively than they could be.
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
|
||
exports_files(["npm_publish.sh"], visibility = ["//visibility:public"]) | ||
|
||
bzl_library( | ||
name = "npm_publish", | ||
srcs = ["npm_publish.bzl"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin") | ||
load("@aspect_rules_js//npm:providers.bzl", "NpmPackageInfo") | ||
load("@bazel_skylib//rules:build_test.bzl", "build_test") | ||
load("//common:label.bzl", "absolute", "file_path_of") | ||
|
||
def npm_publish(name, package, npmrc, testonly = None, visibility = None): | ||
"""Publishes the given `npm_package()` to NPM. | ||
Generates a binary which, when run, publishes the given package to NPM. | ||
Authentication information should be provided via `_authToken` in the | ||
provided `.npmrc` file. Also generates a test to make sure the binary builds | ||
successfully, since this it is very likely nothing else will depend on this. | ||
Args: | ||
name: Name of this target. | ||
package: The `npm_package()` to publish. | ||
npmrc: The `.npmrc` file containing authentication information. | ||
testonly: https://bazel.build/reference/be/common-definitions#common-attributes | ||
visibility: https://bazel.build/reference/be/common-definitions#common-attributes | ||
""" | ||
# For some reason we need to `copy_to_bin()` for the directory to show up in | ||
# `sh_binary()` runfiles? TBH, I don't understand why this is necessary. | ||
package_bin = "%s_package" % name | ||
copy_to_bin( | ||
name = package_bin, | ||
srcs = [package], | ||
testonly = testonly, | ||
) | ||
|
||
# Merge the `.npmrc` and package together, then run `npm publish`. | ||
# Authentication information should be in the `.npmrc`. | ||
native.sh_binary( | ||
name = name, | ||
srcs = [Label("//tools/publish:npm_publish.sh")], | ||
data = [package_bin, npmrc, "@pnpm//:pnpm"], | ||
args = [ | ||
"rules_prerender/%s" % _normalize(file_path_of(absolute(package))), | ||
"rules_prerender/%s" % _normalize(file_path_of(absolute(npmrc))), | ||
"pnpm/pnpm.sh", | ||
], | ||
deps = ["@bazel_tools//tools/bash/runfiles"], | ||
testonly = testonly, | ||
visibility = visibility, | ||
) | ||
|
||
# Always generate a test so the release process doesn't attempt to run | ||
# multiple publish binaries only to find that the second one fails to build. | ||
build_test( | ||
name = "%s_test" % name, | ||
targets = [":%s" % name], | ||
) | ||
|
||
# Bash runfiles is very particular about file paths, extra `./` paths are not | ||
# allowed. | ||
def _normalize(path): | ||
normalized = path.replace("/./", "/") | ||
if normalized.startswith("./"): | ||
return normalized[2:] | ||
return normalized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# --- begin runfiles.bash initialization v2 --- | ||
# Copy-pasted from the Bazel Bash runfiles library v2. | ||
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash | ||
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$0.runfiles/$f" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ | ||
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e | ||
# --- end runfiles.bash initialization v2 --- | ||
|
||
# Resolve arguments. | ||
readonly PACKAGE="$(rlocation ${1})" | ||
readonly NPMRC="$(rlocation ${2})" | ||
readonly NPM="$(rlocation ${3})" | ||
|
||
# Make sure there isn't already an `.npmrc` file in the package. | ||
if [[ -f "${PACKAGE}/.npmrc" ]]; then | ||
echo ".npmrc already exists in `${PACKAGE}`, expected it not to." >&2 | ||
exit 1 | ||
fi | ||
|
||
# Merge the package and the `.npmrc` in the temporary staging directory. | ||
readonly STAGING_DIRECTORY=$(mktemp -d) | ||
cp -r ${PACKAGE}/* "${STAGING_DIRECTORY}" | ||
cp "${NPMRC}" "${STAGING_DIRECTORY}/.npmrc" | ||
|
||
# Publish to NPM. Use `--no-git-checks` to ignore the `.git/` state, which is | ||
# still discovered by NPM, even within a Bazel context. | ||
"${NPM}" publish "${STAGING_DIRECTORY}" --no-git-checks --access public |