Skip to content

Instantly share code, notes, and snippets.

@Plopix

Plopix/README.md Secret

Last active October 16, 2023 13:52
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plopix/58fc3f3be202d9915c466e71077d36a2 to your computer and use it in GitHub Desktop.
Save Plopix/58fc3f3be202d9915c466e71077d36a2 to your computer and use it in GitHub Desktop.
Remix Migration Scripts

Remix Run - Route V2 Migration Script

Following the rules defined here: https://remix.run/docs/en/main/pages/v2

Run the following in the project directory:

curl -fsSL https://gist.github.com/Plopix/58fc3f3be202d9915c466e71077d36a2/raw/migrate-to-v2-routing.bash | bash -s I_ROUTES_FOLDER O_ROUTES_FOLDER

This script does not replace anything for you but create a new folder instead so you can double-check.

Params are optional, by default:

  • I_ROUTES_FOLDER is set to app/routes
  • O_ROUTES_FOLDER is set to app-routes-v2

License: MIT

Author: https://github.com/plopix - https://twitter.com/plopix


</> with <3 by https://crystallize.com

#!/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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment