Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,40 +103,42 @@ const sortObjectByIdent = (a, b) => {
// https://github.com/npm/package-json/blob/b6465f44c727d6513db6898c7cbe41dd355cebe8/lib/update-dependencies.js#L8-L21
const sortDependenciesLikeNpm = sortObjectBy((a, b) => a.localeCompare(b, 'en'))

/**
* "workspaces" can be an array (npm or yarn classic) or an object (pnpm/bun).
* In the case of an array, we do not want to alphabetically sort it in case
* scripts need to run in a specific order.
*
* @see https://docs.npmjs.com/cli/v7/using-npm/workspaces?v=true#running-commands-in-the-context-of-workspaces
*/
const sortWorkspaces = (workspaces) => {
// workspaces can be an array (yarn classic) or an object (pnpm/bun)
if (Array.isArray(workspaces)) {
return uniqAndSortArray(workspaces)
if (!isPlainObject(workspaces)) {
return workspaces
}

if (isPlainObject(workspaces)) {
// Sort known properties in a specific order
const sortedWorkspaces = {}
// Sort known properties in a specific order
const sortedWorkspaces = {}

// First add packages if it exists
if (workspaces.packages) {
sortedWorkspaces.packages = uniqAndSortArray(workspaces.packages)
}

// Then add catalog if it exists and sort it like dependencies
if (workspaces.catalog) {
sortedWorkspaces.catalog = sortDependenciesLikeNpm(workspaces.catalog)
}
// First add packages if it exists
if (workspaces.packages) {
sortedWorkspaces.packages = uniqAndSortArray(workspaces.packages)
}

// Add any other properties in alphabetical order
const knownKeys = ['packages', 'catalog']
const otherKeys = Object.keys(workspaces)
.filter((key) => !knownKeys.includes(key))
.sort()
// Then add catalog if it exists and sort it like dependencies
if (workspaces.catalog) {
sortedWorkspaces.catalog = sortDependenciesLikeNpm(workspaces.catalog)
}

for (const key of otherKeys) {
sortedWorkspaces[key] = workspaces[key]
}
// Add any other properties in alphabetical order
const knownKeys = ['packages', 'catalog']
const otherKeys = Object.keys(workspaces)
.filter((key) => !knownKeys.includes(key))
.sort()

return sortedWorkspaces
for (const key of otherKeys) {
sortedWorkspaces[key] = workspaces[key]
}

return workspaces
return sortedWorkspaces
}

// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js
Expand Down
9 changes: 0 additions & 9 deletions tests/workspaces.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import test from 'ava'
import sortPackageJson from '../index.js'
import { sortPackageJsonAsObject } from './_helpers.js'

test('workspaces array (yarn) should be sorted and unique', (t) => {
const packageJson = {
workspaces: ['packages/c', 'packages/a', 'packages/b', 'packages/a'],
}
const sorted = sortPackageJson(packageJson)
t.deepEqual(sorted.workspaces, ['packages/a', 'packages/b', 'packages/c'])
})

test('workspaces object should be sorted', (t) => {
const sortedWorkspaces = sortPackageJsonAsObject({
path: 'workspaces',
Expand Down