mirror of
https://github.com/creyD/prettier_action.git
synced 2026-04-12 19:30:30 +02:00
37
README.md
37
README.md
@@ -1,7 +1,7 @@
|
||||
# GitHub Prettier Action
|
||||
|
||||
[](https://www.codefactor.io/repository/github/creyd/prettier_action/overview/master)
|
||||
[](https://github.com/prettier/prettier)
|
||||
[](https://app.codacy.com/gh/creyD/prettier_action/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
||||
[](https://github.com/creyD/prettier_action/blob/master/LICENSE)
|
||||
[](https://github.com/creyD/prettier_action/releases)
|
||||
[](https://github.com/creyD/prettier_action/graphs/contributors)
|
||||
@@ -16,8 +16,9 @@ A GitHub action for styling files with [prettier](https://prettier.io).
|
||||
| Parameter | Required | Default | Description |
|
||||
| - | :-: | :-: | - |
|
||||
| dry | :x: | `false` | Runs the action in dry mode. Files wont get changed and the action fails if there are unprettified files. Recommended to use with prettier_options --check |
|
||||
| prettier_version | :x: | `false` | Specific prettier version (by default use latest) |
|
||||
| working_directory | :x: | `false` | Specify a directory to cd into before installing prettier and running it, use relative file path to the repository root for example `app/` |
|
||||
| no_commit | :x: | `false` | Can be used to avoid committing the changes (useful when another workflow step commits after this one anyways; can be combined with dry mode) |
|
||||
| prettier_version | :x: | `latest` | Specific prettier version (by default use latest) |
|
||||
| working_directory | :x: | `${{ github.action_path }}` | Specify a directory to cd into before installing prettier and running it, use relative file path to the repository root for example `app/` |
|
||||
| prettier_options | :x: | `"--write **/*.js"` | Prettier options (by default it applies to the whole repository) |
|
||||
| commit_options | :x: | - | Custom git commit options |
|
||||
| push_options | :x: | - | Custom git push options |
|
||||
@@ -29,6 +30,7 @@ A GitHub action for styling files with [prettier](https://prettier.io).
|
||||
| clean_node_folder | :x: | `true` | Delete the node_modules folder before committing |
|
||||
| only_changed | :x: | `false` | Only prettify changed files, can't be used with file_pattern! This command works only with the checkout action set to fetch depth '0' (see example 2)|
|
||||
| github_token | :x: | `${{ github.token }}` | The default [GITHUB_TOKEN](https://docs.github.com/en/actions/reference/authentication-in-a-workflow#about-the-github_token-secret) or a [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token)
|
||||
| git_identity | :x: | `actions` | Set to `author` to use author's user as committer. This allows triggering [further workflow runs](https://github.com/peter-evans/create-pull-request/blob/main/docs/concepts-guidelines.md#triggering-further-workflow-runs)
|
||||
|
||||
> Note: using the same_commit option may lead to problems if other actions are relying on the commit being the same before and after the prettier action has ran. Keep this in mind.
|
||||
|
||||
@@ -54,13 +56,10 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
# Make sure the actual branch is checked out when running on pull requests
|
||||
ref: ${{ github.head_ref }}
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Prettify code
|
||||
uses: creyD/prettier_action@v4.3
|
||||
uses: creyD/prettier_action@v4.4
|
||||
with:
|
||||
# This part is also where you can pass other options, for example:
|
||||
prettier_options: --write **/*.{js,md}
|
||||
@@ -81,7 +80,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
# Make sure the actual branch is checked out when running on pull requests
|
||||
ref: ${{ github.head_ref }}
|
||||
@@ -89,7 +88,7 @@ jobs:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Prettify code
|
||||
uses: creyD/prettier_action@v4.3
|
||||
uses: creyD/prettier_action@v4.4
|
||||
with:
|
||||
# This part is also where you can pass other options, for example:
|
||||
prettier_options: --write **/*.{js,md}
|
||||
@@ -111,7 +110,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.head_ref }}
|
||||
@@ -119,7 +118,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Prettify code
|
||||
uses: creyD/prettier_action@v4.3
|
||||
uses: creyD/prettier_action@v4.4
|
||||
with:
|
||||
prettier_options: --write **/*.{js,md}
|
||||
only_changed: True
|
||||
@@ -142,7 +141,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.head_ref }}
|
||||
@@ -150,7 +149,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Prettify code
|
||||
uses: creyD/prettier_action@v4.3
|
||||
uses: creyD/prettier_action@v4.4
|
||||
with:
|
||||
dry: True
|
||||
github_token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
|
||||
@@ -170,3 +169,13 @@ This issue was discussed in https://github.com/creyD/prettier_action/issues/113.
|
||||
- name: Install npm v8
|
||||
run: npm i -g npm@8
|
||||
```
|
||||
|
||||
## Star History
|
||||
|
||||
<a href="https://www.star-history.com/#creyD/prettier_action&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=creyD/prettier_action&type=Date&theme=dark" />
|
||||
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=creyD/prettier_action&type=Date" />
|
||||
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=creyD/prettier_action&type=Date" />
|
||||
</picture>
|
||||
</a>
|
||||
|
||||
22
action.yml
22
action.yml
@@ -23,7 +23,7 @@ inputs:
|
||||
description: Git push options
|
||||
required: false
|
||||
file_pattern:
|
||||
description: File pattern used for `git add`, can't be used with only_changed!
|
||||
description: File pattern used for `git add`, can't be used with only_changed or only_changed_pr!
|
||||
required: false
|
||||
default: "*"
|
||||
prettier_options:
|
||||
@@ -34,18 +34,26 @@ inputs:
|
||||
description: Running the script in dry mode just shows whether there are files that should be prettified or not
|
||||
required: false
|
||||
default: false
|
||||
no_commit:
|
||||
description: Can be used to avoid committing the changes (can be combined with dry mode, useful when another workflow steps commits after this commit anyways)
|
||||
required: false
|
||||
default: false
|
||||
prettier_version:
|
||||
description: Specific version of prettier (by default just use the latest version)
|
||||
required: false
|
||||
default: false
|
||||
default: "latest"
|
||||
working_directory:
|
||||
description: Specify a directory to cd into before installing prettier and running it
|
||||
required: false
|
||||
default: false
|
||||
default: ${{ github.action_path }}
|
||||
only_changed:
|
||||
description: Only prettify files changed in the last commit, can't be used with file_pattern!
|
||||
required: false
|
||||
default: false
|
||||
only_changed_pr:
|
||||
description: Only prettify files changed in the current PR. If specified with only_changed, only_changed will take precedent. Can't be used with file_pattern!
|
||||
required: false
|
||||
default: false
|
||||
prettier_plugins:
|
||||
description: Install Prettier plugins, i.e. `@prettier/plugin-php @prettier/plugin-other`
|
||||
required: false
|
||||
@@ -58,6 +66,10 @@ inputs:
|
||||
description: Remove the node_modules folder before committing changes
|
||||
required: false
|
||||
default: true
|
||||
git_identity:
|
||||
description: Which identity is used for git name/email when committing changes. Needs to be one of "actions" or "author".
|
||||
required: false
|
||||
default: "actions"
|
||||
|
||||
runs:
|
||||
using: "composite"
|
||||
@@ -72,15 +84,19 @@ runs:
|
||||
INPUT_COMMIT_DESCRIPTION: ${{ inputs.commit_description }}
|
||||
INPUT_SAME_COMMIT: ${{ inputs.same_commit }}
|
||||
INPUT_COMMIT_OPTIONS: ${{ inputs.commit_options }}
|
||||
INPUT_PUSH_OPTIONS: ${{ inputs.push_options }}
|
||||
INPUT_FILE_PATTERN: ${{ inputs.file_pattern }}
|
||||
INPUT_PRETTIER_OPTIONS: ${{ inputs.prettier_options }}
|
||||
INPUT_DRY: ${{ inputs.dry }}
|
||||
INPUT_NO_COMMIT: ${{ inputs.no_commit }}
|
||||
INPUT_PRETTIER_VERSION: ${{ inputs.prettier_version }}
|
||||
INPUT_ONLY_CHANGED: ${{ inputs.only_changed }}
|
||||
INPUT_ONLY_CHANGED_PR: ${{ inputs.only_changed_pr }}
|
||||
INPUT_PRETTIER_PLUGINS: ${{ inputs.prettier_plugins }}
|
||||
INPUT_WORKING_DIRECTORY: ${{ inputs.working_directory }}
|
||||
INPUT_GITHUB_TOKEN: ${{ inputs.github_token }}
|
||||
INPUT_CLEAN_NODE_FOLDER: ${{ inputs.clean_node_folder }}
|
||||
INPUT_GIT_IDENTITY: ${{ inputs.git_identity }}
|
||||
|
||||
branding:
|
||||
icon: "award"
|
||||
|
||||
@@ -17,8 +17,17 @@ _git_setup ( ) {
|
||||
EOF
|
||||
chmod 600 $HOME/.netrc
|
||||
|
||||
git config --global user.email "actions@github.com"
|
||||
git config --global user.name "GitHub Action"
|
||||
# If GIT_IDENTITY="actor"
|
||||
if [ "$INPUT_GIT_IDENTITY" = "author" ]; then
|
||||
git config --global user.name "$GITHUB_ACTOR"
|
||||
git config --global user.email "$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
|
||||
@@ -26,33 +35,17 @@ _git_changed() {
|
||||
[[ -n "$(git status -s)" ]]
|
||||
}
|
||||
|
||||
_git_changes() {
|
||||
git diff
|
||||
}
|
||||
|
||||
(
|
||||
# PROGRAM
|
||||
# Changing to the directory
|
||||
cd "$GITHUB_ACTION_PATH"
|
||||
if [ -z "$INPUT_WORKING_DIRECTORY" ] ; then
|
||||
INPUT_WORKING_DIRECTORY=$GITHUB_ACTION_PATH
|
||||
fi
|
||||
cd "$INPUT_WORKING_DIRECTORY"
|
||||
|
||||
echo "Installing prettier..."
|
||||
|
||||
case $INPUT_WORKING_DIRECTORY in
|
||||
false)
|
||||
;;
|
||||
*)
|
||||
cd $INPUT_WORKING_DIRECTORY
|
||||
;;
|
||||
esac
|
||||
|
||||
case $INPUT_PRETTIER_VERSION in
|
||||
false)
|
||||
npm install --silent prettier
|
||||
;;
|
||||
*)
|
||||
npm install --silent prettier@$INPUT_PRETTIER_VERSION
|
||||
;;
|
||||
esac
|
||||
npm install --silent prettier@$INPUT_PRETTIER_VERSION
|
||||
|
||||
# Install plugins
|
||||
if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then
|
||||
@@ -72,7 +65,7 @@ PRETTIER_RESULT=0
|
||||
echo "Prettifying files..."
|
||||
echo "Files:"
|
||||
prettier $INPUT_PRETTIER_OPTIONS \
|
||||
|| { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; exit 1; }
|
||||
|| { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; exit 1; } >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
echo "Prettier result: $PRETTIER_RESULT"
|
||||
|
||||
@@ -87,42 +80,65 @@ if $INPUT_CLEAN_NODE_FOLDER; then
|
||||
fi
|
||||
|
||||
if [ -f 'package-lock.json' ]; then
|
||||
git checkout -- package-lock.json
|
||||
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_changes
|
||||
echo "Finishing dry-run. Exiting before committing."
|
||||
exit 1
|
||||
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 environemnt
|
||||
_git_setup
|
||||
|
||||
if $INPUT_ONLY_CHANGED; then
|
||||
# --diff-filter=d excludes deleted files
|
||||
OLDIFS="$IFS"
|
||||
IFS=$'\n'
|
||||
for file in $(git diff --name-only --diff-filter=d HEAD^..HEAD)
|
||||
do
|
||||
git add "$file"
|
||||
done
|
||||
IFS="$OLDIFS"
|
||||
else
|
||||
# Add changes to git
|
||||
git add "${INPUT_FILE_PATTERN}" || echo "Problem adding your files with pattern ${INPUT_FILE_PATTERN}"
|
||||
# Add changes to git
|
||||
git add "${INPUT_FILE_PATTERN}" || echo "Problem adding your files with 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
|
||||
git commit --amend --no-edit --allow-empty
|
||||
git push origin -f
|
||||
else
|
||||
if [ "$INPUT_COMMIT_DESCRIPTION" != "" ]; then
|
||||
|
||||
Reference in New Issue
Block a user