From 6e1eb36f3bcd8db4700b314a126993f024e1eafe Mon Sep 17 00:00:00 2001 From: Frank Harrison Date: Mon, 11 Nov 2024 06:45:24 +0000 Subject: [PATCH] chore(format): adds prettier and commands to run it ... including ci/cd check-only commands. The code is quite complex and missing a lot of dev-ops types checks and standards. One of the key problems with codebases like this is on-boarding new developers to the codebase (like myself). When there is a consistent and enforced coding-style (irrespective of what the coding style is) it makes it _significantly_ easier for collaborators and maintainers to get on with the job in hand. It also, from a day-2-day developer perspective, significantly reduces cognitive overhead re reading code. Finally this is a "trial balloon" PR, if this patch is accepted I will likely do more work on testing and docs for the project. - The new prettier config is a non-standard setup, but a close-match to how the code _currently_ looks. - 120 col-width print width (instead of the better and more information-dense 88), this is so the diff after applying prettier to the code is less disruptive, whilst still showing the benefits of using a prettier. - We use `tabWidth` setting of 4 as the code uses that more common setting instead of the more compact 2 spaces - note that 2 often leads to more readable and compact code. - We enforce trailing commas, as that seems to be the norm in this code-base. We choose the `es5` standard here. - We enforce tailing semi-colons (`semi`) as the majority of code used that flavour of `js`/`ts`. - For now we only run on code and not json files. This is designed such that `npm run pretty` re-formats the code for development, and when integrated with ci/cd, `prettyCheck` will return non-zero exit codes when formatting doesn't match the coding standards. --- .prettierignore | 1 + .prettierrc | 6 ++++++ package-lock.json | 23 +++++++++++++++++++++++ package.json | 4 ++++ 4 files changed, 34 insertions(+) create mode 100644 .prettierignore create mode 100644 .prettierrc diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..76a7a1c --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +pouchdb-browser.js diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..47c72d4 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 4, + "printWidth": 120, + "semi": true +} diff --git a/package-lock.json b/package-lock.json index c985fc1..84f1628 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,6 +58,7 @@ "pouchdb-merge": "^9.0.0", "pouchdb-replication": "^9.0.0", "pouchdb-utils": "^9.0.0", + "prettier": "^3.3.3", "svelte": "^4.2.19", "svelte-preprocess": "^6.0.2", "terser": "^5.31.6", @@ -5690,6 +5691,22 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", + "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", @@ -10787,6 +10804,12 @@ "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==", "dev": true }, + "prettier": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", + "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", + "dev": true + }, "psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", diff --git a/package.json b/package.json index 42ce09c..a96dacb 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,9 @@ "lint": "eslint src", "svelte-check": "svelte-check --tsconfig ./tsconfig.json", "tsc-check": "tsc --noEmit", + "pretty": "npm run prettyNoWrite -- --write --log-level error", + "prettyCheck": "npm run prettyNoWrite -- --check", + "prettyNoWrite": "prettier --config ./.prettierrc \"**/*.js\" \"**/*.ts\"", "check": "npm run lint && npm run svelte-check && npm run tsc-check" }, "keywords": [], @@ -51,6 +54,7 @@ "pouchdb-merge": "^9.0.0", "pouchdb-replication": "^9.0.0", "pouchdb-utils": "^9.0.0", + "prettier": "^3.3.3", "svelte": "^4.2.19", "svelte-preprocess": "^6.0.2", "terser": "^5.31.6",