Skip to content

Commit

Permalink
sorted without adjusting other property positions
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm authored Dec 15, 2023
1 parent 924e349 commit 4d30617
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .changeset/grumpy-insects-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'svelte-migrate': patch
---

chore: sort packages after insert
chore: insert package at sorted position
18 changes: 11 additions & 7 deletions packages/migrate/utils.js
Original file line number Diff line number Diff line change
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 All @@ -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);
}

Expand Down Expand Up @@ -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')));
}
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 4d30617

Please sign in to comment.