diff --git a/src/commands/migrate.command.ts b/src/commands/migrate.command.ts index bf0b0eb9d..89a640351 100644 --- a/src/commands/migrate.command.ts +++ b/src/commands/migrate.command.ts @@ -9,6 +9,7 @@ import { getRemovableNpmDependencies, removeNpmDependencies, updateNpmScripts, + cleanUpPackageJson, } from '../utils/utils.npm'; export const migrate = async () => { @@ -90,7 +91,11 @@ export const migrate = async () => { printMessage(TEXT.updateNpmScriptsAborted); } - // 4. Summary + // Tidy package.json file so any changed fields are sorted as a + // package manager would expect. + cleanUpPackageJson(); + + // 7. Summary // ------------- printSuccessMessage(TEXT.migrationCommandSuccess); } catch (error) { diff --git a/src/utils/utils.npm.ts b/src/utils/utils.npm.ts index 1f230fadf..b420f49ce 100644 --- a/src/utils/utils.npm.ts +++ b/src/utils/utils.npm.ts @@ -187,3 +187,20 @@ export function updateNpmScripts() { writePackageJson(packageJson); } + +export function cleanUpPackageJson() { + const packageJson = getPackageJson(); + packageJson.scripts = sortKeysAlphabetically(packageJson.scripts); + packageJson.dependencies = sortKeysAlphabetically(packageJson.dependencies); + packageJson.devDependencies = sortKeysAlphabetically(packageJson.devDependencies); + writePackageJson(packageJson); +} + +function sortKeysAlphabetically(obj: Record) { + return Object.keys(obj) + .sort() + .reduce>((acc, key) => { + acc[key] = obj[key]; + return acc; + }, {}); +}