Merge pull request #92 from cerc-io/dboreham/check-npm-version-exists

Add test for package version already exists
This commit is contained in:
David Boreham 2023-01-06 10:21:59 -07:00 committed by GitHub
commit 731a9f2072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 7 deletions

View File

@ -36,6 +36,7 @@ def command(ctx, include, exclude):
verbose = ctx.obj.verbose verbose = ctx.obj.verbose
dry_run = ctx.obj.dry_run dry_run = ctx.obj.dry_run
local_stack = ctx.obj.local_stack local_stack = ctx.obj.local_stack
debug = ctx.obj.debug
if local_stack: if local_stack:
dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")] dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")]
@ -57,7 +58,7 @@ def command(ctx, include, exclude):
def build_package(package): def build_package(package):
if not quiet: if not quiet:
print(f"Building: {package}") print(f"Building npm package: {package}")
repo_dir = package repo_dir = package
repo_full_path = os.path.join(dev_root_path, repo_dir) repo_full_path = os.path.join(dev_root_path, repo_dir)
# TODO: make the npm registry url configurable. # TODO: make the npm registry url configurable.
@ -65,12 +66,13 @@ def command(ctx, include, exclude):
if not dry_run: if not dry_run:
if verbose: if verbose:
print(f"Executing: {build_command}") print(f"Executing: {build_command}")
envs = {"CERC_NPM_AUTH_TOKEN": os.environ["CERC_NPM_AUTH_TOKEN"]} | ({"CERC_SCRIPT_DEBUG": "true"} if debug else {})
build_result = docker.run("cerc/builder-js", build_result = docker.run("cerc/builder-js",
remove=True, remove=True,
interactive=True, interactive=True,
tty=True, tty=True,
user=f"{os.getuid()}:{os.getgid()}", user=f"{os.getuid()}:{os.getgid()}",
envs={"CERC_NPM_AUTH_TOKEN": os.environ["CERC_NPM_AUTH_TOKEN"]}, envs=envs,
add_hosts=[("host.docker.internal", "host-gateway")], add_hosts=[("host.docker.internal", "host-gateway")],
volumes=[(repo_full_path, "/workspace")], volumes=[(repo_full_path, "/workspace")],
command=build_command command=build_command

8
cli.py
View File

@ -24,11 +24,12 @@ CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
class Options(object): class Options(object):
def __init__(self, quiet, verbose, dry_run, local_stack): def __init__(self, quiet, verbose, dry_run, local_stack, debug):
self.quiet = quiet self.quiet = quiet
self.verbose = verbose self.verbose = verbose
self.dry_run = dry_run self.dry_run = dry_run
self.local_stack = local_stack self.local_stack = local_stack
self.debug = debug
@click.group(context_settings=CONTEXT_SETTINGS) @click.group(context_settings=CONTEXT_SETTINGS)
@ -36,11 +37,12 @@ class Options(object):
@click.option('--verbose', is_flag=True, default=False) @click.option('--verbose', is_flag=True, default=False)
@click.option('--dry-run', is_flag=True, default=False) @click.option('--dry-run', is_flag=True, default=False)
@click.option('--local-stack', is_flag=True, default=False) @click.option('--local-stack', is_flag=True, default=False)
@click.option('--debug', is_flag=True, default=False)
# See: https://click.palletsprojects.com/en/8.1.x/complex/#building-a-git-clone # See: https://click.palletsprojects.com/en/8.1.x/complex/#building-a-git-clone
@click.pass_context @click.pass_context
def cli(ctx, quiet, verbose, dry_run, local_stack): def cli(ctx, quiet, verbose, dry_run, local_stack, debug):
"""Laconic Stack Orchestrator""" """Laconic Stack Orchestrator"""
ctx.obj = Options(quiet, verbose, dry_run, local_stack) ctx.obj = Options(quiet, verbose, dry_run, local_stack, debug)
cli.add_command(setup_repositories.command, "setup-repositories") cli.add_command(setup_repositories.command, "setup-repositories")

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Usage: build-npm-package.sh <registry-url> <publish-with-this-version> # Usage: build-npm-package.sh <registry-url> <publish-with-this-version>
# Note: supply the registry auth token in CERC_NPM_AUTH_TOKEN # Note: supply the registry auth token in CERC_NPM_AUTH_TOKEN
if [ -n "$CERC_SCRIPT_DEBUG" ]; then if [[ -n "$CERC_SCRIPT_DEBUG" ]]; then
set -x set -x
fi fi
if ! [[ $# -eq 1 || $# -eq 2 ]]; then if ! [[ $# -eq 1 || $# -eq 2 ]]; then
@ -17,11 +17,19 @@ if [[ $# -eq 2 ]]; then
else else
package_publish_version=$( cat package.json | jq -r .version ) package_publish_version=$( cat package.json | jq -r .version )
fi fi
# Get the name of this package from package.json since we weren't passed that
package_name=$( cat package.json | jq -r .name )
local_npm_registry_url=$1 local_npm_registry_url=$1
npm config set @lirewine:registry ${local_npm_registry_url} npm config set @lirewine:registry ${local_npm_registry_url}
npm config set @cerc-io:registry ${local_npm_registry_url} npm config set @cerc-io:registry ${local_npm_registry_url}
npm config set -- ${local_npm_registry_url}:_authToken ${CERC_NPM_AUTH_TOKEN} npm config set -- ${local_npm_registry_url}:_authToken ${CERC_NPM_AUTH_TOKEN}
echo "Build and publish version ${package_publish_version}" # First check if the version of this package we're trying to build already exists in the registry
package_exists=$( yarn info --json ${package_name}@${package_publish_version} | jq -r .data.dist.tarball )
if [[ -n "$package_exists" ]]; then
echo "${package_publish_version} of ${package_name} already exists in the registry, skipping build"
exit 0
fi
echo "Build and publish ${package_name} version ${package_publish_version}"
yarn install yarn install
yarn build yarn build
yarn publish --non-interactive --new-version ${package_publish_version} --no-git-tag-version yarn publish --non-interactive --new-version ${package_publish_version} --no-git-tag-version