1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2021 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# shellcheck disable=SC2181
# Publishes a new version of the project to the npm package registry.
#
# A `release` argument must be one of the following:
#
# - `patch`
# - `minor`
# - `major`
# - `premajor`
# - `preminor`
# - `prepatch`
# - `prerelease`
#
# Usage: npm_publish <release> <message>
#
# Arguments:
#
# release release type.
# message commit message.
#
# VARIABLES #
# Set the release type:
release="$1"
# Set the release commit message:
message="$2"
# Determine the root directory:
root_dir="$(git rev-parse --show-toplevel)"
# Get the current commit hash:
git_hash="$(git rev-parse HEAD)"
# Get the current Git branch:
git_branch="$(git rev-parse --abbrev-ref HEAD)"
# Specify the path to the root package.json:
package_json="${root_dir}/package.json"
# Get the current project version:
script1="var pkg = require( '${package_json}' ); console.log( pkg.version );"
current_version=$(node -e "${script1}" | tr -d '\n' )
# Determine the new project version:
script2="var semver = require( 'semver' ); console.log( semver.inc( '${current_version}', '${release}' ) );"
release_version=$(node -e "${script2}" | tr -d '\n' )
# Specify the project source code directory:
base_dir="${root_dir}/lib/node_modules"
# Specify the path to a script for updating package directories meta data:
update_directories="${base_dir}/@stdlib/_tools/package-json/scripts/update_directories"
# Specify the output build directory when generating a gzipped archive:
npm_tarball_build_out="${root_dir}/build/npm"
# Define the gzipped archive basename:
npm_tarball_basename="stdlib-stdlib-${release_version}.tgz"
# FUNCTIONS #
# Error handler.
#
# $1 - error status
on_error() {
cleanup
exit "$1"
}
# Runs clean-up tasks.
cleanup() {
rm -rf "${npm_tarball_build_out}"
}
# Prints a success message.
print_success() {
echo '' >&2
echo 'Success!' >&2
echo '' >&2
}
# Prints usage information.
usage() {
echo '' >&2
echo 'Usage: npm_publish <release> <message>' >&2
echo '' >&2
echo 'Arguments:' >&2
echo '' >&2
echo ' release release type (patch, minor, major, etc).' >&2
echo ' message commit message.' >&2
echo '' >&2
}
# Reverts changes, resetting repository to its prior state.
revert_changes() {
echo 'Reverting changes...' >&2
git reset --hard "${git_hash}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when attempting to reverting changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully reverted changes.' >&2
echo '' >&2
return 0
}
# Performs checks to prevent unintended behavior when attempting to publish to npm.
check() {
echo 'Performing checks...' >&2
if [[ "${git_branch}" != 'develop' ]]; then
echo '' >&2
echo 'Error: invalid operation. Releasing new versions should only be performed on the develop branch.' >&2
echo '' >&2
return 1
fi
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Error: invalid operation. Working directory must be clean.' >&2
echo '' >&2
return 1
fi
echo 'Successfully performed checks.' >&2
echo '' >&2
return 0
}
# Pulls the latest changes from a remote repository.
update_repository() {
echo 'Pulling the latest changes...' >&2
git pull origin "${git_branch}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Pulling the latest changes failed. Check for merge conflicts.' >&2
echo '' >&2
return 1
fi
echo 'Successfully pulled latest changes.' >&2
echo '' >&2
return 0
}
# Initializes the development environment.
init() {
echo 'Initializing development environment...' >&2
make init
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to initialize development environment.' >&2
echo '' >&2
return 1
fi
echo 'Successfully initialized development environment.' >&2
echo '' >&2
return 0
}
# Checks licenses of dependencies.
check_licenses() {
echo 'Checking dependency licenses...' >&2
make check-licenses-production >/dev/null
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Potential incompatible dependency license. Please resolve before publishing. Command: make check-licenses-production.' >&2
echo '' >&2
return 1
fi
echo 'No detected licensing issues.' >&2
echo '' >&2
return 0
}
# Updates package meta data.
update_package_meta_data() {
echo 'Updating package meta data...' >&2
echo '' >&2
echo 'Updating directories...' >&2
node "${update_directories}" "${base_dir}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when updating directories meta data.' >&2
echo '' >&2
return 1
fi
echo 'Successfully updated directories.' >&2
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Committing changes...' >&2
git add -A && git commit -m 'Update directories meta data'
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to commit changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully committed changes.' >&2
fi
echo '' >&2
return 0
}
# Updates namespace table of contents.
update_namespace_tocs() {
local files
echo 'Updating namespace table of contents...' >&2
echo '' >&2
echo 'Resolving list of namespace READMEs...' >&2
files=$(make list-pkgs-namespace-readmes | tr '\n' ' ')
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when resolving a list of namespace READMEs.' >&2
echo '' >&2
return 1
fi
echo 'Successfully resolved namespace READMEs.' >&2
echo '' >&2
echo 'Updating READMEs...' >&2
make FILES="${files}" markdown-namespace-tocs-files
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when updating namespace README table of contents.' >&2
echo '' >&2
return 1
fi
make FILES="${files}" markdown-pkg-urls-files
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when updating namespace README package URLs.' >&2
echo '' >&2
return 1
fi
echo 'Successfully updated READMEs.' >&2
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Committing changes...' >&2
git add -A && git commit -m 'Update namespace ToCs'
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to commit changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully committed changes.' >&2
fi
echo '' >&2
return 0
}
# Updates package URLs in Markdown files.
update_markdown_package_urls() {
echo 'Updating package URLs in Markdown files...' >&2
make markdown-pkg-urls MARKDOWN_FILTER='.*/lib/node_modules/@stdlib/.*'
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when updating package URLs in Markdown files.' >&2
echo '' >&2
return 1
fi
echo 'Successfully updated Markdown files.' >&2
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Committing changes...' >&2
git add -A && git commit -m 'Update package URLs'
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to commit changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully committed changes.' >&2
fi
echo '' >&2
return 0
}
# Updates REPL documentation.
update_repl_docs() {
echo 'Updating REPL documentation...' >&2
make repl-docs
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when updating REPL documentation.' >&2
echo '' >&2
return 1
fi
echo 'Successfully updated REPL documentation.' >&2
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Committing changes...' >&2
git add -A && git commit -m 'Update REPL docs'
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to commit changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully committed changes.' >&2
fi
echo '' >&2
return 0
}
# Updates the version in the root package.json.
update_version() {
echo 'Updating project version...' >&2
echo "Current version: ${current_version}" >&2
echo "Release version: ${release_version}" >&2
sed -i '' "s/${current_version}/${release_version}/" "${package_json}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when updating project version.' >&2
echo '' >&2
return 1
fi
echo 'Successfully updated version.' >&2
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Committing changes...' >&2
git add -A && git commit -m "${message}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to commit changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully committed changes.' >&2
fi
echo '' >&2
return 0
}
# Generates and publishes distributable bundles.
publish_bundles() {
echo 'Publishing distributable bundles...' >&2
make dist-bundles-publish
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when attempting to publish distributable bundles.' >&2
echo '' >&2
return 1
fi
echo 'Successfully published distributable bundles.' >&2
echo '' >&2
echo 'Removing bundle artifacts...' >&2
make clean-dist-bundles
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when attempting to remove bundle artifacts.' >&2
echo '' >&2
return 1
fi
echo 'Successfully removed bundled artifacts.' >&2
if [[ -n "$(git status --porcelain)" ]]; then
echo '' >&2
echo 'Committing changes...' >&2
git add -A && git commit -m 'Update dist versions'
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to commit changes.' >&2
echo '' >&2
return 1
fi
echo 'Successfully committed changes.' >&2
fi
echo '' >&2
return 0
}
# Creates (and pushes) a new Git tag.
create_tag() {
echo 'Creating a new tag...' >&2
git tag -a "v${release_version}" -m "${message}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when attempting to create a new tag.' >&2
echo '' >&2
return 1
fi
echo 'Successfully created tag.' >&2
echo '' >&2
return 0
}
# Generates a gzipped archive for publishing to npm.
create_tarball() {
echo 'Generating a gzipped archive for publishing to npm...' >&2
mkdir -p "${npm_tarball_build_out}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to create output build directory.' >&2
echo '' >&2
return 1
fi
npm pack "${root_dir}" >/dev/null
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to create a gzipped archive.' >&2
echo '' >&2
return 1
fi
mv "${root_dir}/${npm_tarball_basename}" "${npm_tarball_build_out}/${npm_tarball_basename}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to move gzipped archive to build directory.' >&2
echo '' >&2
return 1
fi
echo 'Successfully generated a gzipped archive.' >&2
echo '' >&2
return 0
}
# Publishes a tarball to the npm package registry.
publish() {
echo 'Publishing tarball to npm...' >&2
npm publish "${npm_tarball_build_out}/${npm_tarball_basename}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Unable to publish to npm.' >&2
echo '' >&2
return 1
fi
echo 'Successfully published to npm.' >&2
echo '' >&2
return 0
}
# Updates a remote repository.
update_remote_repository() {
echo 'Pushing tags to remote repository...' >&2
git push origin "v${release_version}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when attempting to push tags to remote repository.' >&2
echo '' >&2
return 1
fi
echo 'Successfully pushed tags.' >&2
echo '' >&2
echo 'Pushing commits to remote repository...' >&2
git push origin "${git_branch}"
if [[ "$?" -ne 0 ]]; then
echo 'Failed to push commits. Pulling down any recent changes...' >&2
git pull origin "${git_branch}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Pulling the latest changes failed. Check for merge conflicts.' >&2
echo '' >&2
return 1
fi
echo 'Successfully pulled latest changes.' >&2
git push origin "${git_branch}"
if [[ "$?" -ne 0 ]]; then
echo '' >&2
echo 'Error: unexpected error. Encountered an error when attempting to push commits to remote repository.' >&2
echo '' >&2
return 1
fi
fi
echo 'Successfully pushed commits.' >&2
echo '' >&2
return 0
}
# MAIN #
# Main execution sequence.
main() {
echo '' >&2
check
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
update_repository
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
init
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
check_licenses
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
update_package_meta_data
if [[ "$?" -ne 0 ]]; then
revert_changes
on_error 1
fi
update_namespace_tocs
if [[ "$?" -ne 0 ]]; then
revert_changes
on_error 1
fi
update_markdown_package_urls
if [[ "$?" -ne 0 ]]; then
revert_changes
on_error 1
fi
update_repl_docs
if [[ "$?" -ne 0 ]]; then
revert_changes
on_error 1
fi
update_version
if [[ "$?" -ne 0 ]]; then
revert_changes
on_error 1
fi
# WARNING: at this point, we are at the point of no return and, from this point forward, cannot easily revert changes to the local repository as packages may already be published...
publish_bundles
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
create_tag
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
create_tarball
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
publish
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
update_remote_repository
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
print_success
cleanup
exit 0
}
# Handle arguments...
if [[ "$#" -eq 0 ]]; then
usage
exit 0
elif [[ "$#" -gt 2 ]]; then
echo '' >&2
echo 'Error: unrecognized arguments. Must specify a version and message.' >&2
echo '' >&2
exit 1
fi
# Run main:
main
|