Skip to content

Commit

Permalink
chore: sort packages after insert (#11332)
Browse files Browse the repository at this point in the history
inserted without adjusting other property positions

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
  • Loading branch information
hyunbinseo and dummdidumm authored Dec 15, 2023
1 parent f7c44e1 commit f0dcc76
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-insects-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

chore: insert package at sorted position
16 changes: 12 additions & 4 deletions packages/migrate/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';
import colors from 'kleur';
import ts from 'typescript';
import MagicString from 'magic-string';
import { execFileSync, execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import semver from 'semver';
import ts from 'typescript';

/** @param {string} message */
export function bail(message) {
Expand Down Expand Up @@ -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}`);
}
}
Expand Down
73 changes: 73 additions & 0 deletions packages/migrate/utils.spec.js
Original file line number Diff line number Diff line change
@@ -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"
}
}`
);
});

0 comments on commit f0dcc76

Please sign in to comment.