Initial commit; implement package adding

parents
packages
registry.json
public
#!/bin/bash
source functions.sh
if [ "$1" == '-h' ] || [ "$1" == '--help' ] || [ $# -le 3 ] ; then
echo 'Usage: add.sh <package> <version> <main> <main-minified>'
echo ' Main and main-minified are paths to files inside package'
echo ' Example (React): ./add.sh react latest umd/react.development.js umd/react.production.min.js'
echo ' Version should be a number or "latest"'
echo ' Example (jQuery): ./add.sh jquery 1 dist/jquery.js dist/jquery.min.js'
exit 0
fi
re='^[0-9\.]+$'
if ! [[ $2 =~ $re ]] && [ "$2" != "latest" ]; then
echo "Bad version: $2"
exit 1
fi
epm assure jq || exit
package="$1"
version="$2"
main="$3"
main_minified="$4"
if ! npm view "$1" > /dev/null 2>&1; then
echo 'Failed to get package info. Are you sure it exists?'
echo 'If not sure, check npm-debug.log for details.'
exit 1
fi
add_package "$package" "$version" "$main" "$main_minified"
registry_add_library_version "$package" "$version" "$main" "$main_minified"
fatal () {
echo "Error: $*" >&2
exit 1
}
add_package () {
local package="$1"
local version="$2"
local main="$3"
local minified="$4"
rm -rf "packages/$package/$version"
create_package_version "$package" "$version"
echo "Created packages/$package/$version/package.json"
install_or_update_package "$package" "$version"
create_dist_link "$package" "$version" "$main" "$minified"
echo "Installed $package@$version"
}
create_dist_link () {
local package="$1"
local version="$2"
local main="$3"
local minified="$4"
public_base="public/$package/$version"
package_base="packages/$package/$version/node_modules/$package"
mkdir -p "$public_base"
#if [ -z "$3" ]; then
# main_file=$(npm view "$1" main)
#else
local main_file="$3"
local main_file_minified="$4"
#fi
link_and_print "$package_base/$main_file_minified" "$public_base/$package.min.js"
link_and_print "$public_base/$package.min.js" "$public_base/$package.js"
link_and_print "$package_base/$main_file" "$public_base/$package.development.js"
}
create_package_version () {
if [ "$2" == "latest" ]; then
semver_version="*"
else
semver_version="$2.*"
fi
mkdir -p "packages/$1/$2"
rm -f "packages/$1/$2/package.json"
cat >> "packages/$1/$2/package.json" << EOL
{
"name": "",
"description": "",
"version": "0.1.0",
"dependencies": {
EOL
echo " \"$1\": \"$semver_version\"" >> "packages/$1/$2/package.json"
cat >> "packages/$1/$2/package.json" << EOL
}
}
EOL
}
install_or_update_package () {
cd "packages/$1/$2/"
if ! npm i > /dev/null 2>&1; then
fatal "Failed to install $1@$2"
fi
cd ../../../
}
link_and_print () {
if ! [ -e "$1" ]; then
fatal "File not found: $1"
fi
ln "$1" "$2"
echo "Created hardlink: $2 -> $1"
}
registry_add_library_version () {
new_registry=$(cat registry.json | jq --arg lib "$1" --arg version "$2" --arg main "$3" --arg minified "$4" ".installedLibraries[\"$1\"][\"$2\"] = {"main": \$main, "minified": \$minified}") || fatal "jq error"
echo "$new_registry" > registry.json
}
#!/bin/bash
if [ -e registry.json ]; then
echo "Already initalized."
exit 1
fi
cat >> registry.json << EOL
{
"installedLibraries": {}
}
EOL
echo "Created new registry.json (list of installed libraries)."
echo "Now use add.sh to add some libraries."
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment