Skip to content

Instantly share code, notes, and snippets.

@xcsrz
Created April 24, 2019 03:06
Show Gist options
  • Save xcsrz/ad7ee91af31a9ea3c927b5e26169418a to your computer and use it in GitHub Desktop.
Save xcsrz/ad7ee91af31a9ea3c927b5e26169418a to your computer and use it in GitHub Desktop.
Python function to wait for a recently published version of an npm package to become available. Too many times I've been bitten by npm publish returning but the package not becoming available for several seconds, minutes and sometimes even hours later. If you automate your deployment with python this can help you too.
import sys
import json
import time
# pip install npm
from npm.bindings import npm_run
def wait(package, version):
while True:
err, raw = npm_run('view','--json', package)
sys.stdout.write('.')
sys.stdout.flush()
if err:
raise Exception('npm view returned an error: ' + str(err))
info = json.loads(raw)
if version in info['versions']:
print('')
break
time.sleep(1)
wait(*sys.argv[1:])
# python wait-until-published.py PACKAGE VERSION
# python wait-until-published.py npm 6.9.0
# python wait-until-published.py @org/pkg 1.2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment