Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: group changes by the change type and color them #671

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ program
.description('upgrade dependencies and devDependencies of all packages')
.option('--dry-run', 'no actual upgrading (just the fetching process)', false)
.option('--registry <url>', 'npm registry to use')
.action(async (targetPath: string, { dryRun, registry }) => {
.option('--no-color', 'disable colored output', true)
.action(async (targetPath: string, { dryRun, registry, color }) => {
const { upgrade } = await import('./commands/upgrade.js');

await upgrade({
directoryPath: path.resolve(targetPath || ''),
dryRun,
color,
registryUrl: registry,
});
});
Expand Down
46 changes: 35 additions & 11 deletions src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { createCliProgressBar } from '../utils/cli-progress-bar.js';
import { loadPlebConfig, normalizePinnedPackages } from '../utils/config.js';
import { loadEnvNpmConfig } from '../utils/npm-config.js';
import { NpmRegistry, officialNpmRegistryUrl, uriToIdentifier } from '../utils/npm-registry.js';
import { getChangeType, colorizeChangeType, type ChangeType, groupByChangeType } from '../utils/get-change-type.js';

const { gt, coerce } = semver;

export interface UpgradeOptions {
directoryPath: string;
dryRun?: boolean;
registryUrl?: string;
color?: boolean;
log?: (message: unknown) => void;
logError?: (message: unknown) => void;
}
Expand All @@ -23,6 +25,7 @@ export async function upgrade({
directoryPath,
registryUrl,
dryRun,
color = true,
log = console.log,
logError = console.error,
}: UpgradeOptions): Promise<void> {
Expand Down Expand Up @@ -80,24 +83,32 @@ export async function upgrade({
}
};

const replacements = new Map<string, { originalValue: string; newValue: string }>();
const skipped = new Map<string, { originalValue: string; newValue: string; reason: string }>();
const replacements = new Map<string, { originalValue: string; newValue: string; changeType: ChangeType }>();
const skipped = new Map<
string,
{ originalValue: string; newValue: string; reason: string; changeType: ChangeType }
>();

function mapDependencies(obj: Partial<Record<string, string>>): Partial<Record<string, string>> {
const newObj: Partial<Record<string, string>> = {};
for (const [packageName, request] of Object.entries(obj)) {
const newVersionRequest = getVersionRequest(packageName, request!);
newObj[packageName] = request;

const changeType = getChangeType(request!, newVersionRequest);
if (newVersionRequest !== request) {
if (pinnedPackages.has(packageName)) {
skipped.set(packageName, {
originalValue: request!,
newValue: newVersionRequest,
reason: pinnedPackages.get(packageName)!,
changeType,
});
} else {
replacements.set(packageName, { originalValue: request!, newValue: newVersionRequest });
replacements.set(packageName, {
originalValue: request!,
newValue: newVersionRequest,
changeType,
});
newObj[packageName] = newVersionRequest;
}
}
Expand Down Expand Up @@ -127,19 +138,32 @@ export async function upgrade({
if (replacements.size) {
log('Changes:');
const maxKeyLength = Array.from(replacements.keys()).reduce((acc, key) => Math.max(acc, key.length), 0);
for (const [key, { originalValue, newValue }] of replacements) {
log(` ${key.padEnd(maxKeyLength + 2)} ${originalValue.padStart(8)} -> ${newValue}`);
for (const changeGroup of Object.values(groupByChangeType(replacements))) {
for (const [key, { originalValue, newValue, changeType }] of changeGroup) {
log(
colorizeChangeType(
color,
changeType,
` ${key.padEnd(maxKeyLength + 2)} ${originalValue.padStart(8)} -> ${newValue}`,
),
);
}
}
}

if (skipped.size) {
log('Skipped:');
const maxKeyLength = Array.from(skipped.keys()).reduce((acc, key) => Math.max(acc, key.length), 0);
for (const [key, { originalValue, reason, newValue }] of skipped) {
log(
` ${key.padEnd(maxKeyLength + 2)} ${originalValue.padStart(8)} -> ${newValue}` +
(reason ? ` (${reason})` : ``),
);
for (const skippedGroup of Object.values(groupByChangeType(skipped))) {
for (const [key, { originalValue, reason, newValue, changeType }] of skippedGroup) {
log(
colorizeChangeType(
color,
changeType,
` ${key.padEnd(maxKeyLength + 2)} ${originalValue.padStart(8)} -> ${newValue}`,
) + (reason ? ` (${reason})` : ``),
);
}
}
}

Expand Down
66 changes: 66 additions & 0 deletions src/utils/get-change-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import semver from 'semver';

const colors = {
bgWhite: (text: string) => '\x1b[47m' + text + '\x1b[0m',
green: (text: string) => '\x1b[32m' + text + '\x1b[0m',
red: (text: string) => '\x1b[31m' + text + '\x1b[0m',
yellow: (text: string) => '\x1b[33m' + text + '\x1b[0m',
};

export type ChangeType = semver.ReleaseType | 'unknown';

/**
* Returns a colorized message based on the type of change.
*/
export function colorizeChangeType(color: boolean, changeType: ChangeType, message: string) {
if (!color) {
return message;
}
switch (changeType) {
case 'unknown':
return message;
case 'prepatch':
case 'preminor':
case 'premajor':
case 'prerelease':
return colors.bgWhite(colors.red(message));
case 'major':
return colors.red(message);
case 'minor':
return colors.yellow(message);
case 'patch':
return colors.green(message);
}
}

/**
* Returns the type of change between two versions.
*/
export function getChangeType(oldVersion: string, newVersion: string): ChangeType {
const oldVer = semver.coerce(oldVersion);
const newVer = semver.coerce(newVersion);
if (!oldVer || !newVer) {
return 'unknown';
}
return semver.diff(oldVer, newVer) ?? 'unknown';
}

/**
* Groups a map of packages by their change type. into a constant order of change types.
*/
export function groupByChangeType<U extends { changeType: ChangeType }>(map: Map<string, U>) {
const groups: Record<ChangeType, [string, U][]> = {
prerelease: [],
premajor: [],
major: [],
preminor: [],
minor: [],
prepatch: [],
patch: [],
unknown: [],
};
for (const [key, value] of map) {
groups[value.changeType].push([key, value]);
}
return groups;
}