From 2bcb40247c878cbd379fa19fcdc44c8f7437da47 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Fri, 25 Apr 2025 18:51:15 +0800 Subject: [PATCH] refactor: workspace setting sorted by key (#9453) * refactor: workspace setting sorted by key * fix: always put the packages field as the first one in pnpm-workspace.yaml * docs: add changeset --------- Co-authored-by: Zoltan Kochan --- .changeset/free-chairs-cough.md | 6 ++++++ pnpm-lock.yaml | 3 +++ workspace/manifest-writer/package.json | 1 + workspace/manifest-writer/src/index.ts | 7 ++++++- workspace/manifest-writer/tsconfig.json | 3 +++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changeset/free-chairs-cough.md diff --git a/.changeset/free-chairs-cough.md b/.changeset/free-chairs-cough.md new file mode 100644 index 0000000000..7178f5a5fa --- /dev/null +++ b/.changeset/free-chairs-cough.md @@ -0,0 +1,6 @@ +--- +"@pnpm/workspace.manifest-writer": patch +"pnpm": patch +--- + +Sort keys in `pnpm-workspace.yaml` [#9453](https://github.com/pnpm/pnpm/pull/9453). diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b7c9b1985d..e290ccc7a0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8270,6 +8270,9 @@ importers: '@pnpm/constants': specifier: workspace:* version: link:../../packages/constants + '@pnpm/object.key-sorting': + specifier: workspace:* + version: link:../../object/key-sorting '@pnpm/workspace.read-manifest': specifier: workspace:* version: link:../read-manifest diff --git a/workspace/manifest-writer/package.json b/workspace/manifest-writer/package.json index cd5f293947..1f86cfb8ed 100644 --- a/workspace/manifest-writer/package.json +++ b/workspace/manifest-writer/package.json @@ -31,6 +31,7 @@ }, "dependencies": { "@pnpm/constants": "workspace:*", + "@pnpm/object.key-sorting": "workspace:*", "@pnpm/workspace.read-manifest": "workspace:*", "ramda": "catalog:", "write-yaml-file": "catalog:" diff --git a/workspace/manifest-writer/src/index.ts b/workspace/manifest-writer/src/index.ts index 576b68d0b1..2f59962a96 100644 --- a/workspace/manifest-writer/src/index.ts +++ b/workspace/manifest-writer/src/index.ts @@ -4,9 +4,10 @@ import { readWorkspaceManifest, type WorkspaceManifest } from '@pnpm/workspace.r import { WORKSPACE_MANIFEST_FILENAME } from '@pnpm/constants' import writeYamlFile from 'write-yaml-file' import equals from 'ramda/src/equals' +import { sortKeysByPriority } from '@pnpm/object.key-sorting' export async function updateWorkspaceManifest (dir: string, updatedFields: Partial): Promise { - const manifest = await readWorkspaceManifest(dir) ?? {} as WorkspaceManifest + let manifest = await readWorkspaceManifest(dir) ?? {} as WorkspaceManifest let shouldBeUpdated = false for (const [key, value] of Object.entries(updatedFields)) { if (!equals(manifest[key as keyof WorkspaceManifest], value)) { @@ -26,6 +27,10 @@ export async function updateWorkspaceManifest (dir: string, updatedFields: Parti await fs.promises.rm(path.join(dir, WORKSPACE_MANIFEST_FILENAME)) return } + manifest = sortKeysByPriority({ + priority: { packages: 0 }, + deep: false, + }, manifest) await writeYamlFile(path.join(dir, WORKSPACE_MANIFEST_FILENAME), manifest, { lineWidth: -1, // This is setting line width to never wrap blankLines: true, diff --git a/workspace/manifest-writer/tsconfig.json b/workspace/manifest-writer/tsconfig.json index aa058c6ff8..24a85d3efd 100644 --- a/workspace/manifest-writer/tsconfig.json +++ b/workspace/manifest-writer/tsconfig.json @@ -12,6 +12,9 @@ { "path": "../../__utils__/prepare-temp-dir" }, + { + "path": "../../object/key-sorting" + }, { "path": "../../packages/constants" },