Skip to content

Commit

Permalink
feat: check only the major slice on nestjs dep mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Feb 6, 2024
1 parent ec761a4 commit 4c89de4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
35 changes: 17 additions & 18 deletions actions/info.action.ts
Expand Up @@ -30,7 +30,7 @@ interface NestDependencyWarnings {

export class InfoAction extends AbstractAction {
private manager!: AbstractPackageManager;
// Nest dependencies whitelist used to compare minor version
// Nest dependencies whitelist used to compare the major version
private warningMessageDependenciesWhiteList = [
'@nestjs/core',
'@nestjs/common',
Expand Down Expand Up @@ -131,15 +131,15 @@ export class InfoAction extends AbstractAction {
displayWarningMessage(nestDependencies: NestDependency[]) {
try {
const warnings = this.buildNestVersionsWarningMessage(nestDependencies);
const minorVersions = Object.keys(warnings);
if (minorVersions.length > 0) {
const majorVersions = Object.keys(warnings);
if (majorVersions.length > 0) {
console.info('\r');
console.info(chalk.yellow('[Warnings]'));
console.info(
'The following packages are not in the same minor version',
'The following packages are not in the same major version',
);
console.info('This could lead to runtime errors');
minorVersions.forEach((version) => {
majorVersions.forEach((version) => {
console.info(chalk.bold(`* Under version ${version}`));
warnings[version].forEach(({ packageName, value }) => {
console.info(`- ${packageName} ${value}`);
Expand All @@ -161,24 +161,23 @@ export class InfoAction extends AbstractAction {
buildNestVersionsWarningMessage(
nestDependencies: NestDependency[],
): NestDependencyWarnings {
const unsortedWarnings: NestDependencyWarnings =
nestDependencies.reduce<NestDependencyWarnings>(
(acc, { name, packageName, value }) => {
const unsortedWarnings =
nestDependencies.reduce(
(depWarningsGroup, { name, packageName, value }) => {
if (!this.warningMessageDependenciesWhiteList.includes(packageName)) {
return acc;
return depWarningsGroup;
}

const cleanedValue = value.replace(/[^\d.]/g, '');
const [major, minor] = cleanedValue.split('.');
const minorVersion = `${major}.${minor}`;
acc[minorVersion] = [
...(acc[minorVersion] || []),
const [major,] = value.replace(/[^\d.]/g, '').split('.', 1);
const minimumVersion = major;
depWarningsGroup[minimumVersion] = [
...(depWarningsGroup[minimumVersion] || []),
{ name, packageName, value },
];

return acc;
return depWarningsGroup;
},
{},
Object.create(null) as NestDependencyWarnings,
);

const unsortedMinorVersions = Object.keys(unsortedWarnings);
Expand All @@ -201,12 +200,12 @@ export class InfoAction extends AbstractAction {
},
);

return sortedMinorVersions.reduce<NestDependencyWarnings>(
return sortedMinorVersions.reduce(
(warnings, minorVersion) => {
warnings[minorVersion] = unsortedWarnings[minorVersion];
return warnings;
},
{},
Object.create(null) as NestDependencyWarnings,
);
}

Expand Down
21 changes: 10 additions & 11 deletions test/actions/info.action.spec.ts
Expand Up @@ -69,7 +69,7 @@ describe('InfoAction', () => {
];
const result = infoAction.buildNestVersionsWarningMessage(dependencies);
const expected = {
'1.2': [
'1': [
{ packageName: '@nestjs/core', name: 'core', value: '1.2.3' },
{ packageName: '@nestjs/common', name: 'common', value: '1.2.4' },
{
Expand All @@ -93,7 +93,7 @@ describe('InfoAction', () => {
value: '1.2.4',
},
],
'2.1': [
'2': [
{
packageName: '@nestjs/platform-ws',
name: 'platform-ws',
Expand Down Expand Up @@ -131,21 +131,22 @@ describe('InfoAction', () => {
name: 'platform-socket.io',
value: '1.2$$.4',
},
{
name: 'platform-socket.io',
packageName: '@nestjs/platform-socket.io',
value: '2.0.1',
},
{
packageName: '@nestjs/websockets',
name: 'websockets',
value: '^2.*&1.0',
},

{
name: 'platform-socket.io',
packageName: '@nestjs/platform-socket.io',
value: '2.0.1',
},
];

const result = infoAction.buildNestVersionsWarningMessage(dependencies);
const expected = {
'2.1': [
'2': [
{
name: 'platform-fastify',
packageName: '@nestjs/platform-fastify',
Expand All @@ -156,15 +157,13 @@ describe('InfoAction', () => {
name: 'websockets',
value: '^2.*&1.0',
},
],
'2.0': [
{
name: 'platform-socket.io',
packageName: '@nestjs/platform-socket.io',
value: '2.0.1',
},
],
'1.2': [
'1': [
{
name: 'schematics',
packageName: '@nestjs/schematics',
Expand Down

0 comments on commit 4c89de4

Please sign in to comment.