From ec22aa7a891c3b5621178b3982281490473f51b9 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Fri, 26 Jul 2024 09:54:39 +0200 Subject: [PATCH] fix: add playwright dependency in the correct order Currently, the schematics adds playwright at the end of the dev dependencies. This updates the schematic to insert it ar the proper place (with the same code used by the angular-eslint schematics). --- src/schematics/ng-add/index.spec.ts | 4 ++++ src/schematics/ng-add/index.ts | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/schematics/ng-add/index.spec.ts b/src/schematics/ng-add/index.spec.ts index 5bd0865..74254da 100644 --- a/src/schematics/ng-add/index.spec.ts +++ b/src/schematics/ng-add/index.spec.ts @@ -53,5 +53,9 @@ describe('ng-add', () => { const packageJSON = JSON.parse(tree.readContent('/package.json')); expect(packageJSON.devDependencies['@playwright/test']).toBeTruthy(); + // check that the dependency is added in the correct place + expect(Object.keys(packageJSON.devDependencies)).toEqual( + Object.keys(packageJSON.devDependencies).sort(), + ); }); }); diff --git a/src/schematics/ng-add/index.ts b/src/schematics/ng-add/index.ts index a7a3cf2..b0361d2 100644 --- a/src/schematics/ng-add/index.ts +++ b/src/schematics/ng-add/index.ts @@ -106,6 +106,7 @@ function addPackageToPackageJson( if (!json.devDependencies[pkg]) { json.devDependencies[pkg] = version; } + json.devDependencies = sortObjectByKeys(json.devDependencies); tree.overwrite('package.json', JSON.stringify(json, null, 2)); return tree; @@ -123,3 +124,17 @@ function addPlaywright(tree: Tree, context: SchematicContext) { PLAYWRIGHT_TEST_VERSION, ); } + +function sortObjectByKeys( + obj: Record, +): Record { + return Object.keys(obj) + .sort() + .reduce((result, key) => { + return { + // biome-ignore lint/performance/noAccumulatingSpread: small object, no perf cost + ...result, + [key]: obj[key], + }; + }, {}); +}