Skip to content

Instantly share code, notes, and snippets.

@Kirill89
Last active January 3, 2019 20:50
Show Gist options
  • Save Kirill89/982411f5b4e3dd169e78610a34a9960b to your computer and use it in GitHub Desktop.
Save Kirill89/982411f5b4e3dd169e78610a34a9960b to your computer and use it in GitHub Desktop.
Convert JavaScript to TypeScript
#!/usr/bin/env bash
if ! [[ -x "$(command -v lebab)" ]]; then
echo 'Error: lebab is not installed.' >&2
echo 'Please run: npm install -g lebab tslint' >&2
exit 1
fi
if ! [[ -x "$(command -v tslint)" ]]; then
echo 'Error: tslint is not installed.' >&2
echo 'Please run: npm install -g lebab tslint' >&2
exit 1
fi
if [[ -z "$1" ]]; then
echo 'Usage: ./jsts.sh full/path/to/project relative/path/to/file.js' >&1
exit 0
fi
if [[ -z "$2" ]]; then
echo 'Usage: ./jsts.sh full/path/to/project relative/path/to/file.js' >&1
exit 0
fi
cd "$1"
TARGET="$2"
TARGET=${TARGET%???}
echo "New file is $TARGET.ts"
mv "$TARGET.js" "$TARGET.ts"
lebab --replace "$TARGET.ts" --transform arrow
lebab --replace "$TARGET.ts" --transform no-strict
lebab --replace "$TARGET.ts" --transform let
lebab --replace "$TARGET.ts" --transform template
lebab --replace "$TARGET.ts" --transform commonjs
tslint --fix --project . "$TARGET.ts"
# Yep, twice. I have no ideas why it works only this way.
tslint --fix --project . "$TARGET.ts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment