Created
August 15, 2013 21:21
-
-
Save puppybits/6245025 to your computer and use it in GitHub Desktop.
Script to refactor require.js modules by searching the disk for the current folder location and updating the define 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 | |
TFILE="out.tmp.$$" | |
SOURCE_FOLDER="/full/path/to/my/source" | |
ROOT_RELATIVE="/full/path/to/root/for/requirejs/" | |
while read f | |
do | |
# get the file name | |
i=$(basename "$f") | |
# remove the extension | |
clazz="${i%.*}" | |
# remove the full path to make the require.js | |
relative=${f#$ROOT_RELATIVE} | |
# remove the file extension | |
relative="${relative%.*}" | |
# escape slashs for when it's used in sed's regex | |
relative=$(echo $relative | sed "s/\//\\\\\//g") | |
echo replacing class: $clazz to location: $relative | |
# Get all JS files that need the require.js define's updated | |
while read f2 | |
do | |
# test if the class is being used | |
if grep -q $clazz "$f2"; then | |
# replace the require.js define import to the new location | |
sed "s/'.*\/$clazz'/'$relative'/g" "$f2" > $TFILE && mv $TFILE "$f2" | |
fi | |
done < <(find $SOURCE_FOLDER -iname *.JS) | |
# Get a list of all the JS file locations so we can update require.js's defines | |
done < <(find $SOURCE_FOLDER -iname *.JS) | |
# after the script runs, check Git's stage to validate all the defines are updated but nothing else | |
# if anything is wrong then revert that change from the stage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment