@@ -103,40 +103,42 @@ const sortObjectByIdent = (a, b) => {
103103// https://github.com/npm/package-json/blob/b6465f44c727d6513db6898c7cbe41dd355cebe8/lib/update-dependencies.js#L8-L21
104104const sortDependenciesLikeNpm = sortObjectBy ( ( a , b ) => a . localeCompare ( b , 'en' ) )
105105
106+ /**
107+ * "workspaces" can be an array (npm or yarn classic) or an object (pnpm/bun).
108+ * In the case of an array, we do not want to alphabetically sort it in case
109+ * scripts need to run in a specific order.
110+ *
111+ * @see https://docs.npmjs.com/cli/v7/using-npm/workspaces?v=true#running-commands-in-the-context-of-workspaces
112+ */
106113const sortWorkspaces = ( workspaces ) => {
107- // workspaces can be an array (yarn classic) or an object (pnpm/bun)
108- if ( Array . isArray ( workspaces ) ) {
109- return uniqAndSortArray ( workspaces )
114+ if ( ! isPlainObject ( workspaces ) ) {
115+ return workspaces
110116 }
111117
112- if ( isPlainObject ( workspaces ) ) {
113- // Sort known properties in a specific order
114- const sortedWorkspaces = { }
118+ // Sort known properties in a specific order
119+ const sortedWorkspaces = { }
115120
116- // First add packages if it exists
117- if ( workspaces . packages ) {
118- sortedWorkspaces . packages = uniqAndSortArray ( workspaces . packages )
119- }
120-
121- // Then add catalog if it exists and sort it like dependencies
122- if ( workspaces . catalog ) {
123- sortedWorkspaces . catalog = sortDependenciesLikeNpm ( workspaces . catalog )
124- }
121+ // First add packages if it exists
122+ if ( workspaces . packages ) {
123+ sortedWorkspaces . packages = uniqAndSortArray ( workspaces . packages )
124+ }
125125
126- // Add any other properties in alphabetical order
127- const knownKeys = [ 'packages' , 'catalog' ]
128- const otherKeys = Object . keys ( workspaces )
129- . filter ( ( key ) => ! knownKeys . includes ( key ) )
130- . sort ( )
126+ // Then add catalog if it exists and sort it like dependencies
127+ if ( workspaces . catalog ) {
128+ sortedWorkspaces . catalog = sortDependenciesLikeNpm ( workspaces . catalog )
129+ }
131130
132- for ( const key of otherKeys ) {
133- sortedWorkspaces [ key ] = workspaces [ key ]
134- }
131+ // Add any other properties in alphabetical order
132+ const knownKeys = [ 'packages' , 'catalog' ]
133+ const otherKeys = Object . keys ( workspaces )
134+ . filter ( ( key ) => ! knownKeys . includes ( key ) )
135+ . sort ( )
135136
136- return sortedWorkspaces
137+ for ( const key of otherKeys ) {
138+ sortedWorkspaces [ key ] = workspaces [ key ]
137139 }
138140
139- return workspaces
141+ return sortedWorkspaces
140142}
141143
142144// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js
0 commit comments