-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |