Skip to content

Instantly share code, notes, and snippets.

@vidhill
Created October 13, 2024 18:04
Show Gist options
  • Save vidhill/f596b44e79c1e089c17b395bca2fd622 to your computer and use it in GitHub Desktop.
Save vidhill/f596b44e79c1e089c17b395bca2fd622 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Mostly for JS projects
#
# In the scenario where you end up with multiple same name files,
# e.g. `my-component.tsx` `my-component.spec.ts` `my-component.module.css`
#
# It is much tidier to put all related files inside a folder and use an index file naming convention
# e.g. `my-component/index.tsx` `my-component/index.spec.ts` `my-component/index.module.css`
#
# A nice feature of this, is that the import:
# `import { Foo } from "./my-component";`
#
# will still work without requiring any changes
#
# This script, when run with `./nest.sh my-component.tsx`
# Will create a folder if required, move and rename the file into the folder
# Result: `my-component/index.tsx`
#
#
if [[ ! -e "$1" ]]; then
echo "File does not exist"
exit 1
fi
FILE_NAME=$(basename "$1")
DIRNAME=$(dirname "$1")
EXTENSIONS=(
".spec.ts"
".stories.ts"
".ts"
".stories.tsx"
".spec.tsx"
".tsx"
".stories.js"
".spec.js"
".js"
".stories.jsx"
".spec.jsx"
".jsx"
".module.css"
".css"
)
# Get the base name
BASE=$(basename "$1")
for ext in "${EXTENSIONS[@]}"; do
BASE="${BASE%"$ext"}"
done
if [[ "$FILE_NAME" == "$BASE" ]]; then
echo "Unsupported file extension '$1'"
exit 1
fi
# 'subtract' $BASE from $FILE_NAME to get the remaining, the extension
EXTENSION="${FILE_NAME#"$BASE"}"
NEW_DIR_PATH="$DIRNAME/$BASE"
mkdir -p "$NEW_DIR_PATH"
NEW_FILE_NAME="$NEW_DIR_PATH/index$EXTENSION"
if git rev-parse --git-dir >/dev/null 2>&1; then
# we are in a git repository
if git ls-files -o -X .gitignore | grep -q "$1"; then
# File '$1' is not under version control
mv "$1" "$NEW_FILE_NAME"
else
# File '$1' is under version control, using 'git mv'
git mv "$1" "$NEW_FILE_NAME"
fi
else
# not in a git repo
mv "$1" "$NEW_FILE_NAME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment