Skip to content

Commit

Permalink
feat(prettier-plugins): switch to sort-package-json (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown committed Apr 10, 2024
1 parent 7a17932 commit b84fa58
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 187 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-eels-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bfra.me/prettier-plugins": minor
---

Switch to [`sort-package-json`](https://github.com/keithamus/sort-package-json) in the `package-json` plugin
8 changes: 8 additions & 0 deletions .changeset/tall-carrots-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@bfra.me/prettier-plugins": patch
"@bfra.me/semantic-release": patch
"@bfra.me/prettier-config": patch
"@bfra.me/tsconfig": patch
---

Format `package.json` with latest `@bfra.me/prettier-plugins`
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"private": true,
"name": "@bfra.me/works",
"version": "0.0.0",
"private": true,
"description": "bfra.me tools and components",
"keywords": [
"Build Frameworks",
"CLI",
"Components",
"Tools",
"bfra.me"
],
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"type": "module",
Expand All @@ -19,6 +26,14 @@
"version-changesets": "changeset version",
"watch": "pnpm run build --watch"
},
"prettier": "@bfra.me/prettier-config",
"eslintConfig": {
"ignorePatterns": [
"lib",
"dist",
"node_modules"
]
},
"dependencies": {
"@bfra.me/prettier-config": "workspace:*",
"@bfra.me/tsconfig": "workspace:*",
Expand All @@ -32,25 +47,10 @@
"rimraf": "5.0.5",
"typescript": "5.4.4"
},
"keywords": [
"Build Frameworks",
"CLI",
"Components",
"Tools",
"bfra.me"
],
"eslintConfig": {
"ignorePatterns": [
"lib",
"dist",
"node_modules"
]
},
"packageManager": "pnpm@8.15.6",
"pnpm": {
"overrides": {
"read-pkg-up@^11": "npm:read-package-up"
}
},
"prettier": "@bfra.me/prettier-config"
}
}
34 changes: 17 additions & 17 deletions packages/prettier-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
"name": "@bfra.me/prettier-config",
"version": "0.3.1",
"description": "Shared Prettier configuration for bfra.me",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"keywords": [
"bfra.me",
"config",
"prettier",
"prettier-config",
"shared",
"works"
],
"homepage": "https://github.com/bfra-me/works/tree/main/packages/prettier-config#readme",
"bugs": "https://github.com/bfra-me/works/issues",
"repository": {
"directory": "packages/prettier-config",
"type": "git",
"url": "https://github.com/bfra-me/works.git"
"url": "https://github.com/bfra-me/works.git",
"directory": "packages/prettier-config"
},
"bugs": "https://github.com/bfra-me/works/issues",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"type": "module",
"exports": {
".": {
Expand All @@ -20,6 +28,7 @@
"./package.json": "./package.json"
},
"main": "prettier.config.js",
"types": "prettier.config.d.ts",
"files": [
"prettier.config.*",
"!*.map"
Expand All @@ -28,26 +37,17 @@
"build": "tsc --build && pnpm run format",
"format": "prettier --log-level error --ignore-unknown --write prettier.config.*"
},
"types": "prettier.config.d.ts",
"dependencies": {
"@bfra.me/prettier-plugins": "workspace:*"
},
"peerDependencies": {
"prettier": "^3.0.0"
},
"devDependencies": {
"@bfra.me/prettier-config": "workspace:*",
"@bfra.me/tsconfig": "workspace:*",
"prettier": "3.2.5"
},
"keywords": [
"bfra.me",
"config",
"prettier",
"prettier-config",
"shared",
"works"
],
"peerDependencies": {
"prettier": "^3.0.0"
},
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
38 changes: 19 additions & 19 deletions packages/prettier-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@
"name": "@bfra.me/prettier-plugins",
"version": "0.1.1",
"description": "Plugins for Prettier used across bfra.me.",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"keywords": [
"bfra.me",
"plugins",
"prettier",
"prettier-plugins",
"shared",
"works"
],
"homepage": "https://github.com/bfra-me/works/tree/main/packages/prettier-plugins#readme",
"bugs": "https://github.com/bfra-me/works/issues",
"repository": {
"directory": "packages/prettier-plugins",
"type": "git",
"url": "https://github.com/bfra-me/works.git"
"url": "https://github.com/bfra-me/works.git",
"directory": "packages/prettier-plugins"
},
"bugs": "https://github.com/bfra-me/works/issues",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"sideEffects": false,
"type": "module",
"exports": {
"./*": {
Expand All @@ -19,35 +28,26 @@
},
"./package.json": "./package.json"
},
"types": "lib/index.d.ts",
"files": [
"lib",
"!**/*.map"
],
"scripts": {
"build": "tsup"
},
"sideEffects": false,
"types": "lib/index.d.ts",
"dependencies": {
"prettier-package-json": "2.8.0"
},
"peerDependencies": {
"prettier": "^3.0.0"
"sort-package-json": "2.10.0"
},
"devDependencies": {
"@bfra.me/prettier-plugins": "workspace:*",
"@bfra.me/tsconfig": "workspace:*",
"prettier": "3.2.5",
"tsup": "8.0.2"
},
"keywords": [
"bfra.me",
"plugins",
"prettier",
"prettier-plugins",
"shared",
"works"
],
"peerDependencies": {
"prettier": "^3.0.0"
},
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
62 changes: 45 additions & 17 deletions packages/prettier-plugins/src/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
import type {Parser, ParserOptions} from 'prettier'
import type {Parser, SupportOption} from 'prettier'
import prettier from 'prettier'
import {parsers as babelParsers} from 'prettier/plugins/babel'
import {format, type Options as PrettierPackageJsonOptions} from 'prettier-package-json'
import {sortPackageJson} from 'sort-package-json'

const jsonStringifyParser = babelParsers['json-stringify']
type SortPackageJsonOptions = NonNullable<Parameters<typeof sortPackageJson>[1]>

export const parsers = {
interface PluginOptions {
/** Custom ordering array or comparator function. */
sortPackageJsonSortOrder: SortPackageJsonOptions['sortOrder']
}

declare module 'prettier' {
interface RequiredOptions extends PluginOptions {}
}

export const options: Record<keyof PluginOptions, SupportOption> = {
sortPackageJsonSortOrder: {
category: 'Format',
type: 'string',
description: 'Custom ordering array.',
default: [{value: []}],
array: true,
},
}

const parser = babelParsers['json-stringify']

export const parsers: Record<string, Parser> = {
'json-stringify': {
...jsonStringifyParser,
...parser,

preprocess(text: string, options: ParserOptions) {
if (jsonStringifyParser.preprocess) {
text = jsonStringifyParser.preprocess(text, options)
}
if (/package.*json$/u.test(options.filepath)) {
text = format(JSON.parse(text), {
tabWidth: options.tabWidth,
useTabs: options.useTabs === true,
...((options['prettier-package-json'] ?? {}) as Partial<PrettierPackageJsonOptions>),
})
async parse(text, options) {
const {filepath} = options
if (/package.*json$/u.test(filepath)) {
// Format the text with prettier to avoid any parsing errors
text = await prettier.format(text, {filepath})

if (parser.preprocess) {
text = parser.preprocess(text, options)
}

const sortOrder = options?.sortPackageJsonSortOrder
text = sortPackageJson(
text,
(sortOrder && sortOrder.length > 0 ? {sortOrder} : {}) as SortPackageJsonOptions,
)
}
return text

return parser.parse(text, options)
},
},
} satisfies Record<string, Parser>
}
32 changes: 16 additions & 16 deletions packages/semantic-release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
"name": "@bfra.me/semantic-release",
"version": "0.1.2",
"description": "Semantic Release shareable configuration and plugins for bfra.me.",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"keywords": [
"bfra.me",
"config",
"semantic-release",
"shared",
"typescript"
],
"homepage": "https://github.com/bfra-me/works/tree/main/packages/semantic-release#readme",
"bugs": "https://github.com/bfra-me/works/issues",
"repository": {
"directory": "packages/semantic-release",
"type": "git",
"url": "https://github.com/bfra-me/works.git"
"url": "https://github.com/bfra-me/works.git",
"directory": "packages/semantic-release"
},
"bugs": "https://github.com/bfra-me/works/issues",
"license": "MIT",
"author": "Marcus R. Brown <contact@bfra.me>",
"type": "module",
"exports": {
".": {
Expand All @@ -22,6 +29,7 @@
"./package.json": "./package.json"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"tsconfig.json",
Expand All @@ -31,10 +39,6 @@
"build": "tsup-node",
"test": "pnpm build && vitest --typecheck"
},
"types": "lib/index.d.ts",
"peerDependencies": {
"semantic-release": ">=23"
},
"devDependencies": {
"@bfra.me/semantic-release": "workspace:*",
"@bfra.me/tsconfig": "workspace:*",
Expand All @@ -44,13 +48,9 @@
"type-fest": "4.15.0",
"vitest": "1.4.0"
},
"keywords": [
"bfra.me",
"config",
"semantic-release",
"shared",
"typescript"
],
"peerDependencies": {
"semantic-release": ">=23"
},
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
Loading

0 comments on commit b84fa58

Please sign in to comment.