Files
npm-install/npm-install.sh
2022-03-13 09:24:58 +02:00

81 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
print_help() {
echo -e
echo -e "For DOWNLOAD"
echo -e "$0 <package> OR $0 ."
echo -e
echo -e "For UPLOAD"
echo -e "$0 <nexus_host> <repository>"
echo -e "Like this:"
echo -e "<nexus_host>/service/rest/v1/components?repository=<repository>"
echo -e
}
npm_cretae_package_lock_file() {
if [[ $# > 0 ]]; then
local package=$1
fi
npm i --package-lock-only $package
}
download_package() {
local url_package=$1
wget $url_package
}
# insert nexus_host, repository and package to upload
upload_package() {
local nexus_host=$1
local repository=$2
local package=$3
curl -X POST "$nexus_host/service/rest/v1/components?repository=$repository" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "npm.asset=@$package" -i
}
get_all_packages_urls() {
echo $(grep -Eo "https://.+(.tgz)" package-lock.json | sort | uniq)
}
download_all_package() {
local urls_packages=$@
for url_package in ${urls_packages[@]};
do
download_package $url_package
done
}
publish_all_packages() {
local packages_filenames=($(ls *.tgz))
for package_name in ${packages_filenames[@]};
do
upload_package $@ $package_name
done
}
remove_all_local_packages_and_json() {
rm *.tgz *.json
}
echo_npm_registry() {
npm get registry
}
main() {
if [[ $# -eq 1 ]]; then
npm_cretae_package_lock_file $@
urls_packages=($(get_all_packages_urls))
download_all_package ${urls_packages[@]}
elif [[ $# -eq 2 ]]; then
publish_all_packages $@
remove_all_local_packages_and_json
else
print_help
fi
}
main $@