Created
February 23, 2018 22:08
-
-
Save kontrollanten/9ffeaa5ccfb98321662091c54480bcc6 to your computer and use it in GitHub Desktop.
Migrate AngularJS templates to ES6 imports
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 | |
javascript_base_path="$PWD/src/app" | |
while read -r file; do | |
IFS=' ' | |
declare -a controllers | |
i=0 | |
while ((i++)); read -r ctrl_name; do | |
controllers[i]=$ctrl_name | |
done <<< $(sed -n "s/controller: [^']*'\([^']*\)'.*/\1/p" $file) | |
declare -a templates | |
i=0 | |
while ((i++)); read -r template_url; do | |
templates[i]=$template_url | |
done <<< $(sed -n "s/templateUrl: [^']*'\([^']*\)'.*/\1/p" $file) | |
imports="" | |
for index in ${!controllers[*]}; do | |
# Create template name based on the controller name (assumes controller is named [Name]Ctrl) | |
template_name=${controllers[index]/Ctrl/Template} | |
# Get template path relative to current file | |
template_path=$(realpath --relative-to="$(dirname $file)" "$javascript_base_path""${templates[index]}") | |
# Generate import statement | |
import_statement=$(printf "import %s from './%s'" "${template_name}" "$template_path") | |
# Replace `templateUrl: '.../[filename].html'` with `template: templateName` | |
sed -i "s/templateUrl:.*'/template: $template_name/g" $file | |
# Add import statement after `import angular` | |
sed -i '/import angular/a'"$import_statement" $file | |
unset import_statement | |
done | |
unset controllers | |
unset templates | |
unset imports | |
done <<< $(grep --include \*.js -rl templateUrl $javascript_base_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment