|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Build and publish auths-verify to npm. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python scripts/release/npm.py # dry-run (shows what would happen) |
| 7 | + python scripts/release/npm.py --push # build, test, and publish to npm |
| 8 | +
|
| 9 | +What it does: |
| 10 | + 1. Reads the version from package.json |
| 11 | + 2. Checks npm registry to make sure the version has been bumped |
| 12 | + 3. Checks that the git working tree is clean |
| 13 | + 4. Rebuilds WASM, runs tests, and builds the dist |
| 14 | + 5. Publishes to npm with --access public |
| 15 | + 6. Creates and pushes a git tag v{version} |
| 16 | +
|
| 17 | +Requires: |
| 18 | + - python3 (no external dependencies) |
| 19 | + - node/npm on PATH |
| 20 | + - wasm-pack on PATH (for WASM build) |
| 21 | + - git on PATH |
| 22 | + - npm authentication (npm login or NPM_TOKEN) |
| 23 | +""" |
| 24 | + |
| 25 | +import json |
| 26 | +import subprocess |
| 27 | +import sys |
| 28 | +import urllib.request |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 32 | +PACKAGE_JSON = REPO_ROOT / "package.json" |
| 33 | +NPM_REGISTRY_URL = "https://registry.npmjs.org/auths-verify" |
| 34 | + |
| 35 | + |
| 36 | +def get_version() -> str: |
| 37 | + data = json.loads(PACKAGE_JSON.read_text()) |
| 38 | + version = data.get("version") |
| 39 | + if not version: |
| 40 | + print("ERROR: No version found in package.json", file=sys.stderr) |
| 41 | + sys.exit(1) |
| 42 | + return version |
| 43 | + |
| 44 | + |
| 45 | +def get_npm_version() -> str | None: |
| 46 | + req = urllib.request.Request(NPM_REGISTRY_URL, headers={"Accept": "application/json"}) |
| 47 | + try: |
| 48 | + with urllib.request.urlopen(req, timeout=10) as resp: |
| 49 | + data = json.loads(resp.read()) |
| 50 | + return data.get("dist-tags", {}).get("latest") |
| 51 | + except Exception: |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +def git(*args: str) -> str: |
| 56 | + result = subprocess.run( |
| 57 | + ["git", *args], |
| 58 | + capture_output=True, |
| 59 | + text=True, |
| 60 | + cwd=REPO_ROOT, |
| 61 | + ) |
| 62 | + if result.returncode != 0: |
| 63 | + print(f"ERROR: git {' '.join(args)} failed:\n{result.stderr.strip()}", file=sys.stderr) |
| 64 | + sys.exit(1) |
| 65 | + return result.stdout.strip() |
| 66 | + |
| 67 | + |
| 68 | +def local_tag_exists(tag: str) -> bool: |
| 69 | + result = subprocess.run( |
| 70 | + ["git", "tag", "-l", tag], |
| 71 | + capture_output=True, |
| 72 | + text=True, |
| 73 | + cwd=REPO_ROOT, |
| 74 | + ) |
| 75 | + return bool(result.stdout.strip()) |
| 76 | + |
| 77 | + |
| 78 | +def remote_tag_exists(tag: str) -> bool: |
| 79 | + result = subprocess.run( |
| 80 | + ["git", "ls-remote", "--tags", "origin", f"refs/tags/{tag}"], |
| 81 | + capture_output=True, |
| 82 | + text=True, |
| 83 | + cwd=REPO_ROOT, |
| 84 | + ) |
| 85 | + return bool(result.stdout.strip()) |
| 86 | + |
| 87 | + |
| 88 | +def delete_local_tag(tag: str) -> None: |
| 89 | + subprocess.run( |
| 90 | + ["git", "tag", "-d", tag], |
| 91 | + capture_output=True, |
| 92 | + cwd=REPO_ROOT, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def check_tool(name: str) -> None: |
| 97 | + result = subprocess.run(["which", name], capture_output=True) |
| 98 | + if result.returncode != 0: |
| 99 | + print(f"ERROR: {name} not found on PATH", file=sys.stderr) |
| 100 | + sys.exit(1) |
| 101 | + |
| 102 | + |
| 103 | +def run_step(description: str, args: list[str]) -> None: |
| 104 | + print(f"\n{description}...", flush=True) |
| 105 | + result = subprocess.run(args, cwd=REPO_ROOT) |
| 106 | + if result.returncode != 0: |
| 107 | + print(f"\nERROR: {description} failed (exit {result.returncode})", file=sys.stderr) |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + |
| 111 | +def main() -> None: |
| 112 | + push = "--push" in sys.argv |
| 113 | + |
| 114 | + version = get_version() |
| 115 | + tag = f"v{version}" |
| 116 | + print(f"package.json version: {version}") |
| 117 | + print(f"Git tag: {tag}") |
| 118 | + |
| 119 | + # Check npm for version bump |
| 120 | + published = get_npm_version() |
| 121 | + if published: |
| 122 | + print(f"npm latest version: {published}") |
| 123 | + if published == version: |
| 124 | + print(f"\nERROR: Version {version} is already published on npm.", file=sys.stderr) |
| 125 | + print("Bump the version in package.json before releasing.", file=sys.stderr) |
| 126 | + sys.exit(1) |
| 127 | + else: |
| 128 | + print("npm latest version: (not found or not published yet)") |
| 129 | + |
| 130 | + # Check git tag doesn't already exist |
| 131 | + if remote_tag_exists(tag): |
| 132 | + print(f"\nERROR: Git tag {tag} already exists on origin.", file=sys.stderr) |
| 133 | + print("Bump the version in package.json or delete the remote tag first.", file=sys.stderr) |
| 134 | + sys.exit(1) |
| 135 | + |
| 136 | + if local_tag_exists(tag): |
| 137 | + print(f"Local tag {tag} exists but not on origin — deleting stale local tag.") |
| 138 | + delete_local_tag(tag) |
| 139 | + |
| 140 | + # Check working tree is clean |
| 141 | + status = git("status", "--porcelain") |
| 142 | + if status: |
| 143 | + print(f"\nERROR: Working tree is not clean:\n{status}", file=sys.stderr) |
| 144 | + print("Commit or stash changes before releasing.", file=sys.stderr) |
| 145 | + sys.exit(1) |
| 146 | + |
| 147 | + # Check required tools |
| 148 | + check_tool("node") |
| 149 | + check_tool("npm") |
| 150 | + check_tool("wasm-pack") |
| 151 | + |
| 152 | + if not push: |
| 153 | + print(f"\nDry run: would build, test, and publish {version} to npm") |
| 154 | + print(f" would create and push tag {tag}") |
| 155 | + print("Run with --push to execute.") |
| 156 | + return |
| 157 | + |
| 158 | + # Build WASM |
| 159 | + run_step("Building WASM", ["npm", "run", "build:wasm"]) |
| 160 | + |
| 161 | + # Run tests |
| 162 | + run_step("Running tests", ["npm", "test"]) |
| 163 | + |
| 164 | + # Build dist |
| 165 | + run_step("Building dist", ["npm", "run", "build"]) |
| 166 | + |
| 167 | + # Publish to npm |
| 168 | + print("\nPublishing to npm...", flush=True) |
| 169 | + result = subprocess.run( |
| 170 | + ["npm", "publish", "--access", "public"], |
| 171 | + cwd=REPO_ROOT, |
| 172 | + ) |
| 173 | + if result.returncode != 0: |
| 174 | + print(f"\nERROR: npm publish failed (exit {result.returncode})", file=sys.stderr) |
| 175 | + sys.exit(1) |
| 176 | + |
| 177 | + # Create and push git tag |
| 178 | + print(f"\nCreating tag {tag}...", flush=True) |
| 179 | + result = subprocess.run( |
| 180 | + ["git", "tag", "-a", tag, "-m", f"release: {version}"], |
| 181 | + cwd=REPO_ROOT, |
| 182 | + env={**__import__("os").environ, "GIT_EDITOR": "true"}, |
| 183 | + ) |
| 184 | + if result.returncode != 0: |
| 185 | + print(f"\nWARNING: git tag failed (exit {result.returncode})", file=sys.stderr) |
| 186 | + else: |
| 187 | + print(f"Pushing tag {tag} to origin...", flush=True) |
| 188 | + result = subprocess.run( |
| 189 | + ["git", "push", "--no-verify", "origin", tag], |
| 190 | + cwd=REPO_ROOT, |
| 191 | + ) |
| 192 | + if result.returncode != 0: |
| 193 | + print(f"\nWARNING: Failed to push tag {tag}", file=sys.stderr) |
| 194 | + |
| 195 | + print(f"\nDone. Published auths-verify@{version} to npm.") |
| 196 | + print(f" https://www.npmjs.com/package/auths-verify") |
| 197 | + |
| 198 | + |
| 199 | +if __name__ == "__main__": |
| 200 | + main() |
0 commit comments