From c2bb52e5730f19944a60888ca90040a7476ae634 Mon Sep 17 00:00:00 2001 From: Liam Rella Date: Mon, 17 Feb 2025 23:03:30 +1030 Subject: [PATCH] feat: update prettier to latest config standard --- .prettierignore | 52 +------- packages/prettier-config/init/.prettierignore | 51 +------- .../init/.vscode/settings.json | 1 + .../prettier-config/init/prettier.config.js | 17 +-- packages/prettier-config/package.json | 104 ++++++++------- packages/prettier-config/prettier.config.js | 74 ----------- packages/prettier-config/src/cli.ts | 30 +---- packages/prettier-config/src/command.ts | 67 ++++++++++ packages/prettier-config/src/index.ts | 123 ++++++++++++++++++ packages/prettier-config/tsconfig.json | 10 ++ pnpm-lock.yaml | 36 +++-- prettier.config.js | 17 +-- 12 files changed, 295 insertions(+), 287 deletions(-) delete mode 100644 packages/prettier-config/prettier.config.js create mode 100644 packages/prettier-config/src/command.ts create mode 100644 packages/prettier-config/src/index.ts create mode 100644 packages/prettier-config/tsconfig.json diff --git a/.prettierignore b/.prettierignore index 6657601..d8dca02 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,58 +1,8 @@ # Prettier Ignore -# Does not inherit from .gitignore - -# @envsa/repo-config boilerplate -# ===== -# Craft -# ===== -.env -.env.* -!.env.example -vendor/ -/web/assets/* - -# ====== -# Builds -# ====== -node_modules/ -**/*.min.js - -# ===== -# Tests -# ===== -/test-results/ -/playwright-report/ -/playwright/.cache/ - -# ==== -# Misc -# ==== -.cache -.DS_Store -_junk/ -tmp/ -temp/ -.project -.settings -*.esproj -*.sublime-workspace -*.sublime-project -*.tmproj -*.tmproject -config.codekit3 -prepros-6.config +# Also respects .gitignore # @envsa/prettier-config boilerplate pnpm-lock.yaml package-lock.json -composer.lock -config/project/**/* -storage/config-deltas/**/* -storage/**/* -web/cpresources/**/* -web/web.config -*.svg -.ddev/ # Customizations -bin/ diff --git a/packages/prettier-config/init/.prettierignore b/packages/prettier-config/init/.prettierignore index 6583f99..d8dca02 100644 --- a/packages/prettier-config/init/.prettierignore +++ b/packages/prettier-config/init/.prettierignore @@ -1,57 +1,8 @@ # Prettier Ignore -# Does not inherit from .gitignore - -# @envsa/repo-config boilerplate -# ===== -# Craft -# ===== -.env -.env.* -!.env.example -vendor/ -/web/assets/* - -# ====== -# Builds -# ====== -node_modules/ -**/*.min.js - -# ===== -# Tests -# ===== -/test-results/ -/playwright-report/ -/playwright/.cache/ - -# ==== -# Misc -# ==== -.cache -.DS_Store -_junk/ -tmp/ -temp/ -.project -.settings -*.esproj -*.sublime-workspace -*.sublime-project -*.tmproj -*.tmproject -config.codekit3 -prepros-6.config +# Also respects .gitignore # @envsa/prettier-config boilerplate pnpm-lock.yaml package-lock.json -composer.lock -config/project/**/* -storage/config-deltas/**/* -storage/**/* -web/cpresources/**/* -web/web.config -*.svg -.ddev/ # Customizations diff --git a/packages/prettier-config/init/.vscode/settings.json b/packages/prettier-config/init/.vscode/settings.json index 0d0e5b8..a4d5828 100644 --- a/packages/prettier-config/init/.vscode/settings.json +++ b/packages/prettier-config/init/.vscode/settings.json @@ -11,6 +11,7 @@ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, "prettier.documentSelectors": [ + "**/.eslintignore", "**/.node-version", "**/.npmrc", "**/.prettierignore", diff --git a/packages/prettier-config/init/prettier.config.js b/packages/prettier-config/init/prettier.config.js index 0687905..bc9b5c2 100644 --- a/packages/prettier-config/init/prettier.config.js +++ b/packages/prettier-config/init/prettier.config.js @@ -1,16 +1,3 @@ -import sharedConfig from '@envsa/prettier-config'; +import { prettierConfig } from '@envsa/prettier-config'; -/** @type {import("prettier").Config} */ -const localConfig = { - // Config overrides - // overrides: [ - // ...sharedConfig.overrides, - // { - // // Per-file overrides overrides - // }, - // ], -}; -export default { - ...sharedConfig, - ...localConfig, -}; +export default prettierConfig(); diff --git a/packages/prettier-config/package.json b/packages/prettier-config/package.json index 5db1db4..db628ba 100644 --- a/packages/prettier-config/package.json +++ b/packages/prettier-config/package.json @@ -1,61 +1,71 @@ { - "name": "@envsa/prettier-config", - "version": "8.2.0", - "type": "module", - "description": "Prettier configuration for @envsa/shared-config.", - "repository": { - "type": "git", - "url": "git@github.com:envsa/shared-config.git", - "directory": "packages/prettier-config" - }, - "bugs": { - "url": "https://github.com/envsa/shared-config/issues", - "email": "DEW.ICTWebServices@sa.gov.au" - }, - "author": { - "name": "Liam Rella", - "email": "DEW.ICTWebServices@sa.gov.au", - "url": "https://github.com/rellafella" - }, - "license": "MIT", - "engines": { - "node": ">=22.0.0", - "pnpm": ">=10.0.0" - }, - "bin": { - "prettier-config": "bin/cli.js" - }, - "main": "prettier.config.js", - "files": [ - "bin/*", - "init/*" - ], - "keywords": [ - "shared-config", - "prettier-config", - "prettier", - "cli" - ], - "scripts": { - "build": "../../scripts/build.ts && mdat readme", - "cli": "node ./bin/cli.js", - "prepublishOnly": "pnpm run build" - }, - "peerDependencies": { - "prettier": "^3.3.1" - }, + "name": "@envsa/prettier-config", + "version": "8.2.0", + "description": "Prettier configuration for @envsa/shared-config.", + "keywords": [ + "shared-config", + "prettier-config", + "prettier", + "cli" + ], + "homepage": "https://github.com/envsa/shared-config/packages/prettier-config", + "bugs": { + "url": "https://github.com/envsa/shared-config/issues", + "email": "DEW.ICTWebServices@sa.gov.au" + }, + "repository": { + "type": "git", + "url": "git@github.com:envsa/shared-config.git", + "directory": "packages/prettier-config" + }, + "license": "MIT", + "author": { + "name": "Liam Rella", + "email": "DEW.ICTWebServices@sa.gov.au", + "url": "https://github.com/rellafella" + }, + "type": "module", + "exports": { + ".": { + "import": "./dist/index.js", + "types": "./dist/index.d.ts" + } + }, + "main": "dist/index.js", + "types": "dist/index.d.ts", + "bin": { + "envsa-prettier": "bin/cli.js" + }, + "files": [ + "bin/*", + "dist/*", + "init/*" + ], + "scripts": { + "build": "tsc && ../../scripts/build.ts", + "cli": "node ./bin/cli.js", + "prepublishOnly": "pnpm run build" + }, "dependencies": { "@pinojs/json-colorizer": "^4.0.0", "@prettier/plugin-php": "^0.22.2", "@prettier/plugin-xml": "^3.4.1", "@zackad/prettier-plugin-twig": "^0.13.0", "cosmiconfig": "^9.0.0", + "deepmerge-ts": "^7.1.4", "execa": "^9.5.2", + "find-workspaces": "^0.3.1", "fs-extra": "^11.2.0", - "prettier-plugin-pkg": "^0.18.1", + "prettier": "^3.3.1", + "prettier-plugin-packagejson": "^2.5.8", "prettier-plugin-sh": "^0.14.0", "prettier-plugin-svelte": "^3.3.2", - "prettier-plugin-tailwindcss": "^0.6.9" + "prettier-plugin-tailwindcss": "^0.6.9", + "sort-package-json": "^2.14.0" + }, + "engines": { + "node": ">=22.0.0", + "pnpm": ">=10.0.0" }, "publishConfig": { "access": "public" diff --git a/packages/prettier-config/prettier.config.js b/packages/prettier-config/prettier.config.js deleted file mode 100644 index e82dbc3..0000000 --- a/packages/prettier-config/prettier.config.js +++ /dev/null @@ -1,74 +0,0 @@ -/** @type {import("prettier").Config} */ -const config = { - arrowParens: 'always', - bracketSpacing: true, - overrides: [ - { - files: '*.json5', - options: { - parser: 'json5', - quoteProps: 'preserve', - singleQuote: false, - trailingComma: 'none', - }, - }, - { - files: '*.php', - options: { - phpVersion: '8.2', - printWidth: 80, - parser: 'php', - tabWidth: 4, - trailingCommaPHP: true, - }, - }, - { - files: '*.{yml,yaml}', - options: { - singleQuote: false, - tabWidth: 4, - }, - }, - { - files: ['*rc', '*ignore', '*.sh', '*.zsh', '*.bash', '*.fish'], - options: { - parser: 'sh', - }, - }, - { - files: '*.svelte', - options: { - parser: 'svelte', - }, - }, - { - files: '*.twig', - options: { - twigSingleQuote: false, - twigMultiTags: [ - 'apply,endapply', - 'cache,endcache', - 'ifchildren,endifchildren', - 'nav,endnav', - 'switch,case,default,endswitch', - ], - }, - }, - ], - plugins: [ - '@prettier/plugin-php', - '@prettier/plugin-xml', - '@zackad/prettier-plugin-twig', - 'prettier-plugin-pkg', - 'prettier-plugin-svelte', - 'prettier-plugin-tailwindcss', - ], - printWidth: 100, - semi: true, - singleQuote: true, - tabWidth: 2, - trailingComma: 'all', - useTabs: false, -}; - -export default config; diff --git a/packages/prettier-config/src/cli.ts b/packages/prettier-config/src/cli.ts index ee3384a..1dcd77a 100644 --- a/packages/prettier-config/src/cli.ts +++ b/packages/prettier-config/src/cli.ts @@ -1,29 +1,5 @@ #!/usr/bin/env node -import { buildCommands } from '$root/src/command-builder.ts'; +import { buildCommands } from '../../../src/command-builder.js' +import { commandDefinition } from './command.js' -// TODO bad idea? -// At least test the ruby situation -const sharedOptions = [ - '--log-level=warn', - '--plugin=@prettier/plugin-php', - '--plugin=@prettier/plugin-xml', - '--plugin=prettier-plugin-pkg', - '--plugin=prettier-plugin-sh', - '--plugin=prettier-plugin-svelte', - '--plugin=prettier-plugin-tailwindcss', -]; - -await buildCommands('prettier-config', '[Prettier]', 'blue', { - check: { - command: 'prettier', - defaultArguments: ['.'], - options: [...sharedOptions, '--check'], - }, - fix: { - command: 'prettier', - defaultArguments: ['.'], - options: [...sharedOptions, '--write'], - }, - init: {}, - printConfig: {}, -}); +await buildCommands(commandDefinition) diff --git a/packages/prettier-config/src/command.ts b/packages/prettier-config/src/command.ts new file mode 100644 index 0000000..cb52a1e --- /dev/null +++ b/packages/prettier-config/src/command.ts @@ -0,0 +1,67 @@ +import { + type CommandDefinition, + DESCRIPTION, + getCosmiconfigCommand, +} from '../../../src/command-builder.js' +import { getFilePathAtProjectRoot } from '../../../src/path-utils.js' + +// TODO bad idea? +const sharedOptions = [ + '--log-level=warn', + '--plugin=@prettier/plugin-php', + '--plugin=@prettier/plugin-xml', + '--plugin=prettier-plugin-packagejson', + '--plugin=prettier-plugin-sh', + '--plugin=prettier-plugin-svelte', + '--plugin=prettier-plugin-tailwindcss', + // Have to resolve to the project root for ignore to work when calling prettier in subdirectories + `--ignore-path=${getFilePathAtProjectRoot('.gitignore') ?? '.gitignore'}`, + `--ignore-path=${getFilePathAtProjectRoot('.prettierignore') ?? '.prettierignore'}`, +] + +export const commandDefinition: CommandDefinition = { + commands: { + fix: { + commands: [ + { + name: 'prettier', + optionFlags: [...sharedOptions, '--write'], + receivePositionalArguments: true, + }, + ], + description: `Format files according to your Prettier configuration. ${DESCRIPTION.fileRun}`, + positionalArgumentDefault: '.', + positionalArgumentMode: 'optional', + }, + init: { + configFile: 'prettier.config.js', + configPackageJson: { + prettier: '@envsa/prettier-config', + }, + locationOptionFlag: true, + }, + lint: { + commands: [ + { + name: 'prettier', + optionFlags: [...sharedOptions, '--check'], + receivePositionalArguments: true, + }, + ], + description: `Check that files are formatted according to your Prettier configuration. ${DESCRIPTION.fileRun}`, + positionalArgumentDefault: '.', + positionalArgumentMode: 'optional', + }, + printConfig: { + // See also --find-config-path and --file-info options for fancier per-file approaches... + commands: [getCosmiconfigCommand('prettier')], + description: `Print the effective Prettier configuration. ${DESCRIPTION.packageSearch}. ${DESCRIPTION.monorepoSearch}.`, + positionalArgumentMode: 'none', + }, + }, + description: "Envsa's Prettier shared configuration tools.", + logColor: 'blue', + logPrefix: '[Prettier]', + name: 'envsa-prettier', + order: 9, +} diff --git a/packages/prettier-config/src/index.ts b/packages/prettier-config/src/index.ts new file mode 100644 index 0000000..5b3670c --- /dev/null +++ b/packages/prettier-config/src/index.ts @@ -0,0 +1,123 @@ +import type { Config as PrettierConfig} from 'prettier'; +import { deepmerge} from 'deepmerge-ts'; +import {sortOrder as sortPackageJsonSortOrder} from 'sort-package-json'; + +/** + * Merge custom keys into the `sort-package-json` `order` array. Where + * duplicated, delete existing and prioritize new keys. + */ +function customizeSortOrder(keys: string[], newKeys: string[]): string[] { + // If new keys are in keys, remove them + const filteredKeys = keys.filter((key) => !newKeys.includes(key)); + + // Append new keys to the end + return [...filteredKeys, ...newKeys]; +} + +const sharedPrettierConfig: PrettierConfig = { + arrowParens: 'always', + bracketSpacing: true, + overrides: [ + { + files: '*.json5', + options: { + parser: 'json5', + quoteProps: 'preserve', + singleQuote: false, + trailingComma: 'none', + }, + }, + { + files: '*.php', + options: { + phpVersion: '8.2', + printWidth: 80, + parser: 'php', + tabWidth: 4, + trailingCommaPHP: true, + }, + }, + { + files: '*.{yml,yaml}', + options: { + singleQuote: false, + tabWidth: 4, + }, + }, + { + files: ['*rc', '*ignore', '*.sh', '*.zsh', '*.bash', '*.fish'], + options: { + parser: 'sh', + plugins: ["prettier-plugin-sh"] + }, + }, + { + files: '*.svelte', + options: { + parser: 'svelte', + plugins: ["prettier-plugin-svelte"] + }, + }, + { + files: '*.twig', + options: { + twigSingleQuote: false, + twigMultiTags: [ + 'apply,endapply', + 'cache,endcache', + 'ifchildren,endifchildren', + 'nav,endnav', + 'switch,case,default,endswitch', + ], + }, + }, + // Make this match eslint 'json-package/order-properties' + // https://github.com/matzkoh/prettier-plugin-packagejson/issues/188 + // This must stay in sync with packages/eslint-config/src/configs/json.ts + { + files: 'package.json', + options: { + packageSortOrder: customizeSortOrder(sortPackageJsonSortOrder, [ + 'cspell', + 'knip', + 'mdat', + 'prettier', + 'remarkConfig', + 'stylelint' + ]) + } + } + ], + plugins: [ + '@prettier/plugin-php', + '@prettier/plugin-xml', + 'prettier-plugin-packagejson', + 'prettier-plugin-sh', + '@zackad/prettier-plugin-twig', + 'prettier-plugin-tailwindcss', + ], + printWidth: 100, + semi: true, + singleQuote: true, + tabWidth: 2, + trailingComma: 'all', + useTabs: false, +} + + +/** + * **\@Kitschpatrol's Shared Prettier Configuration** + * @see [@envsa/prettier-config](https://github.com/envsa/shared-config/tree/main/packages/prettier-config) + * @see [@envsa/shared-config](https://github.com/envsa/shared-config) + * @example + * ```js + * export default prettierConfig({ + * printWidth: 120, + * }) + * ``` + */ +export function prettierConfig(config?: PrettierConfig): PrettierConfig { + return deepmerge(sharedPrettierConfig, config) +} + +export default sharedPrettierConfig; diff --git a/packages/prettier-config/tsconfig.json b/packages/prettier-config/tsconfig.json new file mode 100644 index 0000000..3bad3a7 --- /dev/null +++ b/packages/prettier-config/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "allowImportingTsExtensions": false, + "declaration": true, + "noEmit": false, + "outDir": "dist" + }, + "include": ["./src/index.ts"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 15b7e58..ef763b3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -330,18 +330,24 @@ importers: cosmiconfig: specifier: ^9.0.0 version: 9.0.0(typescript@5.7.3) + deepmerge-ts: + specifier: ^7.1.4 + version: 7.1.4 execa: specifier: ^9.5.2 version: 9.5.2 + find-workspaces: + specifier: ^0.3.1 + version: 0.3.1 fs-extra: specifier: ^11.2.0 version: 11.3.0 prettier: specifier: ^3.3.1 version: 3.5.1 - prettier-plugin-pkg: - specifier: ^0.18.1 - version: 0.18.1(prettier@3.5.1) + prettier-plugin-packagejson: + specifier: ^2.5.8 + version: 2.5.8(prettier@3.5.1) prettier-plugin-sh: specifier: ^0.14.0 version: 0.14.0(prettier@3.5.1) @@ -351,6 +357,9 @@ importers: prettier-plugin-tailwindcss: specifier: ^0.6.9 version: 0.6.11(@zackad/prettier-plugin-twig@0.13.0)(prettier-plugin-svelte@3.3.3(prettier@3.5.1)(svelte@5.20.1))(prettier@3.5.1) + sort-package-json: + specifier: ^2.14.0 + version: 2.14.0 packages/repo-config: dependencies: @@ -2146,6 +2155,10 @@ packages: deep-is@0.1.4: resolution: {integrity: sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==} + deepmerge-ts@7.1.4: + resolution: {integrity: sha512-fxqo6nHGQ9zOVgI4KXqtWXJR/yCLtC7aXIVq+6jc8tHPFUxlFmuUcm2kC4vztQ+LJxQ3gER/XAWearGYQ8niGA==} + engines: {node: '>=16.0.0'} + deepmerge@4.3.1: resolution: {integrity: sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==} engines: {node: '>=0.10.0'} @@ -3703,11 +3716,13 @@ packages: resolution: {integrity: sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==} engines: {node: '>=6.0.0'} - prettier-plugin-pkg@0.18.1: - resolution: {integrity: sha512-FuUxvsYZR/8rsLH8s/jbPQmgYvv0yxW8LoIHCy6+Q7p4FBjjdP3DNKx8fMTOsc0SlEB1skB4o1LcahRceIh87A==} - engines: {node: ^14.18.0 || >=16.0.0} + prettier-plugin-packagejson@2.5.8: + resolution: {integrity: sha512-BaGOF63I0IJZoudxpuQe17naV93BRtK8b3byWktkJReKEMX9CC4qdGUzThPDVO/AUhPzlqDiAXbp18U6X8wLKA==} peerDependencies: - prettier: ^3.0.3 + prettier: '>= 1.16.0' + peerDependenciesMeta: + prettier: + optional: true prettier-plugin-sh@0.14.0: resolution: {integrity: sha512-hfXulj5+zEl/ulrO5kMuuTPKmXvOg0bnLHY1hKFNN/N+/903iZbNp8NyZBTsgI8dtkSgFfAEIQq0IQTyP1ZVFQ==} @@ -6179,6 +6194,8 @@ snapshots: deep-is@0.1.4: {} + deepmerge-ts@7.1.4: {} + deepmerge@4.3.1: {} defu@6.1.4: {} @@ -8041,8 +8058,11 @@ snapshots: dependencies: fast-diff: 1.3.0 - prettier-plugin-pkg@0.18.1(prettier@3.5.1): + prettier-plugin-packagejson@2.5.8(prettier@3.5.1): dependencies: + sort-package-json: 2.14.0 + synckit: 0.9.2 + optionalDependencies: prettier: 3.5.1 prettier-plugin-sh@0.14.0(prettier@3.5.1): diff --git a/prettier.config.js b/prettier.config.js index 0687905..bc9b5c2 100644 --- a/prettier.config.js +++ b/prettier.config.js @@ -1,16 +1,3 @@ -import sharedConfig from '@envsa/prettier-config'; +import { prettierConfig } from '@envsa/prettier-config'; -/** @type {import("prettier").Config} */ -const localConfig = { - // Config overrides - // overrides: [ - // ...sharedConfig.overrides, - // { - // // Per-file overrides overrides - // }, - // ], -}; -export default { - ...sharedConfig, - ...localConfig, -}; +export default prettierConfig();