Repository: un-ts/prettier (ts)

Issue:
[prettier-plugin-pkg] add option to ignore sorting scripts
@JounQin would you consider such an option?

In the original repo nothing happens so far 🙁 and as their repo is written JS, it is hard for me to add stuff to their repo.

https://github.com/keithamus/sort-package-json/issues/359#issuecomment-2870064069

---

here we could just allow options and allow to ignore sort scripts:

https://github.com/un-ts/prettier/blob/802cb584cc145312cd7499154b83e638b54b83aa/packages/pkg/src/index.ts#L24

might be a lot easier

and then I could switch away from `prettier-plugin-packagejson` in all my projects, even company-work

Pull request description:
feat(pkg): add option packageIgnoreSort
closes #466 
<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Add `packageIgnoreSort` option to `prettier-plugin-pkg` to ignore sorting specified `package.json` properties.
> 
>   - **Feature**:
>     - Add `packageIgnoreSort` option in `index.ts` to specify properties in `package.json` to ignore during sorting.
>     - Update `process()` in `object.ts` to skip sorting for keys in `packageIgnoreSort`.
>   - **Types**:
>     - Add `packageIgnoreSort` to `FormatOptions` in `types.ts`.
>   - **Tests**:
>     - Add `ignore-sort.spec.ts` to test `packageIgnoreSort` functionality with a snapshot test.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=un-ts%2Fprettier&utm_source=github&utm_medium=referral)<sup> for fb938a5bb2c5e1059ff1ea4cc46d772ada8e8913. You can [customize](https://app.ellipsis.dev/un-ts/settings/summaries) this summary. It will automatically update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Added a new option to ignore sorting for specified properties in package.json files.
- **Tests**
  - Introduced tests to verify that ignored properties are not sorted when the new option is used.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Patch:
diff --git a/.changeset/full-frogs-join.md b/.changeset/full-frogs-join.md
new file mode 100644
index 0000000..47e41e7
--- /dev/null
+++ b/.changeset/full-frogs-join.md
@@ -0,0 +1,5 @@
+---
+"prettier-plugin-pkg": minor
+---
+
+feat(pkg): add option `packageIgnoreSort`
diff --git a/packages/pkg/src/index.ts b/packages/pkg/src/index.ts
index 2e1ea7e..2ea1c27 100644
--- a/packages/pkg/src/index.ts
+++ b/packages/pkg/src/index.ts
@@ -14,9 +14,9 @@ import { files } from './rules/files.js'
 import { object } from './rules/object.js'
 import { dependencyNames, sort } from './rules/sort.js'
 import type {
+  FormatOptions,
   ObjectExpression,
   ObjectProperty,
-  FormatOptions,
 } from './types.js'
 
 const PKG_REG = /[/\\]?package\.json$/
@@ -25,11 +25,15 @@ const {
   json: { parse },
 } = babelParser.parsers
 
+const DEFAULT_SORTS = ['engines', 'devEngines', 'scripts', ...dependencyNames]
+
 const format = (properties: ObjectProperty[], options: FormatOptions) => {
-  let props = ['engines', 'devEngines', 'scripts', ...dependencyNames].reduce(
-    (acc, item) => object(acc, item),
-    sort(properties, options),
-  )
+  const { packageIgnoreSort } = options
+  let props = (
+    packageIgnoreSort?.length
+      ? DEFAULT_SORTS.filter(item => !packageIgnoreSort.includes(item))
+      : DEFAULT_SORTS
+  ).reduce((acc, item) => object(acc, item), sort(properties, options))
   props = files(props)
   return props
 }
@@ -62,5 +66,14 @@ export default {
       description:
         'An array of property names to sort the package.json properties by.',
     },
+    packageIgnoreSort: {
+      since: '0.21.0',
+      category: 'Package',
+      type: 'string',
+      array: true,
+      default: [{ value: [] }],
+      description:
+        'An array of property names to ignore when sorting the package.json properties.',
+    },
   },
 } as Plugin
diff --git a/packages/pkg/src/types.ts b/packages/pkg/src/types.ts
index 51605c3..a277042 100644
--- a/packages/pkg/src/types.ts
+++ b/packages/pkg/src/types.ts
@@ -46,4 +46,5 @@ export type { StringLiteral } from '@babel/types'
 
 export interface FormatOptions {
   packageSortOrder?: string[]
+  packageIgnoreSort?: string[]
 }


Test patch:
diff --git a/packages/pkg/test/__snapshots__/ignore-sort.spec.ts.snap b/packages/pkg/test/__snapshots__/ignore-sort.spec.ts.snap
new file mode 100644
index 0000000..905913a
--- /dev/null
+++ b/packages/pkg/test/__snapshots__/ignore-sort.spec.ts.snap
@@ -0,0 +1,53 @@
+// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
+
+exports[`ignore-sort 1`] = `
+"{
+  "name": "prettier-plugin-pkg",
+  "version": "0.0.0",
+  "description": "An opinionated package.json formatter plugin for Prettier",
+  "repository": "git@github.com/un-ts/prettier.git",
+  "homepage": "https://github.com/un-ts/prettier/tree/master/packages/pkg",
+  "author": "JounQin <admin@1stg.me> (https://www.1stG.me)",
+  "license": "MPL-2.0",
+  "engines": {
+    "node": ">= 8.0.0"
+  },
+  "main": "lib/cjs",
+  "module": "lib/esm",
+  "es2015": "lib/es2015",
+  "files": [
+    "lib",
+    "rules"
+  ],
+  "keywords": [
+    "package",
+    "package.json",
+    "pkg",
+    "plugin",
+    "prettier",
+    "prettier-plugin"
+  ],
+  "peerDependencies": {
+    "prettier": "^1.18.2"
+  },
+  "resolutions": {
+    "imagemin-gifsicle": "^6.0.1",
+    "@babel/core": "^7.9.0",
+    "typescript": "^3.8.3",
+    "prettier": "^2.0.4",
+    "@babel/preset-env": "^7.12.1",
+    "eslint-plugin-prettier": "^3.1.2"
+  },
+  "commitlint": {
+    "extends": [
+      "@1stg"
+    ]
+  },
+  "eslintIgnore": [
+    "CHANGELOG.md",
+    "lib",
+    "!.*.js"
+  ]
+}
+"
+`;
diff --git a/packages/pkg/test/ignore-sort.spec.ts b/packages/pkg/test/ignore-sort.spec.ts
new file mode 100644
index 0000000..8b5184a
--- /dev/null
+++ b/packages/pkg/test/ignore-sort.spec.ts
@@ -0,0 +1,17 @@
+import { format } from 'prettier'
+
+import pkg1 from './fixtures/fixture1.json'
+
+import PkgPlugin from 'prettier-plugin-pkg'
+
+test('ignore-sort', async () => {
+  const input = JSON.stringify(pkg1, null, 2)
+  const output = await format(input, {
+    filepath: 'package.json',
+    parser: 'json-stringify',
+    plugins: [PkgPlugin],
+    packageIgnoreSort: ['resolutions'],
+  })
+
+  expect(output).toMatchSnapshot()
+})