Created
October 24, 2025 17:23
-
-
Save clhenrick/0d763dda0d182479d4ae96ea1dcf498a to your computer and use it in GitHub Desktop.
Script that checks the npm registry to see if a package at a specified version exists. Will optionally recheck the registry for a specified number of attempts with a time delay between attempts.
This file contains hidden or 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
| #!/usr/bin/env zx | |
| import "zx/globals"; | |
| // required cli arguments | |
| /** the name of the npm package to check */ | |
| const packageName = argv["package-name"]; | |
| /** the semantic version of the package to check in major.minor.patch format */ | |
| const version = argv.version; | |
| // optional cli arguments | |
| /** how many times to retry checking for the package version */ | |
| const numberRetries = argv["number-retries"] || 1; | |
| /** how long to wait between retries */ | |
| const retryDelay = argv["retry-delay"] || "60s"; | |
| // messages to log | |
| const messages = { | |
| /** script usage instructions */ | |
| usage: () => | |
| echo( | |
| "usage: `npx zx ./index.mjs --package-name '<npm package>' --version '<major.minor.patch>' --number-retries=<n> --retry-delay=<n>s`" | |
| ), | |
| /** message to log when the package version is found */ | |
| searching: () => echo(`Searching for ${packageName}@${version}...`), | |
| /** message to log when the package version is not found */ | |
| noMatchFound: () => echo(`Package ${packageName}@${version} not found`), | |
| /** message to log when the package version is found */ | |
| success: (dateTimeStr) => | |
| echo(`Package ${packageName}@${version} published on ${dateTimeStr}`), | |
| }; | |
| // helpers | |
| /** exits the script with an exit code */ | |
| const exit = (code = 0) => process.exit(code); | |
| // check for required cli args, if missing print usage and exit | |
| if (!packageName || !version) { | |
| messages.usage(); | |
| exit(); | |
| } | |
| // search for the package version, retrying specified number of times with specified time delay | |
| try { | |
| const result = await retry(numberRetries, retryDelay, async () => { | |
| messages.searching(); | |
| const p = await $`npm show "${packageName}" time --json`; | |
| if (!p.stdout) { | |
| await $`exit 1`; | |
| } | |
| const publishedVersions = JSON.parse(p.stdout); | |
| const match = publishedVersions[version]; | |
| if (match) { | |
| return match; | |
| } | |
| await $`exit 1`; | |
| }); | |
| messages.success(result); | |
| } catch { | |
| messages.noMatchFound(); | |
| } | |
| exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment