Skip to content

Instantly share code, notes, and snippets.

@EtherDream
Last active August 4, 2022 10:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EtherDream/f9f01a2bc73d055f8f861eca29e83b8a to your computer and use it in GitHub Desktop.
Save EtherDream/f9f01a2bc73d055f8f861eca29e83b8a to your computer and use it in GitHub Desktop.
# see https://github.com/EtherDream/freecdn/blob/master/examples/free-host/npm-upload.sh
IFS=$'\n'
if [[ "$NPM_PKG" == "" ]]; then
echo "\$NPM_PKG is not specified"
exit
fi
if [[ "$*" == "" ]]; then
echo "file list empty"
exit
fi
tmp=$(mktemp -d)
for src in $*; do
hash=$(openssl dgst -sha256 -binary $src | openssl base64 -A)
key=$(sed 's/[^a-zA-Z0-9]//g' <<< $hash | head -c 16)
ver=0.0.0-$key
pkg=$NPM_PKG@$ver
dst=$pkg/index.js
echo "upload: $src ($hash)"
list="$list
$hash https://unpkg.com/$dst
$hash https://cdn.jsdelivr.net/npm/$dst
"
str=$(npm view $pkg name)
if [[ $str == $NPM_PKG ]]; then
echo "file existed: https://unpkg.com/$dst"
continue
fi
rm -f $tmp/*
cp $src $tmp/index.js
echo '{"name":"'$NPM_PKG'","version":"'$ver'"}' > $tmp/package.json
npm publish $tmp --quiet
echo "uploaded: https://unpkg.com/$dst"
done
rm -rf $tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment