|
1 | 1 | import {BaseCommand, WorkspaceRequiredError} from '@yarnpkg/cli';
|
2 |
| -import {Configuration, Project, structUtils} from '@yarnpkg/core'; |
| 2 | +import {Configuration, Descriptor, DescriptorHash, Project, structUtils} from '@yarnpkg/core'; |
3 | 3 | import {npath, xfs, ppath, PortablePath, Filename} from '@yarnpkg/fslib';
|
4 | 4 | import {Command, Option, Usage, UsageError} from 'clipanion';
|
5 | 5 |
|
@@ -63,12 +63,54 @@ export default class PatchCommitCommand extends BaseCommand {
|
63 | 63 | await xfs.mkdirPromise(patchFolder, {recursive: true});
|
64 | 64 | await xfs.writeFilePromise(patchPath, diff);
|
65 | 65 |
|
66 |
| - const relPath = ppath.relative(project.cwd, patchPath); |
| 66 | + const transitiveDependencies = new Map<DescriptorHash, Descriptor>(); |
67 | 67 |
|
68 |
| - project.topLevelWorkspace.manifest.resolutions.push({ |
69 |
| - pattern: {descriptor: {fullName: structUtils.stringifyIdent(locator), description: meta.version}}, |
70 |
| - reference: `patch:${structUtils.stringifyLocator(locator)}#${relPath}`, |
71 |
| - }); |
| 68 | + for (const pkg of project.storedPackages.values()) { |
| 69 | + if (structUtils.isVirtualLocator(pkg)) |
| 70 | + continue; |
| 71 | + |
| 72 | + const descriptor = pkg.dependencies.get(locator.identHash); |
| 73 | + if (!descriptor) |
| 74 | + continue; |
| 75 | + |
| 76 | + const devirtualizedDescriptor = structUtils.isVirtualDescriptor(descriptor) |
| 77 | + ? structUtils.devirtualizeDescriptor(descriptor) |
| 78 | + : descriptor; |
| 79 | + |
| 80 | + const unpatchedDescriptor = patchUtils.ensureUnpatchedDescriptor(devirtualizedDescriptor); |
| 81 | + |
| 82 | + const resolution = project.storedResolutions.get(unpatchedDescriptor.descriptorHash); |
| 83 | + if (!resolution) |
| 84 | + throw new Error(`Assertion failed: Expected the resolution to have been registered`); |
| 85 | + |
| 86 | + const dependency = project.storedPackages.get(resolution); |
| 87 | + if (!dependency) |
| 88 | + throw new Error(`Assertion failed: Expected the package to have been registered`); |
| 89 | + |
| 90 | + const originalPkg = project.originalPackages.get(pkg.locatorHash); |
| 91 | + if (!originalPkg) |
| 92 | + throw new Error(`Assertion failed: Expected the original package to have been registered`); |
| 93 | + |
| 94 | + const originalDependency = originalPkg.dependencies.get(descriptor.identHash); |
| 95 | + if (!originalDependency) |
| 96 | + throw new Error(`Assertion failed: Expected the original dependency to have been registered`); |
| 97 | + |
| 98 | + transitiveDependencies.set(originalDependency.descriptorHash, originalDependency); |
| 99 | + } |
| 100 | + |
| 101 | + for (const originalDescriptor of transitiveDependencies.values()) { |
| 102 | + const newDescriptor = patchUtils.makeDescriptor(originalDescriptor, { |
| 103 | + parentLocator: null, |
| 104 | + sourceDescriptor: structUtils.convertLocatorToDescriptor(locator), |
| 105 | + sourceVersion: null, |
| 106 | + patchPaths: [`./${ppath.relative(project.cwd, patchPath)}` as PortablePath], |
| 107 | + }); |
| 108 | + |
| 109 | + project.topLevelWorkspace.manifest.resolutions.push({ |
| 110 | + pattern: {descriptor: {fullName: structUtils.stringifyIdent(newDescriptor), description: originalDescriptor.range}}, |
| 111 | + reference: newDescriptor.range, |
| 112 | + }); |
| 113 | + } |
72 | 114 |
|
73 | 115 | await project.persist();
|
74 | 116 | }
|
|
0 commit comments