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
10 changes: 7 additions & 3 deletions examples/fixtures/basic/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ packages:
- packages/*

catalog:
vue: ^3.4.0
react: ^18.2.0
"@antfu/eslint-config": ^6.7.3
lodash: ^4.17.21
dayjs: ^1.11.10
axios: ^1.6.0
express: ^4.18.0
typescript: ^5.3.0
eslint: ^8.56.0

catalogs:
dev:
axios: ^1.6.0
react: ^18.2.0
vue: ^3.4.0
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,12 @@ export const isCancelProcess = (value: unknown, message: string) => {
return process.exit(0)
}
}

export const packageSort = (packages: Record<string, string>) =>
Object.fromEntries(
Object.entries(packages).sort(([a], [b]) =>
a.startsWith('@') === b.startsWith('@')
? a.localeCompare(b)
: a.startsWith('@') ? -1 : 1,
),
)
8 changes: 4 additions & 4 deletions src/work.space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { findUp } from 'find-up'
import pc from 'picocolors'
import { parse, stringify } from 'yaml'
import { CANCEL_PROCESS, DEFAULT_CATALOGS } from '@/constant.ts'
import { formatDependencyUsage, isCancelProcess } from '@/utils.ts'
import { formatDependencyUsage, isCancelProcess, packageSort } from '@/utils.ts'

export const getWorkSpaceYaml = async (config: IConfig): Promise<IWorkSpace> => {
const workSpaceYamlPath = await findUp('pnpm-workspace.yaml', {
Expand Down Expand Up @@ -46,14 +46,14 @@ export const updateCatalogsWithContext = (options: CatalogsContextType) => {

// Check if the node exists; if it does, merge; if it doesn't, create
if (context.catalogs[catalogsName]) {
context.catalogs[catalogsName] = {
context.catalogs[catalogsName] = packageSort({
...context.catalogs[catalogsName],
...dependencies,
}
})
// console.log(`✅ ${choice.length} packages have been merged into the catalogs.${catalogsName} node`)
}
else {
context.catalogs[catalogsName] = dependencies
context.catalogs[catalogsName] = packageSort(dependencies)
// console.log(`✅ ${choice.length} packages have been added to the catalogs.${catalogsName} node`)
}
return context
Expand Down
41 changes: 41 additions & 0 deletions tests/package.sort.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from 'vitest'
import { packageSort } from '@/utils.ts'

describe('packageSort', () => {
it('should sort packages alphabetically', () => {
const input = {
react: '18.2.0',
axios: '1.3.0',
lodash: '4.17.21',
}
const expected = {
axios: '1.3.0',
lodash: '4.17.21',
react: '18.2.0',
}
expect(packageSort(input)).toEqual(expected)
expect(Object.keys(packageSort(input))).toEqual(['axios', 'lodash', 'react'])
})

it('should handle scoped packages correctly', () => {
const input = {
'zod': '3.21.4',
'@types/node': '18.15.0',
'react': '18.2.0',
'@clack/prompts': '0.6.0',
}
const expected = {
'@clack/prompts': '0.6.0',
'@types/node': '18.15.0',
'react': '18.2.0',
'zod': '3.21.4',
}
expect(packageSort(input)).toEqual(expected)
expect(Object.keys(packageSort(input))).toEqual([
'@clack/prompts',
'@types/node',
'react',
'zod',
])
})
})