Files
prettier_action/entrypoint.sh
Claude a5f1df5578 Add use_repo_config parameter to automatically detect prettier configuration
This addresses GitHub issue #41 by adding an optional `use_repo_config` parameter
that allows the action to automatically detect and use the repository's prettier
configuration files and .prettierignore.

Changes:
- Added `use_repo_config` input parameter (default: false for backward compatibility)
- Implemented config file detection in entrypoint.sh for all prettier config formats
- Automatically changes file pattern to current directory when using repo config
- Provides helpful messages about found config files and warnings when none exist
- Updated README with new parameter documentation and Example 5

Benefits:
- Users no longer need to manually specify --config or --ignore-path
- Eliminates confusion about whether config files are being used
- Maintains backward compatibility (opt-in via parameter)
- Supports all prettier configuration file formats

Related: #41
2025-11-17 15:40:12 +00:00

206 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
# e is for exiting the script automatically if a command fails, u is for exiting if a variable is not set
# x would be for showing the commands before they are executed
set -eu
shopt -s globstar
# FUNCTIONS
# Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh)
_git_setup ( ) {
cat <<- EOF > $HOME/.netrc
machine github.com
login $GITHUB_ACTOR
password $INPUT_GITHUB_TOKEN
machine api.github.com
login $GITHUB_ACTOR
password $INPUT_GITHUB_TOKEN
EOF
chmod 600 $HOME/.netrc
# If GIT_IDENTITY="actor"
if [ "$INPUT_GIT_IDENTITY" = "author" ]; then
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR_ID+$GITHUB_ACTOR@users.noreply.github.com"
elif [ "$INPUT_GIT_IDENTITY" = "actions" ]; then
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Action"
else
echo "GIT_IDENTITY must be either 'author' or 'actions'";
exit 1;
fi;
}
# Checks if any files are changed
_git_changed() {
[[ -n "$(git status -s)" ]]
}
(
# PROGRAM
# Changing to the directory
if [ -z "$INPUT_WORKING_DIRECTORY" ] ; then
INPUT_WORKING_DIRECTORY=$GITHUB_ACTION_PATH
fi
cd "$INPUT_WORKING_DIRECTORY"
echo "Installing prettier..."
npm install --silent prettier@$INPUT_PRETTIER_VERSION
# Install plugins
if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then
if [ "$INPUT_ALLOW_OTHER_PLUGINS" != "true" ]; then
for plugin in $INPUT_PRETTIER_PLUGINS; do
echo "Checking plugin: $plugin"
# check regex against @prettier/xyz
if ! echo "$plugin" | grep -Eq '(@prettier\/plugin-|(@[a-z\-]+\/)?prettier-plugin-){1}([a-z\-]+)'; then
echo "$plugin does not seem to be a valid @prettier/plugin-x plugin. Exiting."
exit 1
fi
done
fi
npm install --silent $INPUT_PRETTIER_PLUGINS
fi
)
# Handle repository configuration files
if [ "$INPUT_USE_REPO_CONFIG" = "true" ]; then
echo "Using repository prettier configuration..."
# Check for prettier config files
CONFIG_FOUND=false
for config_file in .prettierrc .prettierrc.json .prettierrc.yml .prettierrc.yaml .prettierrc.js .prettierrc.cjs .prettierrc.mjs prettier.config.js prettier.config.cjs prettier.config.mjs .prettierrc.toml; do
if [ -f "$config_file" ]; then
echo " Found prettier config: $config_file"
CONFIG_FOUND=true
break
fi
done
# Check package.json for prettier config
if [ -f "package.json" ] && grep -q '"prettier"' package.json; then
echo " Found prettier config in package.json"
CONFIG_FOUND=true
fi
if [ "$CONFIG_FOUND" = "false" ]; then
echo " Warning: No prettier config file found. Prettier will use its default configuration."
fi
# Check for .prettierignore
if [ -f ".prettierignore" ]; then
echo " Found .prettierignore file"
else
echo " Note: No .prettierignore file found. All files will be processed."
fi
# When using repo config, if prettier_options is still the default, use a more sensible pattern
if [ "$INPUT_PRETTIER_OPTIONS" = "--write **/*.js" ]; then
echo " Using default file pattern: . (current directory)"
INPUT_PRETTIER_OPTIONS="--write ."
else
echo " Using custom prettier_options: $INPUT_PRETTIER_OPTIONS"
fi
fi
PRETTIER_RESULT=0
echo "Prettifying files..."
echo "Files:"
npx prettier $INPUT_PRETTIER_OPTIONS \
|| { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; exit 1; } >> $GITHUB_STEP_SUMMARY
echo "Prettier result: $PRETTIER_RESULT"
# Removing the node_modules folder, so it doesn't get committed if it is not added in gitignore
if $INPUT_CLEAN_NODE_FOLDER; then
echo "Deleting node_modules/ folder..."
if [ -d 'node_modules' ]; then
rm -r node_modules/
else
echo "No node_modules/ folder."
fi
fi
if [ -f 'package-lock.json' ]; then
git checkout -- package-lock.json || echo "No package-lock.json file tracked by git."
else
echo "No package-lock.json file."
fi
# If running under only_changed, reset every modified file that wasn't also modified in the last commit
# This allows only_changed and dry to work together, and simplified the non-dry logic below
if [ $INPUT_ONLY_CHANGED = true ] || [ $INPUT_ONLY_CHANGED_PR = true ] ; then
BASE_BRANCH=origin/$GITHUB_BASE_REF
if $INPUT_ONLY_CHANGED; then
BASE_BRANCH=HEAD~1
fi
echo "Resetting changes, removing changes to files not changed since $BASE_BRANCH"
# list of all files changed in the previous commit
git diff --name-only HEAD $BASE_BRANCH > /tmp/prev.txt
# list of all files with outstanding changes
git diff --name-only HEAD > /tmp/cur.txt
OLDIFS="$IFS"
IFS=$'\n'
# get all files that are in prev.txt that aren't also in cur.txt
for file in $(comm -1 -3 /tmp/prev.txt /tmp/cur.txt)
do
echo "resetting: $file"
git restore -- "$file"
done
IFS="$OLDIFS"
fi
# To keep runtime good, just continue if something was changed
if _git_changed; then
# case when --write is used with dry-run so if something is unpretty there will always have _git_changed
if $INPUT_DRY; then
echo "Unpretty Files Changes:"
git diff
if $INPUT_NO_COMMIT; then
echo "There are changes that won't be commited, you can use an external job to do so."
else
echo "Finishing dry-run. Exiting before committing."
exit 1
fi
else
# Calling method to configure the git environment
_git_setup
# Add changes to git
git add ${INPUT_ADD_OPTIONS:+"$INPUT_ADD_OPTIONS"} "${INPUT_FILE_PATTERN}" || echo -e "Problem adding your files via 'git add':\n flags: ${INPUT_ADD_OPTIONS:+"$INPUT_ADD_OPTIONS"}\n pattern: ${INPUT_FILE_PATTERN}"
if $INPUT_NO_COMMIT; then
echo "There are changes that won't be commited, you can use an external job to do so."
exit 0
fi
# Commit and push changes back
if $INPUT_SAME_COMMIT; then
echo "Amending the current commit..."
git pull
git commit --amend --no-edit --allow-empty
git push origin -f
else
if [ "$INPUT_COMMIT_DESCRIPTION" != "" ]; then
git commit -m "$INPUT_COMMIT_MESSAGE" -m "$INPUT_COMMIT_DESCRIPTION" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} || echo "No files added to commit"
else
git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"} || echo "No files added to commit"
fi
git push origin ${INPUT_PUSH_OPTIONS:-}
fi
echo "Changes pushed successfully."
fi
else
# case when --check is used so there will never have something to commit but there are unpretty files
if [ "$PRETTIER_RESULT" -eq 1 ]; then
echo "Prettier found unpretty files!"
exit 1
else
echo "Finishing dry-run."
fi
echo "No unpretty files!"
echo "Nothing to commit. Exiting."
fi