keithamus / sort-package-json

Sort an Object or package.json based on the well-known package.json keys
MIT License
790 stars 83 forks source link

fix: support for sorting dependenciesMeta in Yarn berry #323

Closed rickh18 closed 3 weeks ago

rickh18 commented 3 weeks ago

A PR for fixing #301

This implementation follows the sorting of yarn.

In Yarn berry, the dependenciesMeta field can also contain a range ("package-name@1.2.3").

The Yarn source code only uses the identifier when sorting dependencyMeta. This is different from the current implementation of this package. Each time when running Yarn, this will result in a conflict. This fix will bring the sorting in line with that of Yarn.

The current implementation sorts on the whole name including the range. Therefore, in the example below, "bats-assert@2.0.0" comes before "bats@1.10.0" (because - comes before @)

In this implementation, the range will be stripped from the name before sorting. Therefore, it will sort like yarn: "bats" comes before "bats-assert"

current sort-package-json implementation:

"dependenciesMeta": {
    "bats-assert@2.0.0": {
      "unplugged": true
    },
    "bats-support@0.3.0": {
      "unplugged": true
    },
    "bats@1.10.0": {
      "unplugged": true
    }
  },

yarn:

  "dependenciesMeta": {
    "bats@1.10.0": {
      "unplugged": true
    },
    "bats-assert@2.0.0": {
      "unplugged": true
    },
    "bats-support@0.3.0": {
      "unplugged": true
    }

The sorting stays the same for dependenciesMeta that do not have a range in their name.

I also checked if peerDependenciesMeta should have the same fix, however a range is not supported there in Yarn Berry.

github-actions[bot] commented 3 weeks ago

:tada: This PR is included in version 2.10.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: