Last active
January 6, 2017 21:43
-
-
Save rjz/9242dd0c3428245a3eda9b0ca9de6709 to your computer and use it in GitHub Desktop.
Updating TypeScript typings to latest versions
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
#!/bin/bash | |
# Update TypeScript `typings` to their latest versions. | |
# | |
# Running this ad-hoc may break your project; be prepared to resolve issues by | |
# hand. | |
# | |
# Released under the terms of the MIT License | |
set -e | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
# Paths to `typings.json` and `typings` executable--adjust to suit! | |
TYPINGSFILE="$DIR/../typings.json" | |
TYPINGSDIR="$DIR/../typings" | |
TYPINGS="${DIR}/../node_modules/.bin/typings" | |
bail () { | |
echo $1 | |
exit 1 | |
} | |
# extract "dt~package" from "registry:dt/package#some-version" | |
typings_lock_to_loc () { | |
echo $1 \ | |
| cut -d':' -f2- \ | |
| cut -d'#' -f1 \ | |
| tr '/' '~' | |
} | |
# iterate over a dependency list in typings.json and attempt to update | |
update_typings () { | |
local key=$1 extra_args=$2 | |
local ts=$(cat "$TYPINGSFILE" \ | |
| jq -r ".${key} | to_entries | .[] | [.key, .value] | @sh") | |
echo "$ts" | while read t; do | |
local name=$(echo $t | cut -d' ' -f1) | |
local lock=$(echo $t | cut -d' ' -f2) | |
local location=$(typings_lock_to_loc "$lock") | |
typings un --save $name 2> /dev/null || echo "Skipped removing $name" | |
typings install $location --save $extra_args | |
done | |
} | |
# Check dependencies | |
command -v jq > /dev/null \ | |
|| bail 'Could not find jq (See: https://stedolan.github.io/jq/)' | |
[[ -x "$TYPINGS" ]] \ | |
|| bail "Could not run typings; please set \$TYPINGS in $0" | |
[[ -f "$TYPINGSFILE" ]] \ | |
|| bail "Could not find typings.json; please set \$TYPINGSFILE in $0" | |
# Remove existing typings directory | |
[[ -d "$TYPINGSDIR" ]] && { | |
rm -rf "$TYPINGSDIR" | |
} | |
# Update typings | |
update_typings dependencies | |
update_typings globalDependencies --global |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment