Skip to content

Commit

Permalink
Create formatter-web.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Gellipapa authored Jan 20, 2024
1 parent 3033b55 commit 7823ce6
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions .github/workflows/formatter-web.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Code formatter web files with prettier

on:
workflow_call:
inputs:
all-changed-files-list:
required: true
type: string
any-changed-files-list:
required: true
type: string
jobs:
formatter:
runs-on: ubuntu-latest
permissions:
contents: write
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: "16"

- name: Get changed web file extensions
if: ${{ inputs.any-changed-files-list == 'true' }}
id: getext
run: |
import os
files = '${{ inputs.all-changed-files-list }}'.split()
extensions = set('*' + os.path.splitext(file)[1] for file in files)
with open(os.getenv('GITHUB_ENV'), 'a') as f:
f.write(f"CHANGED_EXTENSIONS={' '.join(extensions)}\n")
shell: python

- name: Install Prettier
if: ${{ inputs.any-changed-files-list == 'true' }}
run: |
npm install --save-dev prettier
- name: Check prettier code format
id: check-prettier-format
if: ${{ inputs.any-changed-files-list == 'true' }}
run: |
npx prettier --check ${{ inputs.all-changed-files-list }}
continue-on-error: true

- name: Format changed files with Prettier
if: ${{ inputs.any-changed-files-list == 'true' && steps.check-prettier-format.outcome == 'failure' }}
run: |
npx prettier --write ${{ inputs.all-changed-files-list }}
- name: Generate formatted files list
id: generate-formatted-list
if: ${{ inputs.any-changed-files-list == 'true' && steps.check-prettier-format.outcome == 'failure' }}
run: |
echo -e ":art: Web code formatted\n$(
echo "${{ inputs.all-changed-files-list }}" | tr ' ' '\n' | sed 's/^/Formatted file: /'
)" > commitmessage.txt
- name: Set commit message
if: ${{ inputs.any-changed-files-list == 'true' && steps.check-prettier-format.outcome == 'failure' }}
id: commit-message-step
run: |
echo 'commit_message<<EOF' >> $GITHUB_OUTPUT
cat commitmessage.txt >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
- name: Create or Update .gitignore
if: ${{ inputs.any-changed-files-list == 'true' && steps.check-prettier-format.outcome == 'failure' }}
run: |
echo "node_modules/" >> .gitignore
echo "package.json" >> .gitignore
echo "package-lock.json" >> .gitignore
- name: Update repo before push
if: ${{ inputs.any-changed-files-list == 'true' && steps.check-prettier-format.outcome == 'failure' }}
run: |
git pull
- name: Commit changed files and push current branch
if: ${{ inputs.any-changed-files-list == 'true' && steps.check-prettier-format.outcome == 'failure' }}
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_user_name: ESX GITHUB ACTIONS BOT
commit_user_email: [email protected]
commit_message: ${{ steps.commit-message-step.outputs.commit_message }}
file_pattern: ${{ env.CHANGED_EXTENSIONS }}
status_options: '--untracked-files=no'
skip_fetch: false

0 comments on commit 7823ce6

Please sign in to comment.