From 4d306172d26ed75967bfe4787e040b91a5dd6522 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 15 Dec 2023 09:25:18 +0100 Subject: [PATCH] sorted without adjusting other property positions --- .changeset/grumpy-insects-flow.md | 2 +- packages/migrate/utils.js | 18 +++++--- packages/migrate/utils.spec.js | 73 +++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 packages/migrate/utils.spec.js diff --git a/.changeset/grumpy-insects-flow.md b/.changeset/grumpy-insects-flow.md index 3a54414aec79..cac8f1a6a1e5 100644 --- a/.changeset/grumpy-insects-flow.md +++ b/.changeset/grumpy-insects-flow.md @@ -2,4 +2,4 @@ 'svelte-migrate': patch --- -chore: sort packages after insert +chore: insert package at sorted position diff --git a/packages/migrate/utils.js b/packages/migrate/utils.js index 35eddc5780f4..a01be4bde3f6 100644 --- a/packages/migrate/utils.js +++ b/packages/migrate/utils.js @@ -235,7 +235,15 @@ export function update_pkg(content, updates) { if (insert && !pkg[insert]?.[name]) { if (!pkg[insert]) pkg[insert] = {}; - pkg[insert][name] = version; + + // Insert the property in sorted position without adjusting other positions so diffs are easier to read + const sorted_keys = Object.keys(pkg[insert]).sort(); + const index = sorted_keys.findIndex((key) => name.localeCompare(key) === -1); + const insert_index = index !== -1 ? index : sorted_keys.length; + const new_properties = Object.entries(pkg[insert]); + new_properties.splice(insert_index, 0, [name, version]); + pkg[insert] = Object.fromEntries(new_properties); + log_migration(`Added ${name} version ${version} ${additional}`); } } @@ -244,10 +252,6 @@ export function update_pkg(content, updates) { update_pkg(...update); } - ['dependencies', 'devDependencies'].forEach((key) => { - if (key in pkg) pkg[key] = Object.fromEntries(Object.entries(pkg[key]).sort()); - }); - return JSON.stringify(pkg, null, indent); } @@ -326,8 +330,8 @@ export function update_tsconfig(update_tsconfig_content) { const file = fs.existsSync('tsconfig.json') ? 'tsconfig.json' : fs.existsSync('jsconfig.json') - ? 'jsconfig.json' - : null; + ? 'jsconfig.json' + : null; if (file) { fs.writeFileSync(file, update_tsconfig_content(fs.readFileSync(file, 'utf8'))); } diff --git a/packages/migrate/utils.spec.js b/packages/migrate/utils.spec.js new file mode 100644 index 000000000000..cd9437e2968f --- /dev/null +++ b/packages/migrate/utils.spec.js @@ -0,0 +1,73 @@ +import { assert, test } from 'vitest'; +import { update_pkg } from './utils.js'; + +test('Inserts package at correct position (1)', () => { + const result = update_pkg( + `{ + "dependencies": { + "a": "1", + "z": "3", + "c": "4" + } +}`, + [['b', '2', '', 'dependencies']] + ); + + assert.equal( + result, + `{ + "dependencies": { + "a": "1", + "b": "2", + "z": "3", + "c": "4" + } +}` + ); +}); + +test('Inserts package at correct position (2)', () => { + const result = update_pkg( + `{ + "dependencies": { + "a": "1", + "b": "2" + } +}`, + [['c', '3', '', 'dependencies']] + ); + + assert.equal( + result, + `{ + "dependencies": { + "a": "1", + "b": "2", + "c": "3" + } +}` + ); +}); + +test('Inserts package at correct position (3)', () => { + const result = update_pkg( + `{ + "dependencies": { + "b": "2", + "c": "3" + } +}`, + [['a', '1', '', 'dependencies']] + ); + + assert.equal( + result, + `{ + "dependencies": { + "a": "1", + "b": "2", + "c": "3" + } +}` + ); +});