cli: sort dependencies after modifying them with diff

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-06-17 20:02:53 +02:00
parent 22fa107eb8
commit 22684fa0d4
+25 -3
View File
@@ -18,6 +18,15 @@ import chalk from 'chalk';
import { diffLines } from 'diff';
import { FileDiff, PromptFunc, FileHandler, WriteFileFunc } from './types';
function sortObjectKeys(obj: Record<string, unknown>) {
const sortedKeys = Object.keys(obj).sort();
for (const key of sortedKeys) {
const value = obj[key];
delete obj[key];
obj[key] = value;
}
}
class PackageJsonHandler {
static async handler(
{ path, write, missing, targetContents, templateContents }: FileDiff,
@@ -74,6 +83,7 @@ class PackageJsonHandler {
obj: any = this.pkg,
targetObj: any = this.targetPkg,
prefix?: string,
sort?: boolean,
) {
const fullFieldName = chalk.cyan(
prefix ? `${prefix}[${fieldName}]` : fieldName,
@@ -91,6 +101,9 @@ class PackageJsonHandler {
const msg = `package.json has mismatched field, ${fullFieldName}, change from ${coloredOldValue} to ${coloredNewValue}?`;
if (await this.prompt(msg)) {
targetObj[fieldName] = newValue;
if (sort) {
sortObjectKeys(targetObj);
}
await this.write();
}
} else if (fieldName in obj) {
@@ -100,6 +113,9 @@ class PackageJsonHandler {
)
) {
targetObj[fieldName] = newValue;
if (sort) {
sortObjectKeys(targetObj);
}
await this.write();
}
}
@@ -169,15 +185,21 @@ class PackageJsonHandler {
}
// Hardcoded removal of these during migration
await this.syncField('@backstage/core', {}, targetDeps, fieldName);
await this.syncField('@backstage/core-api', {}, targetDeps, fieldName);
await this.syncField('@backstage/core', {}, targetDeps, fieldName, true);
await this.syncField(
'@backstage/core-api',
{},
targetDeps,
fieldName,
true,
);
for (const key of Object.keys(pkgDeps)) {
if (this.variant === 'app' && key.startsWith('plugin-')) {
continue;
}
await this.syncField(key, pkgDeps, targetDeps, fieldName);
await this.syncField(key, pkgDeps, targetDeps, fieldName, true);
}
}