From cf6f04b54e5b66753862cb3d65a3b5c2f6cd84b1 Mon Sep 17 00:00:00 2001 From: nospame Date: Tue, 5 Nov 2024 13:10:01 -0800 Subject: [PATCH 1/7] Add Prettier dependency --- package.json | 2 ++ yarn.lock | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/package.json b/package.json index 6dc0233c947e..92c1e87c7c54 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,8 @@ "lodash": "4.17.21", "mocha": "9.2.2", "mocha-headless-chrome": "4.0.0", + "prettier": "^3.3.3", + "prettier-plugin-jinja-template": "^2.0.0", "sinon": "2.3.2" }, "engines": { diff --git a/yarn.lock b/yarn.lock index da660f1a7981..4e19b1144cbd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5620,6 +5620,11 @@ prelude-ls@^1.2.1: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +prettier@^3.3.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105" + integrity sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew== + proc-log@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/proc-log/-/proc-log-3.0.0.tgz#fb05ef83ccd64fd7b20bbe9c8c1070fc08338dd8" From d8d815d7f89107f1ecd6c727128e1e96d0aeabc0 Mon Sep 17 00:00:00 2001 From: nospame Date: Tue, 5 Nov 2024 13:10:37 -0800 Subject: [PATCH 2/7] Add Prettier plugin for jinja-style templates --- yarn.lock | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/yarn.lock b/yarn.lock index 4e19b1144cbd..e051a9774753 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5620,6 +5620,11 @@ prelude-ls@^1.2.1: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +prettier-plugin-jinja-template@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/prettier-plugin-jinja-template/-/prettier-plugin-jinja-template-2.0.0.tgz#23c7589be8b35d704480b5a7d17e7f84ad4df704" + integrity sha512-REZDAcZuOUvMDaPS47/GNRLKvbxh9DO9euXhWA7gJGqTLGzHPK2Z841F8I4bxsR7e2lqnHezkQ8GcWaKekKBVQ== + prettier@^3.3.3: version "3.3.3" resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.3.tgz#30c54fe0be0d8d12e6ae61dbb10109ea00d53105" From 1d03eafefe706935c0963b0745f852434d4e20de Mon Sep 17 00:00:00 2001 From: nospame Date: Tue, 5 Nov 2024 13:16:08 -0800 Subject: [PATCH 3/7] Add prettierrc with .html-specific config --- .prettierrc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000000..4d53d7f86c7e --- /dev/null +++ b/.prettierrc @@ -0,0 +1,14 @@ +{ + "plugins": ["prettier-plugin-jinja-template"], + "overrides": [ + { + "files": [ + "*.html" + ], + "options": { + "parser": "jinja-template" + } + } + ], + "tabWidth": 2 +} From 1e681878e72e2a13c19f485d84d9c512de81f979 Mon Sep 17 00:00:00 2001 From: nospame Date: Fri, 15 Nov 2024 09:02:28 -0800 Subject: [PATCH 4/7] Add prettify-staged.sh script --- scripts/prettify-staged.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100755 scripts/prettify-staged.sh diff --git a/scripts/prettify-staged.sh b/scripts/prettify-staged.sh new file mode 100755 index 000000000000..945bf6c88e7b --- /dev/null +++ b/scripts/prettify-staged.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -e + +prettify() { + FILES=$1 + echo "Formatting staged and uncommitted files:" + ./node_modules/.bin/prettier --write $FILES + echo + echo "Remember to stage these files before committing." +} + +# Get a list of staged files with appropriate extensions +STAGED_FILES=$(git diff --staged --name-only --diff-filter=d| grep -E "\.(html|css|scss|less)$") +if [ -z "$STAGED_FILES" ]; then + echo "No staged files to format." + exit 0 +fi + +prettify "$STAGED_FILES" From 12e9a3f99079fb17ab969896c86ba19913288548 Mon Sep 17 00:00:00 2001 From: nospame Date: Fri, 15 Nov 2024 09:49:40 -0800 Subject: [PATCH 5/7] Add Prettier check to pre-commit --- git-hooks/pre-commit.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/git-hooks/pre-commit.sh b/git-hooks/pre-commit.sh index d48e0b3ffc56..bd555cc0da36 100755 --- a/git-hooks/pre-commit.sh +++ b/git-hooks/pre-commit.sh @@ -54,7 +54,18 @@ if [ $? != 0 ]; then status=1; fi pyfiles=($(git diff --staged --name-only --diff-filter=d| grep "\.py$")) if [ -n "$pyfiles" ]; then flake8 --show-source --config=.flake8 "${pyfiles[@]}" - if [ $? != 0 ]; then status=1; fi + if [ $? != 0 ]; then status=1 && echo; fi +fi + +html_or_stylesheets=($(git diff --staged --name-only --diff-filter=d| grep -E "\.(html|css|scss|less)$")) +if [ -n "$html_or_stylesheets" ]; then + ./node_modules/.bin/prettier --list-different "${html_or_stylesheets[@]}" + if [ $? != 0 ]; then + status=1; + echo "HTML and/or stylesheets above have code style issues." + echo "Run ./scripts/prettify-staged.sh to fix them automatically." + echo + fi fi if [ $status != 0 ] From 57dd3698fa2c27e95e7d854b926d20646be89853 Mon Sep 17 00:00:00 2001 From: nospame Date: Mon, 2 Dec 2024 12:22:53 -0800 Subject: [PATCH 6/7] prettify-staged => prettify-changed Look for already committed files instead of uncommitted, and improve error handling --- git-hooks/pre-commit.sh | 2 +- scripts/prettify-changed.sh | 18 ++++++++++++++++++ scripts/prettify-staged.sh | 20 -------------------- 3 files changed, 19 insertions(+), 21 deletions(-) create mode 100755 scripts/prettify-changed.sh delete mode 100755 scripts/prettify-staged.sh diff --git a/git-hooks/pre-commit.sh b/git-hooks/pre-commit.sh index bd555cc0da36..327b0b04984d 100755 --- a/git-hooks/pre-commit.sh +++ b/git-hooks/pre-commit.sh @@ -63,7 +63,7 @@ if [ -n "$html_or_stylesheets" ]; then if [ $? != 0 ]; then status=1; echo "HTML and/or stylesheets above have code style issues." - echo "Run ./scripts/prettify-staged.sh to fix them automatically." + echo "Run ./scripts/prettify-changed.sh to fix them in a separate commit." echo fi fi diff --git a/scripts/prettify-changed.sh b/scripts/prettify-changed.sh new file mode 100755 index 000000000000..0c73ceec97ed --- /dev/null +++ b/scripts/prettify-changed.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +get_changed() { + # Get a list of changed and committed files with appropriate extensions + git diff master...HEAD --name-only --diff-filter=d | grep -E "\.(html|css|scss|less)$" +} + +prettify() { + FILES=$1 + echo "Formatting files changed on this branch:" + ./node_modules/.bin/prettier --write "$FILES" + echo +} + +CHANGED_FILES=$(get_changed) || { echo "No changed files found to format." && exit 1; } +prettify "$CHANGED_FILES" || { echo "Oops! Did you remember to run yarn install?" && exit 1; } + +echo "Remember to commit these changes." diff --git a/scripts/prettify-staged.sh b/scripts/prettify-staged.sh deleted file mode 100755 index 945bf6c88e7b..000000000000 --- a/scripts/prettify-staged.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -set -e - -prettify() { - FILES=$1 - echo "Formatting staged and uncommitted files:" - ./node_modules/.bin/prettier --write $FILES - echo - echo "Remember to stage these files before committing." -} - -# Get a list of staged files with appropriate extensions -STAGED_FILES=$(git diff --staged --name-only --diff-filter=d| grep -E "\.(html|css|scss|less)$") -if [ -z "$STAGED_FILES" ]; then - echo "No staged files to format." - exit 0 -fi - -prettify "$STAGED_FILES" From d3d73110763edf453731316fbef45f5e0debae8d Mon Sep 17 00:00:00 2001 From: Evan Mapson <100609770+nospame@users.noreply.github.com> Date: Mon, 9 Dec 2024 12:56:22 -0800 Subject: [PATCH 7/7] Clarify when to run prettify-changed Co-authored-by: Daniel Miller --- git-hooks/pre-commit.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-hooks/pre-commit.sh b/git-hooks/pre-commit.sh index 327b0b04984d..cf7ad33fcecf 100755 --- a/git-hooks/pre-commit.sh +++ b/git-hooks/pre-commit.sh @@ -63,7 +63,7 @@ if [ -n "$html_or_stylesheets" ]; then if [ $? != 0 ]; then status=1; echo "HTML and/or stylesheets above have code style issues." - echo "Run ./scripts/prettify-changed.sh to fix them in a separate commit." + echo "After committing changes with --no-verify, run ./scripts/prettify-changed.sh to fix them." echo fi fi