Skip to content

Instantly share code, notes, and snippets.

@puppybits
Created August 15, 2013 21:21
Show Gist options
  • Save puppybits/6245025 to your computer and use it in GitHub Desktop.
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
#!/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