diff --git a/index.js b/index.js index 0df3af9..c37bd9c 100755 --- a/index.js +++ b/index.js @@ -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 diff --git a/tests/workspaces.js b/tests/workspaces.js index 054ada5..1aab2fb 100644 --- a/tests/workspaces.js +++ b/tests/workspaces.js @@ -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',