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

[rush-migrate-subspace-plugin]: add clean subspace feature #22

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "rush-migrate-subspace-plugin",
"comment": "Provides a parameter to automatically remove similar dependency versions, including duplicates and unused",
"type": "minor"
}
],
"packageName": "rush-migrate-subspace-plugin"
}
6 changes: 6 additions & 0 deletions rush-plugins/rush-migrate-subspace-plugin/command-line.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"longName": "--debug",
"description": "Provide debug logs",
"associatedCommands": ["migrate-subspace"]
},
{
"parameterKind": "flag",
"longName": "--clean",
"description": "Merge and clean multiple common versions together",
"associatedCommands": ["migrate-subspace"]
}
]
}
262 changes: 250 additions & 12 deletions rush-plugins/rush-migrate-subspace-plugin/src/cleanSubspace.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,243 @@
import { chooseSubspacePrompt } from './prompts/subspace';
import {
chooseSubspacePrompt,
scanForUnusedDependencyVersionsPrompt,
scanForDuplicatedDependenciesPrompt,
scanForSupersetDependencyVersionsPrompt,
scanForAllDependenciesPrompt
} from './prompts/subspace';
import Console from './providers/console';
import { getRootPath } from './utilities/path';
import { Colorize } from '@rushstack/terminal';
import {
cleanSubspaceCommonVersions,
getRushSubspaceCommonVersionsFilePath,
isSubspaceSupported
getSubspaceDependencies,
isSubspaceSupported,
loadRushSubspaceCommonVersions,
queryProjectsFromSubspace
} from './utilities/subspace';
import { getRushSubspacesConfigurationJsonPath, querySubspaces } from './utilities/repository';
import { RushConstants } from '@rushstack/rush-sdk';
import { chooseDependencyPrompt, confirmNextDependencyPrompt } from './prompts/dependency';
import { IPackageJson, JsonFile } from '@rushstack/node-core-library';
import { rSortVersions, subsetVersion } from './utilities/dependency';
import {
pedro-gomes-92 marked this conversation as resolved.
Show resolved Hide resolved
getProjectPackageFilePath,
loadProjectPackageJson,
updateProjectDependency
} from './utilities/project';
import { IRushConfigurationProjectJson } from '@rushstack/rush-sdk/lib/api/RushConfigurationProject';
import { RESERVED_VERSIONS } from './constants/versions';

const removeSupersetDependency = async (
subspaceName: string,
dependencyName: string,
versionsMap: Map<string, string[]>,
rootPath: string = getRootPath()
): Promise<number> => {
const versions: string[] = Array.from(versionsMap.keys());
const subspaceCommonVersionsPath: string = getRushSubspaceCommonVersionsFilePath(subspaceName, rootPath);
const subspaceCommonVersionsJson: RushSubspaceCommonVersionsJson = loadRushSubspaceCommonVersions(
subspaceName,
rootPath
);

const newValidVersions: string[] = rSortVersions(versions).reduce<string[]>((prevVersions, currVersion) => {
const newVersions: string[] = [...prevVersions];
if (newVersions.includes(currVersion)) {
return newVersions;
}

const newSubsetVersion: string | undefined = newVersions.find((newVersion) =>
subsetVersion(newVersion, currVersion)
);

if (RESERVED_VERSIONS.includes(currVersion) || !newSubsetVersion) {
newVersions.push(currVersion);
} else {
// Update projects with new subset version
versionsMap.get(currVersion)?.forEach((projectName) => {
if (updateProjectDependency(projectName, dependencyName, newSubsetVersion, rootPath)) {
Console.debug(
`Updated project ${Colorize.bold(projectName)} for dependency ${Colorize.bold(
dependencyName
)} ${Colorize.bold(currVersion)} => ${Colorize.bold(newSubsetVersion)}!`
);
}
});
}

return newVersions;
}, []);

const removedAlternativeVersionsCount: number = versions.length - newValidVersions.length;
if (removedAlternativeVersionsCount > 0) {
// Update subspace common versions
if (newValidVersions.length > 0) {
subspaceCommonVersionsJson.allowedAlternativeVersions![dependencyName] = newValidVersions;
} else {
delete subspaceCommonVersionsJson.allowedAlternativeVersions![dependencyName];
}

JsonFile.save(subspaceCommonVersionsJson, subspaceCommonVersionsPath);
}

return removedAlternativeVersionsCount;
};

const removeDuplicatedDependencies = (subspaceName: string, rootPath: string = getRootPath()): void => {
Console.log(`Removing duplicated dependencies for subspace ${Colorize.bold(subspaceName)}...`);

const projects: IRushConfigurationProjectJson[] = queryProjectsFromSubspace(subspaceName, rootPath);
let countRemoved: number = 0;
projects.forEach((project) => {
const projectPackageFilePath: string = getProjectPackageFilePath(project.projectFolder, rootPath);
const projectPackageJson: IPackageJson = loadProjectPackageJson(project.projectFolder, rootPath);

const dependencies: string[] = Object.keys(projectPackageJson.dependencies || {});
const devDependencies: string[] = Object.keys(projectPackageJson.devDependencies || {});

devDependencies.forEach((devDependency) => {
if (dependencies.includes(devDependency)) {
pedro-gomes-92 marked this conversation as resolved.
Show resolved Hide resolved
countRemoved += 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is valid to declare something like this:

   "dependencies": {
      "thing": "^1.0.0"  // people who install my project can use the latest 1.x
   },
   "devDependencies": {
      "thing": "1.2.3" // but for development, I want the specific version 1.2.3
   },

Copy link
Collaborator Author

@pedro-gomes-92 pedro-gomes-92 Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@octogonz I understand your point, but for the sake of cleaning a subspace dependency tree, don't you think it is more beneficial for a package to keep 1 standard version instead of multiple possible versions?
In other words, what is the benefit for developers and users to use 2 different versions of "thing", which can lead to different results (and even issues that might not be tracked during development)?

Console.debug(
`Removed ${Colorize.bold(devDependency)} from project ${Colorize.bold(project.packageName)}`
);
delete projectPackageJson.devDependencies![devDependency];
}
});

JsonFile.save(projectPackageJson, projectPackageFilePath);
});

if (countRemoved > 0) {
Console.success(
`Removed ${Colorize.bold(`${countRemoved}`)} duplicated dependencies from subspace ${Colorize.bold(
subspaceName
)}!`
);
} else {
Console.success(`No duplicated dependencies found for subspace ${Colorize.bold(subspaceName)}!`);
}
};

const removeUnusedAlternativeVersions = (
subspaceName: string,
subspaceDependencies: Map<string, Map<string, string[]>>
): void => {
Console.log(`Removing unused alternative versions for subspace ${Colorize.bold(subspaceName)}...`);

const subspaceCommonVersionsPath: string = getRushSubspaceCommonVersionsFilePath(subspaceName);
const subspaceCommonVersionsJson: RushSubspaceCommonVersionsJson =
loadRushSubspaceCommonVersions(subspaceName);

if (!subspaceCommonVersionsJson.allowedAlternativeVersions) {
return;
}

let countRemoved: number = 0;

for (const [dependency, alternativeVersions] of Object.entries(
subspaceCommonVersionsJson.allowedAlternativeVersions
)) {
const subspaceDependency: Map<string, string[]> | undefined = subspaceDependencies.get(dependency);
const newAlternativeVersions: string[] =
subspaceDependency && subspaceDependency.size > 1
? alternativeVersions.filter((version) => subspaceDependency.has(version))
: [];

const removedAlternativeVersionsCount: number =
alternativeVersions.length - newAlternativeVersions.length;
if (removedAlternativeVersionsCount > 0) {
countRemoved += removedAlternativeVersionsCount;
Console.debug(
`Moving from [${Colorize.bold(alternativeVersions.join(','))}] to [${Colorize.bold(
newAlternativeVersions.join(',')
)}] for dependency ${Colorize.bold(dependency)}`
);
}

if (newAlternativeVersions.length === 0) {
delete subspaceCommonVersionsJson.allowedAlternativeVersions[dependency];
continue;
}

subspaceCommonVersionsJson.allowedAlternativeVersions = {
...subspaceCommonVersionsJson.allowedAlternativeVersions,
[dependency]: newAlternativeVersions
};
}

if (countRemoved > 0) {
JsonFile.save(subspaceCommonVersionsJson, subspaceCommonVersionsPath);
Console.success(
`Removed ${Colorize.bold(`${countRemoved}`)} unused alternative versions from subspace ${Colorize.bold(
subspaceName
)}!`
);
} else {
Console.success(`No unused alternative versions found for subspace ${Colorize.bold(subspaceName)}!`);
}
};

const removeSupersetDependencyVersions = async (
subspaceName: string,
subspaceDependencies: Map<string, Map<string, string[]>>
): Promise<void> => {
const multipleVersionDependencies: string[] = Array.from(subspaceDependencies.keys()).filter(
(dependency) => subspaceDependencies.get(dependency)!.size > 1
);

if (multipleVersionDependencies.length === 0) {
Console.success(
`The subspace ${Colorize.bold(subspaceName)} doesn't contain alternative versions! Exiting...`
);
return;
}

if (await scanForAllDependenciesPrompt()) {
Console.log(`Removing superset versions for subspace ${Colorize.bold(subspaceName)}...`);
await Promise.all(
Array.from(subspaceDependencies.entries()).map(([dependency, versionsMap]) =>
pedro-gomes-92 marked this conversation as resolved.
Show resolved Hide resolved
removeSupersetDependency(subspaceName, dependency, versionsMap)
)
).then((countPerDependency) => {
const count: number = countPerDependency.reduce((a, b) => a + b, 0);
pedro-gomes-92 marked this conversation as resolved.
Show resolved Hide resolved
if (count > 0) {
Console.success(`Removed ${Colorize.bold(`${count}`)} superset alternative versions!`);
} else {
Console.success(`No alternative versions have been removed!`);
}
});

return;
}

do {
const selectedDependency: string = await chooseDependencyPrompt(multipleVersionDependencies);

Console.log(`Removing superset versions for dependency ${Colorize.bold(selectedDependency)}...`);
const count: number = await removeSupersetDependency(
subspaceName,
selectedDependency,
subspaceDependencies.get(selectedDependency) as Map<string, string[]>
);

if (count > 0) {
Console.success(
`Removed ${Colorize.bold(`${count}`)} superset alternative versions for dependency ${Colorize.bold(
selectedDependency
)}!`
);
} else {
Console.success(
`No alternative versions have been removed for dependency ${Colorize.bold(selectedDependency)}!`
);
}

const index: number = multipleVersionDependencies.indexOf(selectedDependency);
multipleVersionDependencies.splice(index, 1);
} while (multipleVersionDependencies.length > 0 && (await confirmNextDependencyPrompt()));
};

export const cleanSubspace = async (): Promise<void> => {
Console.debug('Executing clean subspace command...');
Expand All @@ -26,15 +255,24 @@ export const cleanSubspace = async (): Promise<void> => {
}

const targetSubspace: string = await chooseSubspacePrompt(targetSubspaces);
Console.title(`🛁 Cleaning subspace ${Colorize.underline(targetSubspace)} common versions...`);
Console.title(`🛁 Cleaning subspace ${Colorize.underline(targetSubspace)} alternative versions...`);

if (cleanSubspaceCommonVersions(targetSubspace)) {
Console.success(
`${Colorize.bold(
getRushSubspaceCommonVersionsFilePath(targetSubspace)
)} has been successfully refactored!`
);
} else {
Console.success(`The subspace ${Colorize.bold(targetSubspace)} doesn't require cleaning! Exiting...`);
let subspaceDependencies: Map<string, Map<string, string[]>> = getSubspaceDependencies(targetSubspace);
if (await scanForDuplicatedDependenciesPrompt()) {
removeDuplicatedDependencies(targetSubspace);
subspaceDependencies = getSubspaceDependencies(targetSubspace);
}

if (await scanForSupersetDependencyVersionsPrompt()) {
await removeSupersetDependencyVersions(targetSubspace, subspaceDependencies);
subspaceDependencies = getSubspaceDependencies(targetSubspace);
}

if (await scanForUnusedDependencyVersionsPrompt()) {
removeUnusedAlternativeVersions(targetSubspace, subspaceDependencies);
}

Console.warn(
`Please run "rush update --subspace ${targetSubspace}" to update the subspace shrinkwrap file.`
);
};
14 changes: 11 additions & 3 deletions rush-plugins/rush-migrate-subspace-plugin/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Command } from 'commander';
import inquirer from 'inquirer';
import path from 'path';
import { IPackageJson, JsonFile } from '@rushstack/node-core-library';
import inquirerSearchList from 'inquirer-search-list';
import { syncVersions } from './syncVersions';
import { migrateProject } from './migrateProject';
import Console from './providers/console';
import { interactMenu } from './interactMenu';
import { cleanSubspace } from './cleanSubspace';

inquirer.registerPrompt('search-list', inquirerSearchList);

Expand All @@ -13,17 +16,22 @@ const program: Command = new Command();
program
.option('--sync', 'to sync the versions in a subspace')
.option('--move', 'to move projects to a new subspace')
.option('--clean', 'to reduce subspace alternative versions')
.option('--debug', 'to provide debug logs')
.description('Example: rush migrate-subspace [--move] [--sync] [--debug]')
.action(async ({ sync, debug, move }) => {
.description('Example: rush migrate-subspace [--move] [--sync] [--debug] [--clean]')
.action(async ({ sync, debug, move, clean }) => {
const packageJson: IPackageJson = JsonFile.load(`${path.resolve(__dirname, '../package.json')}`);

Console.enableDebug(debug);
Console.title('🚀 Welcome to the Rush Migrate Subspace Plugin!');
Console.title(`🚀 Welcome to the Rush Migrate Subspace Plugin! Version: ${packageJson.version}`);
pedro-gomes-92 marked this conversation as resolved.
Show resolved Hide resolved
Console.newLine();

if (sync) {
await syncVersions();
} else if (move) {
await migrateProject();
} else if (clean) {
await cleanSubspace();
} else {
await interactMenu();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const LATEST_VERSION: string = 'latest';
export const LOCAL_VERSION: string = 'workspace:*';

export const RESERVED_VERSIONS: string[] = [LATEST_VERSION, LOCAL_VERSION];
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,17 @@ export const syncProjectMismatchedDependencies = async (projectName: string): Pr
break;
case 'fix':
do {
const selectedDependency: string = await chooseDependencyPrompt(mismatchedDependencies);
const selectedDependency: string = await chooseDependencyPrompt(
mismatchedDependencies,
` (${Colorize.bold(`${mismatchedDependencies.length}`)} mismatched dependencies)`
);
if (await syncDependencyVersion(selectedDependency, projectName)) {
mismatchedDependencies = fetchProjectMismatches(projectName);
}
} while (mismatchedDependencies.length > 0 && (await confirmNextDependencyPrompt(projectName)));
} while (
mismatchedDependencies.length > 0 &&
(await confirmNextDependencyPrompt(` (Current project: ${Colorize.bold(projectName)})`))
);

break;
case 'skip':
Expand Down
6 changes: 6 additions & 0 deletions rush-plugins/rush-migrate-subspace-plugin/src/interactMenu.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cleanSubspace } from './cleanSubspace';
import { migrateProject } from './migrateProject';
import { chooseCommandPrompt } from './prompts/command';
import Console from './providers/console';
Expand All @@ -21,6 +22,11 @@ export const interactMenu = async (): Promise<void> => {
await syncVersions();
Console.newLine();
break;

case 'clean':
await cleanSubspace();
Console.newLine();
break;
}
} while (!exitApplication);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const chooseCommandPrompt = async (): Promise<string> => {
choices: [
{ name: 'Move a project to a new subspace', value: 'move' },
{ name: 'Scan & fix version mismatches', value: 'sync' },
{ name: 'Clean common subspace versions', value: 'clean' },
{ name: 'Exit application', value: 'exit' }
]
}
Expand Down
Loading
Loading