Skip to content

Commit

Permalink
Merge pull request #68 from grafana/jackw/clean-package-json
Browse files Browse the repository at this point in the history
Migrate: Sort package.json fields
  • Loading branch information
jackw authored Oct 17, 2022
2 parents f0f7ec8 + 0c5a7c3 commit bd741f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/commands/migrate.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getRemovableNpmDependencies,
removeNpmDependencies,
updateNpmScripts,
cleanUpPackageJson,
} from '../utils/utils.npm';

export const migrate = async () => {
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/utils.npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>) {
return Object.keys(obj)
.sort()
.reduce<Record<string, string>>((acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
}

0 comments on commit bd741f0

Please sign in to comment.