-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: extract remove service This change extracts all business logic out of the remove command into its own service function. The cmd function is now only responsible for cli related logic. This change also slightly changes the behavior of the remove command. - Logs are rewritten - Information about removed packages is printed all at once at the end when the atomic remove process is complete. Previously messages about removed packages were printed as soon as the in-memory manifest was modified. The command could still fail however and then these logs would be confusing. * reformat files
- Loading branch information
1 parent
707d97e
commit 61ebed9
Showing
14 changed files
with
320 additions
and
243 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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,101 @@ | ||
import { AsyncResult, Err, Ok, Result } from "ts-results-es"; | ||
import { | ||
removeDependency, | ||
UnityProjectManifest, | ||
} from "../domain/project-manifest"; | ||
import { PackumentNotFoundError } from "../common-errors"; | ||
import { DomainName } from "../domain/domain-name"; | ||
import { | ||
LoadProjectManifest, | ||
ManifestLoadError, | ||
ManifestWriteError, | ||
WriteProjectManifest, | ||
} from "../io/project-manifest-io"; | ||
import { SemanticVersion } from "../domain/semantic-version"; | ||
import { PackageUrl } from "../domain/package-url"; | ||
|
||
export type RemovedPackage = { | ||
name: DomainName; | ||
version: SemanticVersion | PackageUrl; | ||
}; | ||
|
||
export type RemovePackagesError = | ||
| ManifestLoadError | ||
| PackumentNotFoundError | ||
| ManifestWriteError; | ||
|
||
export type RemovePackages = ( | ||
projectPath: string, | ||
packageNames: ReadonlyArray<DomainName> | ||
) => AsyncResult<ReadonlyArray<RemovedPackage>, RemovePackagesError>; | ||
|
||
export function makePackageRemover( | ||
loadProjectManifest: LoadProjectManifest, | ||
writeProjectManifest: WriteProjectManifest | ||
): RemovePackages { | ||
const tryRemoveSingle = function ( | ||
manifest: UnityProjectManifest, | ||
packageName: DomainName | ||
): Result<[UnityProjectManifest, RemovedPackage], PackumentNotFoundError> { | ||
// not found array | ||
const versionInManifest = manifest.dependencies[packageName]; | ||
if (versionInManifest === undefined) { | ||
return Err(new PackumentNotFoundError(packageName)); | ||
} | ||
|
||
manifest = removeDependency(manifest, packageName); | ||
|
||
manifest = { | ||
...manifest, | ||
scopedRegistries: manifest.scopedRegistries | ||
// Remove package scope from all scoped registries | ||
?.map((scopedRegistry) => ({ | ||
...scopedRegistry, | ||
scopes: scopedRegistry.scopes.filter( | ||
(scope) => scope !== packageName | ||
), | ||
})), | ||
}; | ||
|
||
return Ok([manifest, { name: packageName, version: versionInManifest }]); | ||
}; | ||
|
||
function tryRemoveAll( | ||
manifest: UnityProjectManifest, | ||
packageNames: ReadonlyArray<DomainName> | ||
): Result< | ||
[UnityProjectManifest, ReadonlyArray<RemovedPackage>], | ||
PackumentNotFoundError | ||
> { | ||
if (packageNames.length == 0) return Ok([manifest, []]); | ||
|
||
const currentPackageName = packageNames[0]!; | ||
const remainingPackageNames = packageNames.slice(1); | ||
|
||
return tryRemoveSingle(manifest, currentPackageName).andThen( | ||
([updatedManifest, removedPackage]) => | ||
tryRemoveAll(updatedManifest, remainingPackageNames).map( | ||
([finalManifest, removedPackages]) => [ | ||
finalManifest, | ||
[removedPackage, ...removedPackages], | ||
] | ||
) | ||
); | ||
} | ||
|
||
return (projectPath, packageNames) => { | ||
// load manifest | ||
const initialManifest = loadProjectManifest(projectPath); | ||
|
||
// remove | ||
const removeResult = initialManifest.andThen((it) => | ||
tryRemoveAll(it, packageNames) | ||
); | ||
|
||
return removeResult.andThen(([updatedManifest, removedPackages]) => | ||
writeProjectManifest(projectPath, updatedManifest).map( | ||
() => removedPackages | ||
) | ||
); | ||
}; | ||
} |
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
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
Oops, something went wrong.