-
Notifications
You must be signed in to change notification settings - Fork 4
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
pedro-gomes-92
wants to merge
11
commits into
tiktok:main
Choose a base branch
from
pedro-gomes-92:pedrogomes/support-clean-subspace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+581
−259
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
976d3b5
feat: start clean
pedro-gomes-92 dfc5415
feat: support clean subspace
pedro-gomes-92 92df3f8
feat: support clean subspace (II)
pedro-gomes-92 e4afdb3
feat: support clean subspace (III)
pedro-gomes-92 aeaa914
feat: support clean subspace (IV)
pedro-gomes-92 d06f2cc
fix: remove superset
pedro-gomes-92 77cbc44
feat: add changelog
pedro-gomes-92 219e04a
Update rush-plugins/rush-migrate-subspace-plugin/src/cli.ts
pedro-gomes-92 2ce2242
Update rush-plugins/rush-migrate-subspace-plugin/src/prompts/subspace.ts
pedro-gomes-92 38d22be
chore: improve mr after review
pedro-gomes-92 b372387
chore: improve mr after review (II)
pedro-gomes-92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...nges/rush-migrate-subspace-plugin/pedrogomes-support-clean-subspace_2024-12-20-04-18.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
285 changes: 265 additions & 20 deletions
285
rush-plugins/rush-migrate-subspace-plugin/src/cleanSubspace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,285 @@ | ||
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 { reverseSortVersions, subsetVersion } from './utilities/dependency'; | ||
import { | ||
getProjectPackageFilePath, | ||
loadProjectPackageJson, | ||
updateProjectDependency | ||
} from './utilities/project'; | ||
import { IRushConfigurationProjectJson } from '@rushstack/rush-sdk/lib/api/RushConfigurationProject'; | ||
import { RESERVED_VERSIONS } from './constants/versions'; | ||
|
||
const removeSupersetDependency = ( | ||
subspaceName: string, | ||
dependencyName: string, | ||
versionsMap: Map<string, string[]>, | ||
rootPath: string | ||
): number => { | ||
const versions: string[] = Array.from(versionsMap.keys()); | ||
const subspaceCommonVersionsPath: string = getRushSubspaceCommonVersionsFilePath(subspaceName, rootPath); | ||
const subspaceCommonVersionsJson: RushSubspaceCommonVersionsJson = loadRushSubspaceCommonVersions( | ||
subspaceName, | ||
rootPath | ||
); | ||
|
||
const newValidVersions: string[] = reverseSortVersions(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 | ||
for (const projectName of versionsMap.get(currVersion) || []) { | ||
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): void => { | ||
Console.log(`Removing duplicated dependencies for subspace ${Colorize.bold(subspaceName)}...`); | ||
|
||
const projects: IRushConfigurationProjectJson[] = queryProjectsFromSubspace(subspaceName, rootPath); | ||
let countRemoved: number = 0; | ||
|
||
for (const project of projects) { | ||
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 || {}); | ||
|
||
for (const devDependency of devDependencies) { | ||
if (dependencies.includes(devDependency)) { | ||
countRemoved += 1; | ||
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[]>>, | ||
rootPath: string | ||
): void => { | ||
Console.log(`Removing unused alternative versions for subspace ${Colorize.bold(subspaceName)}...`); | ||
|
||
const subspaceCommonVersionsPath: string = getRushSubspaceCommonVersionsFilePath(subspaceName, rootPath); | ||
const subspaceCommonVersionsJson: RushSubspaceCommonVersionsJson = loadRushSubspaceCommonVersions( | ||
subspaceName, | ||
rootPath | ||
); | ||
|
||
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; | ||
} | ||
|
||
export const cleanSubspace = async (): Promise<void> => { | ||
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[]>>, | ||
rootPath: 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)}...`); | ||
const countPerDependency: number[] = Array.from(subspaceDependencies.entries()).map( | ||
([dependency, versionsMap]) => removeSupersetDependency(subspaceName, dependency, versionsMap, rootPath) | ||
); | ||
|
||
const count: number = countPerDependency.reduce((a, b) => a + b, 0); | ||
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[]>, | ||
rootPath | ||
); | ||
|
||
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 (rootPath: string): Promise<void> => { | ||
Console.debug('Executing clean subspace command...'); | ||
|
||
const targetSubspaces: string[] = querySubspaces(); | ||
if (!isSubspaceSupported()) { | ||
const targetSubspaces: string[] = querySubspaces(rootPath); | ||
if (!isSubspaceSupported(rootPath)) { | ||
Console.error( | ||
`The monorepo ${Colorize.bold( | ||
getRootPath() | ||
)} doesn't support subspaces! Make sure you have ${Colorize.bold( | ||
getRushSubspacesConfigurationJsonPath() | ||
`The monorepo ${Colorize.bold(rootPath)} doesn't support subspaces! Make sure you have ${Colorize.bold( | ||
getRushSubspacesConfigurationJsonPath(rootPath) | ||
)} with the ${Colorize.bold(RushConstants.defaultSubspaceName)} subspace. Exiting...` | ||
); | ||
return; | ||
} | ||
|
||
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, | ||
rootPath | ||
); | ||
if (await scanForDuplicatedDependenciesPrompt()) { | ||
removeDuplicatedDependencies(targetSubspace, rootPath); | ||
subspaceDependencies = getSubspaceDependencies(targetSubspace, rootPath); | ||
} | ||
|
||
if (await scanForSupersetDependencyVersionsPrompt()) { | ||
await removeSupersetDependencyVersions(targetSubspace, subspaceDependencies, rootPath); | ||
subspaceDependencies = getSubspaceDependencies(targetSubspace, rootPath); | ||
} | ||
|
||
if (await scanForUnusedDependencyVersionsPrompt()) { | ||
removeUnusedAlternativeVersions(targetSubspace, subspaceDependencies, rootPath); | ||
} | ||
|
||
Console.warn( | ||
`Please run "rush update --subspace ${targetSubspace}" to update the subspace shrinkwrap file.` | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
rush-plugins/rush-migrate-subspace-plugin/src/constants/versions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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)?