From 017733ee4900777ebecd3979a6539b3148a64c7b Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Wed, 10 Jan 2024 17:24:51 -0600 Subject: [PATCH] Add prettier formatting of JavaScript, style, and HTML files, and check formatting in a workflow. Prettier is used to format JavaScript, style, and HTML files in htdocs. The configuration chosen is in .prettierrc and .editorconfig. Prettier doesn't really have many options as it is very opinionated. I am not entirely happy with everything prettier does, but it does give a more uniform code format to files. Furthermore, it is nice to be able to be able to just run prettier and not need to worry about formatting code. A workflow now checks formatting of JavaScript, style, and HTML files with prettier in addition checking formatting of perl files with perltidy. Note the "linter" workflow has been renamed to "check-formats", because that is what it does, and this really does not have anything to do with linting. Note that developers can (and should) run `npm run prettier-format` in the htdocs directory to format files. You can also run `npm run prettier-check` to check that formatting of these files is correct, and not actually change the files. This is what the workflow does. All `.dist.yml` files are also formatted and checked with prettier. --- .editorconfig | 17 +++++++++++ .github/workflows/check-formats.yml | 44 +++++++++++++++++++++++++++++ .github/workflows/linter.yml | 32 --------------------- .prettierrc | 8 ++++++ htdocs/package-lock.json | 22 +++++++++++++++ htdocs/package.json | 5 +++- 6 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/check-formats.yml delete mode 100644 .github/workflows/linter.yml create mode 100644 .prettierrc diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..ee82116646 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +max_line_length = 120 +trim_trailing_whitespace = true +indent_style = tab +indent_size = 4 + +[*.yml] +indent_style = space +indent_size = 2 + +[*.pg] +trim_trailing_whitespace = false diff --git a/.github/workflows/check-formats.yml b/.github/workflows/check-formats.yml new file mode 100644 index 0000000000..ec47d6c741 --- /dev/null +++ b/.github/workflows/check-formats.yml @@ -0,0 +1,44 @@ +--- +name: Check Formatting of Code Base + +defaults: + run: + shell: bash + +on: + push: + branches-ignore: [main, develop] + pull_request: + +jobs: + perltidy: + name: Check Perl file formatting with perltidy + runs-on: ubuntu-22.04 + container: + image: perl:5.34 + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Install dependencies + run: cpanm -n Perl::Tidy@20220613 + - name: Run perltidy + shell: bash + run: | + git config --global --add safe.directory "$GITHUB_WORKSPACE" + shopt -s extglob globstar nullglob + perltidy --pro=./.perltidyrc -b -bext='/' ./**/*.p[lm] ./**/*.t && git diff --exit-code + + prettier: + name: Check JavaScript, style, and HTML file formatting with prettier + runs-on: ubuntu-22.04 + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Install Node + uses: actions/setup-node@v4 + with: + node-version: '20' + - name: Install Dependencies + run: cd htdocs && npm ci --ignore-scripts + - name: Check formatting with prettier + run: cd htdocs && npm run prettier-check diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml deleted file mode 100644 index d64ec1b02a..0000000000 --- a/.github/workflows/linter.yml +++ /dev/null @@ -1,32 +0,0 @@ ---- -name: Lint Code Base - -defaults: - run: - shell: bash - -on: - push: - branches-ignore: [main, develop] - pull_request: - -jobs: - perltidy: - name: Run perltidy on Perl Files - runs-on: ubuntu-22.04 - container: - image: perl:5.34 - steps: - - uses: actions/checkout@v4 - - name: perl -V - run: perl -V - - name: Install dependencies - run: cpanm -n Perl::Tidy@20220613 - - name: perltidy --version - run: perltidy --version - - name: Run perltidy - shell: bash - run: | - git config --global --add safe.directory "$GITHUB_WORKSPACE" - shopt -s extglob globstar nullglob - perltidy --pro=./.perltidyrc -b -bext='/' ./**/*.p[lm] ./**/*.t && git diff --exit-code diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000000..b21dab0657 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "arrowParens": "always", + "bracketSpacing": true, + "printWidth": 120, + "semi": true, + "singleQuote": true, + "trailingComma": "none" +} diff --git a/htdocs/package-lock.json b/htdocs/package-lock.json index 28ebb0033b..9caff05ca4 100644 --- a/htdocs/package-lock.json +++ b/htdocs/package-lock.json @@ -24,6 +24,7 @@ "chokidar": "^3.5.3", "cssnano": "^6.0.1", "postcss": "^8.4.31", + "prettier": "^3.1.1", "rtlcss": "^4.1.1", "sass": "^1.69.5", "terser": "^5.24.0", @@ -1370,6 +1371,21 @@ "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", "dev": true }, + "node_modules/prettier": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.1.1.tgz", + "integrity": "sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", @@ -2544,6 +2560,12 @@ "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", "dev": true }, + "prettier": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.1.1.tgz", + "integrity": "sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw==", + "dev": true + }, "readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", diff --git a/htdocs/package.json b/htdocs/package.json index 13ef2aabea..50d0fb21e5 100644 --- a/htdocs/package.json +++ b/htdocs/package.json @@ -4,7 +4,9 @@ "license": "GPL-2.0+", "scripts": { "generate-assets": "node generate-assets", - "prepare": "npm run generate-assets" + "prepare": "npm run generate-assets", + "prettier-format": "prettier --ignore-path=../.gitignore --write \"**/*.{js,css,scss,html}\" \"../**/*.dist.yml\"", + "prettier-check": "prettier --ignore-path=../.gitignore --check \"**/*.{js,css,scss,html}\" \"../**/*.dist.yml\"" }, "repository": { "type": "git", @@ -28,6 +30,7 @@ "chokidar": "^3.5.3", "cssnano": "^6.0.1", "postcss": "^8.4.31", + "prettier": "^3.1.1", "rtlcss": "^4.1.1", "sass": "^1.69.5", "terser": "^5.24.0",