diff --git a/examples/fixtures/basic/pnpm-workspace.yaml b/examples/fixtures/basic/pnpm-workspace.yaml index 62cf6db..75e867b 100644 --- a/examples/fixtures/basic/pnpm-workspace.yaml +++ b/examples/fixtures/basic/pnpm-workspace.yaml @@ -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 diff --git a/src/utils.ts b/src/utils.ts index 5588d34..3dd91f7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -113,3 +113,12 @@ export const isCancelProcess = (value: unknown, message: string) => { return process.exit(0) } } + +export const packageSort = (packages: Record) => + Object.fromEntries( + Object.entries(packages).sort(([a], [b]) => + a.startsWith('@') === b.startsWith('@') + ? a.localeCompare(b) + : a.startsWith('@') ? -1 : 1, + ), + ) diff --git a/src/work.space.ts b/src/work.space.ts index f9f9825..795c8bf 100644 --- a/src/work.space.ts +++ b/src/work.space.ts @@ -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 => { const workSpaceYamlPath = await findUp('pnpm-workspace.yaml', { @@ -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 diff --git a/tests/package.sort.spec.ts b/tests/package.sort.spec.ts new file mode 100644 index 0000000..bca60e2 --- /dev/null +++ b/tests/package.sort.spec.ts @@ -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', + ]) + }) +})