Format JavaScript Files workflow (horsicq/Detect-It-Easy)
The Format JavaScript Files workflow from horsicq/Detect-It-Easy, explained and optimized by Latchkey.
CI health: C - fair
Point runs-on at Latchkey and get run de-duplication, job timeouts, self-healing for flaky steps, and up to 58% lower cost, applied automatically.
What it does
This is the Format JavaScript Files workflow from the horsicq/Detect-It-Easy repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.
Below, Latchkey shows a faster, safer version produced by its optimization engine.
The workflow
name: Format JavaScript Files
on:
push:
paths:
- 'db/**'
- 'db_custom/**'
- 'db_extra/**'
branches:
- master
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
format:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
token: ${{ secrets. RELEASE_TOKEN }}
- name: Configure git to preserve line endings
run: |
git config --local core.autocrlf false
git config --local core.eol lf
- name: Format JavaScript files in db directories
run: |
find db db_custom db_extra -type f \
! -path '*/.vscode/*' \
! -path '*/_icons/*' \
! -name '*.txt' \
! -name '*.md' \
! -name '*.png' \
! -name '*.ico' \
! -name '*.svg' \
-print0 2>/dev/null | while IFS= read -r -d '' file; do
# Skip files with beautify ignore comment (with or without spaces)
if grep -qE 'beautify ignore:\s*start' "$file" 2>/dev/null; then
echo "Skipping (beautify ignore): $file"
continue
fi
# Check if file contains JavaScript-like syntax
if head -c 1000 "$file" 2>/dev/null | grep -qE '(function|var |if\s*\(|for\s*\(|return |includeScript)'; then
echo "Formatting: $file"
# Save original file for comparison
cp "$file" "$file.orig"
# Remove trailing whitespace only (preserve everything else)
perl -pi -e 's/[ \t]+$//' "$file"
# Convert tabs to 4 spaces
perl -pi -e 's/\t/ /g' "$file"
# If file is unchanged, restore original to avoid any byte differences
if cmp -s "$file" "$file.orig"; then
mv "$file.orig" "$file"
else
rm "$file.orig"
fi
fi
done
- name: Check for changes
id: check_changes
run: |
if git diff --quiet; then
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "has_changes=true" >> $GITHUB_OUTPUT
fi
- name: Commit and push changes
if: steps.check_changes.outputs.has_changes == 'true'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add -A
git commit -m "style: auto-format JavaScript files in db directories"
git push
The same workflow, on Latchkey
Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.
name: Format JavaScript Files on: push: paths: - 'db/**' - 'db_custom/**' - 'db_extra/**' branches: - master # Allows you to run this workflow manually from the Actions tab workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: format: timeout-minutes: 30 runs-on: latchkey-small permissions: contents: write steps: - name: Checkout repository uses: actions/checkout@v6 with: token: ${{ secrets. RELEASE_TOKEN }} - name: Configure git to preserve line endings run: | git config --local core.autocrlf false git config --local core.eol lf - name: Format JavaScript files in db directories run: | find db db_custom db_extra -type f \ ! -path '*/.vscode/*' \ ! -path '*/_icons/*' \ ! -name '*.txt' \ ! -name '*.md' \ ! -name '*.png' \ ! -name '*.ico' \ ! -name '*.svg' \ -print0 2>/dev/null | while IFS= read -r -d '' file; do # Skip files with beautify ignore comment (with or without spaces) if grep -qE 'beautify ignore:\s*start' "$file" 2>/dev/null; then echo "Skipping (beautify ignore): $file" continue fi # Check if file contains JavaScript-like syntax if head -c 1000 "$file" 2>/dev/null | grep -qE '(function|var |if\s*\(|for\s*\(|return |includeScript)'; then echo "Formatting: $file" # Save original file for comparison cp "$file" "$file.orig" # Remove trailing whitespace only (preserve everything else) perl -pi -e 's/[ \t]+$//' "$file" # Convert tabs to 4 spaces perl -pi -e 's/\t/ /g' "$file" # If file is unchanged, restore original to avoid any byte differences if cmp -s "$file" "$file.orig"; then mv "$file.orig" "$file" else rm "$file.orig" fi fi done - name: Check for changes id: check_changes run: | if git diff --quiet; then echo "has_changes=false" >> $GITHUB_OUTPUT else echo "has_changes=true" >> $GITHUB_OUTPUT fi - name: Commit and push changes if: steps.check_changes.outputs.has_changes == 'true' run: | git config --local user.email "github-actions[bot]@users.noreply.github.com" git config --local user.name "github-actions[bot]" git add -A git commit -m "style: auto-format JavaScript files in db directories" git push
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Add a job timeout so a hung step cannot burn hours of runner time.
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.