Skip to content

Instantly share code, notes, and snippets.

@ammmze
Last active June 13, 2023 16:24
Show Gist options
  • Save ammmze/ce38fc64ecb257a8bfd3f98cbf6db8e6 to your computer and use it in GitHub Desktop.
Save ammmze/ce38fc64ecb257a8bfd3f98cbf6db8e6 to your computer and use it in GitHub Desktop.
Surfaces "use client" and "use server" directives to the top of the file. Workaround for https://github.com/swc-project/swc/issues/7315
#!/usr/bin/env sh
# Workaround for https://github.com/swc-project/swc/issues/7315
DIR="${1}"
if [ -z "${DIR}" ]; then
echo "Missing path to search"
echo "Usage: ${0} ./path-to-search"
exit 1
fi
# Default case for Linux sed, just use "-i"
sedi=(-i)
case "$(uname)" in
# For macOS, use two parameters
Darwin*) sedi=(-i "")
esac
find "${DIR}" -type f -print \
| xargs grep -l '"use client";' \
| xargs -I{} sed "${sedi[@]}" -e '/^"use client";$/d' -e '1 i\
"use client";' '{}'
find "${DIR}" -type f -print \
| xargs grep -l '"use server";' \
| xargs -I{} sed "${sedi[@]}" -e '/^"use server";$/d' -e '1 i\
"use server";' '{}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment