|
#!/usr/bin/env bash |
|
|
|
SOURCE_DIR=${1:-"app/routes"} |
|
DEST_DIR=${2:-"app/routes-v2"} |
|
|
|
echo "" |
|
echo "..:: Migrating to Remix V2 routing ::.." |
|
echo "</> with <3 for you by https://crystallize.com" |
|
echo "" |
|
|
|
if [ ! -d "$SOURCE_DIR" ]; then |
|
echo "Source directory does not exist. Exiting." |
|
exit 1 |
|
fi |
|
|
|
if [ -d "$DEST_DIR" ]; then |
|
echo "Destination directory already exist. Exiting." |
|
exit 1 |
|
fi |
|
|
|
mkdir -p ${DEST_DIR} |
|
|
|
ABSOLUTE_DEST_DIR=`readlink -f ${DEST_DIR}` |
|
|
|
echo "Going to: ${SOURCE_DIR}" |
|
cd ${SOURCE_DIR} |
|
for ITEM in `find . -type f` |
|
do |
|
OG_ITEM=`echo ${ITEM} | sed 's/^\.\///'` |
|
NORMALIZED_ITEM=${OG_ITEM} |
|
|
|
# suffixed_ underscores in segments opt-out of nesting with a potentially matching parent route instead of dots (.). |
|
# this one is tricky we need to check if the folder exist to know if we replace with a suffix underscore or not |
|
oldIFS="$IFS" |
|
SEPARTOR='.' |
|
IFS="$SEPARTOR" |
|
PARTS=(${NORMALIZED_ITEM}) |
|
ONGOING_TREE="" |
|
for PART in ${PARTS[@]} |
|
do |
|
ONGOING_TREE="${ONGOING_TREE}${PART}/" |
|
if [ -d "${ONGOING_TREE}" ]; then |
|
IFS="$oldIFS" |
|
NORMALIZED_ITEM=`echo ${NORMALIZED_ITEM} | sed "s:${PART}:${PART}_:g"` |
|
IFS="$SEPARTOR" |
|
fi |
|
done |
|
IFS="$oldIFS" |
|
|
|
# _prefixed underscores in segments create layout routes without a path instead of a __double underscore prefix. |
|
DOUBLE_UNDERSCORE=`echo ${NORMALIZED_ITEM} | sed 's/__/_/g'` |
|
|
|
# Route nesting is now created by dots (.) in file names instead of folder nesting |
|
FOLDER_CHANGE=`echo ${DOUBLE_UNDERSCORE} | sed 's/\//./g'` |
|
|
|
# _index.tsx files create index routes instead of index.tsx |
|
PREFIX_INDEX=`echo ${FOLDER_CHANGE} | sed 's/\.index./\._index\./g'` |
|
ROOT_INDEX=`echo ${FOLDER_CHANGE} | sed 's/^index./_index\./g'` |
|
|
|
echo "- '${OG_ITEM}' -> '${ROOT_INDEX}'" |
|
cp "${ITEM}" "${ABSOLUTE_DEST_DIR}/${ROOT_INDEX}" |
|
done |
|
|
|
echo -n "Back to: " |
|
cd - |
|
echo "Done! New files are in ${DEST_DIR}" |
|
echo "Now you can double-check the changes, remove '${SOURCE_DIR}' and rename '${DEST_DIR}' to '${SOURCE_DIR}'." |
|
echo "Something like: 'mv ${SOURCE_DIR} /dev/null && mv ${DEST_DIR} ${SOURCE_DIR}'" |
|
echo "" |
|
|
|
echo "Enjoy Remix V2 routing!" |
|
echo "" |
|
|
|
echo "Follow us on Twitter:" |
|
echo "- https://twitter.com/crystallizeAPI" |
|
echo "- https://twitter.com/plopix" |
|
|