|
| 1 | +// From https://github.com/yarnpkg/berry/blob/master/packages/plugin-version/sources/commands/version/apply.ts in accordance with the BSD license, see Notice.txt |
| 2 | + |
| 3 | +const Decision = { |
| 4 | + UNDECIDED: 'undecided', |
| 5 | + DECLINE: 'decline', |
| 6 | + MAJOR: 'major', |
| 7 | + MINOR: 'minor', |
| 8 | + PATCH: 'patch', |
| 9 | + PRERELEASE: 'prerelease' |
| 10 | +}; |
| 11 | + |
| 12 | +module.exports = { |
| 13 | + name: `plugin-nightly-prep`, |
| 14 | + factory: require => { |
| 15 | + const {PortablePath, npath, ppath, xfs} = require('@yarnpkg/fslib'); |
| 16 | + const {BaseCommand} = require(`@yarnpkg/cli`); |
| 17 | + const {Project, Configuration, Cache, StreamReport, structUtils, Manifest, miscUtils, MessageName, WorkspaceResolver} = require(`@yarnpkg/core`); |
| 18 | + const {Command, Option} = require(`clipanion`); |
| 19 | + const {parseSyml, stringifySyml} = require(`@yarnpkg/parsers`); |
| 20 | + |
| 21 | + class NightlyPrepCommand extends BaseCommand { |
| 22 | + static paths = [[`apply-nightly`]]; |
| 23 | + |
| 24 | + // Show descriptive usage for a --help argument passed to this command |
| 25 | + static usage = Command.Usage({ |
| 26 | + description: `apply nightly version bumps`, |
| 27 | + details: ` |
| 28 | + This command will update all references in every workspace package json to point to the exact nightly version, no range. |
| 29 | + `, |
| 30 | + examples: [[ |
| 31 | + `yarn apply-nightly`, |
| 32 | + ]], |
| 33 | + }); |
| 34 | + |
| 35 | + all = Option.Boolean(`--all`, false, { |
| 36 | + description: `Apply the deferred version changes on all workspaces`, |
| 37 | + }); |
| 38 | + |
| 39 | + async execute() { |
| 40 | + const configuration = await Configuration.find(this.context.cwd, this.context.plugins); |
| 41 | + const {project, workspace} = await Project.find(configuration, this.context.cwd); |
| 42 | + const cache = await Cache.find(configuration); |
| 43 | + |
| 44 | + const applyReport = await StreamReport.start({ |
| 45 | + configuration, |
| 46 | + json: this.json, |
| 47 | + stdout: this.context.stdout, |
| 48 | + }, async report => { |
| 49 | + const prerelease = this.prerelease |
| 50 | + ? typeof this.prerelease !== `boolean` ? this.prerelease : `rc.%n` |
| 51 | + : null; |
| 52 | + |
| 53 | + const allReleases = await resolveVersionFiles(project, xfs, ppath, parseSyml, structUtils, {prerelease}); |
| 54 | + let filteredReleases = new Map(); |
| 55 | + |
| 56 | + if (this.all) { |
| 57 | + filteredReleases = allReleases; |
| 58 | + } else { |
| 59 | + const relevantWorkspaces = this.recursive |
| 60 | + ? workspace.getRecursiveWorkspaceDependencies() |
| 61 | + : [workspace]; |
| 62 | + |
| 63 | + for (const child of relevantWorkspaces) { |
| 64 | + const release = allReleases.get(child); |
| 65 | + if (typeof release !== `undefined`) { |
| 66 | + filteredReleases.set(child, release); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (filteredReleases.size === 0) { |
| 72 | + const protip = allReleases.size > 0 |
| 73 | + ? ` Did you want to add --all?` |
| 74 | + : ``; |
| 75 | + |
| 76 | + report.reportWarning(MessageName.UNNAMED, `The current workspace doesn't seem to require a version bump.${protip}`); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + applyReleases(project, filteredReleases, Manifest, miscUtils, structUtils, MessageName, npath, WorkspaceResolver, {report}); |
| 81 | + |
| 82 | + if (!this.dryRun) { |
| 83 | + if (!prerelease) { |
| 84 | + if (this.all) { |
| 85 | + await clearVersionFiles(project, xfs); |
| 86 | + } else { |
| 87 | + await updateVersionFiles(project, [...filteredReleases.keys()], xfs, parseSyml, stringifySyml, structUtils); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + report.reportSeparator(); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + if (this.dryRun || applyReport.hasErrors()) |
| 96 | + return applyReport.exitCode(); |
| 97 | + |
| 98 | + return await project.installWithNewReport({ |
| 99 | + json: this.json, |
| 100 | + stdout: this.context.stdout, |
| 101 | + }, { |
| 102 | + cache, |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return { |
| 108 | + commands: [ |
| 109 | + NightlyPrepCommand, |
| 110 | + ], |
| 111 | + }; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +async function resolveVersionFiles(project, xfs, ppath, parseSyml, structUtils, miscUtils, {prerelease = null} = {}) { |
| 116 | + let candidateReleases = new Map(); |
| 117 | + |
| 118 | + const deferredVersionFolder = project.configuration.get(`deferredVersionFolder`); |
| 119 | + if (!xfs.existsSync(deferredVersionFolder)) |
| 120 | + return candidateReleases; |
| 121 | + |
| 122 | + const deferredVersionFiles = await xfs.readdirPromise(deferredVersionFolder); |
| 123 | + |
| 124 | + for (const entry of deferredVersionFiles) { |
| 125 | + if (!entry.endsWith(`.yml`)) |
| 126 | + continue; |
| 127 | + |
| 128 | + const versionPath = ppath.join(deferredVersionFolder, entry); |
| 129 | + const versionContent = await xfs.readFilePromise(versionPath, `utf8`); |
| 130 | + const versionData = parseSyml(versionContent); |
| 131 | + |
| 132 | + |
| 133 | + for (const [identStr, decision] of Object.entries(versionData.releases || {})) { |
| 134 | + if (decision === Decision.DECLINE) |
| 135 | + continue; |
| 136 | + |
| 137 | + const ident = structUtils.parseIdent(identStr); |
| 138 | + |
| 139 | + const workspace = project.tryWorkspaceByIdent(ident); |
| 140 | + if (workspace === null) |
| 141 | + throw new Error(`Assertion failed: Expected a release definition file to only reference existing workspaces (${ppath.basename(versionPath)} references ${identStr})`); |
| 142 | + |
| 143 | + if (workspace.manifest.version === null) |
| 144 | + throw new Error(`Assertion failed: Expected the workspace to have a version (${structUtils.prettyLocator(project.configuration, workspace.anchoredLocator)})`); |
| 145 | + |
| 146 | + // If there's a `stableVersion` field, then we assume that `version` |
| 147 | + // contains a prerelease version and that we need to base the version |
| 148 | + // bump relative to the latest stable instead. |
| 149 | + const baseVersion = workspace.manifest.raw.stableVersion ?? workspace.manifest.version; |
| 150 | + const suggestedRelease = applyStrategy(baseVersion, validateReleaseDecision(decision, miscUtils), miscUtils); |
| 151 | + |
| 152 | + if (suggestedRelease === null) |
| 153 | + throw new Error(`Assertion failed: Expected ${baseVersion} to support being bumped via strategy ${decision}`); |
| 154 | + |
| 155 | + const bestRelease = suggestedRelease; |
| 156 | + |
| 157 | + candidateReleases.set(workspace, bestRelease); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if (prerelease) { |
| 162 | + candidateReleases = new Map([...candidateReleases].map(([workspace, release]) => { |
| 163 | + return [workspace, applyPrerelease(release, {current: workspace.manifest.version, prerelease})]; |
| 164 | + })); |
| 165 | + } |
| 166 | + |
| 167 | + return candidateReleases; |
| 168 | +} |
| 169 | + |
| 170 | +function applyReleases(project, newVersions, Manifest, miscUtils, structUtils, MessageName, npath, WorkspaceResolver, {report}) { |
| 171 | + const allDependents = new Map(); |
| 172 | + |
| 173 | + // First we compute the reverse map to figure out which workspace is |
| 174 | + // depended upon by which other. |
| 175 | + // |
| 176 | + // Note that we need to do this before applying the new versions, |
| 177 | + // otherwise the `findWorkspacesByDescriptor` calls won't be able to |
| 178 | + // resolve the workspaces anymore (because the workspace versions will |
| 179 | + // have changed and won't match the outdated dependencies). |
| 180 | + |
| 181 | + for (const dependent of project.workspaces) { |
| 182 | + for (const set of Manifest.allDependencies) { |
| 183 | + for (const descriptor of dependent.manifest[set].values()) { |
| 184 | + const workspace = project.tryWorkspaceByDescriptor(descriptor); |
| 185 | + if (workspace === null) |
| 186 | + continue; |
| 187 | + |
| 188 | + // We only care about workspaces that depend on a workspace that will |
| 189 | + // receive a fresh update |
| 190 | + if (!newVersions.has(workspace)) |
| 191 | + continue; |
| 192 | + |
| 193 | + const dependents = miscUtils.getArrayWithDefault(allDependents, workspace); |
| 194 | + dependents.push([dependent, set, descriptor.identHash]); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // Now that we know which workspaces depend on which others, we can |
| 200 | + // proceed to update everything at once using our accumulated knowledge. |
| 201 | + |
| 202 | + for (const [workspace, newVersion] of newVersions) { |
| 203 | + const oldVersion = workspace.manifest.version; |
| 204 | + workspace.manifest.version = newVersion; |
| 205 | + |
| 206 | + const identString = workspace.manifest.name !== null |
| 207 | + ? structUtils.stringifyIdent(workspace.manifest.name) |
| 208 | + : null; |
| 209 | + |
| 210 | + report.reportInfo(MessageName.UNNAMED, `${structUtils.prettyLocator(project.configuration, workspace.anchoredLocator)}: Bumped to ${newVersion}`); |
| 211 | + report.reportJson({cwd: npath.fromPortablePath(workspace.cwd), ident: identString, oldVersion, newVersion}); |
| 212 | + |
| 213 | + const dependents = allDependents.get(workspace); |
| 214 | + if (typeof dependents === `undefined`) |
| 215 | + continue; |
| 216 | + |
| 217 | + for (const [dependent, set, identHash] of dependents) { |
| 218 | + const descriptor = dependent.manifest[set].get(identHash); |
| 219 | + if (typeof descriptor === `undefined`) |
| 220 | + throw new Error(`Assertion failed: The dependency should have existed`); |
| 221 | + |
| 222 | + let range = descriptor.range; |
| 223 | + let useWorkspaceProtocol = false; |
| 224 | + |
| 225 | + if (range.startsWith(WorkspaceResolver.protocol)) { |
| 226 | + range = range.slice(WorkspaceResolver.protocol.length); |
| 227 | + useWorkspaceProtocol = true; |
| 228 | + |
| 229 | + // Workspaces referenced through their path never get upgraded ("workspace:packages/yarnpkg-core") |
| 230 | + if (range === workspace.relativeCwd) { |
| 231 | + continue; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + let newRange = `${newVersion}`; |
| 236 | + if (useWorkspaceProtocol) |
| 237 | + newRange = `${WorkspaceResolver.protocol}${newRange}`; |
| 238 | + |
| 239 | + const newDescriptor = structUtils.makeDescriptor(descriptor, newRange); |
| 240 | + dependent.manifest[set].set(identHash, newDescriptor); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +async function clearVersionFiles(project, xfs) { |
| 246 | + const deferredVersionFolder = project.configuration.get(`deferredVersionFolder`); |
| 247 | + if (!xfs.existsSync(deferredVersionFolder)) |
| 248 | + return; |
| 249 | + |
| 250 | + await xfs.removePromise(deferredVersionFolder); |
| 251 | +} |
| 252 | + |
| 253 | +async function updateVersionFiles(project, workspaces, xfs, parseSyml, stringifySyml, structUtils) { |
| 254 | + const workspaceSet = new Set(workspaces); |
| 255 | + |
| 256 | + const deferredVersionFolder = project.configuration.get(`deferredVersionFolder`); |
| 257 | + if (!xfs.existsSync(deferredVersionFolder)) |
| 258 | + return; |
| 259 | + |
| 260 | + const deferredVersionFiles = await xfs.readdirPromise(deferredVersionFolder); |
| 261 | + |
| 262 | + for (const entry of deferredVersionFiles) { |
| 263 | + if (!entry.endsWith(`.yml`)) |
| 264 | + continue; |
| 265 | + |
| 266 | + const versionPath = ppath.join(deferredVersionFolder, entry); |
| 267 | + const versionContent = await xfs.readFilePromise(versionPath, `utf8`); |
| 268 | + const versionData = parseSyml(versionContent); |
| 269 | + |
| 270 | + const releases = versionData?.releases; |
| 271 | + if (!releases) |
| 272 | + continue; |
| 273 | + |
| 274 | + for (const locatorStr of Object.keys(releases)) { |
| 275 | + const ident = structUtils.parseIdent(locatorStr); |
| 276 | + const workspace = project.tryWorkspaceByIdent(ident); |
| 277 | + |
| 278 | + if (workspace === null || workspaceSet.has(workspace)) { |
| 279 | + delete versionData.releases[locatorStr]; |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + if (Object.keys(versionData.releases).length > 0) { |
| 284 | + await xfs.changeFilePromise(versionPath, stringifySyml( |
| 285 | + new stringifySyml.PreserveOrdering( |
| 286 | + versionData, |
| 287 | + ), |
| 288 | + )); |
| 289 | + } else { |
| 290 | + await xfs.unlinkPromise(versionPath); |
| 291 | + } |
| 292 | + } |
| 293 | +} |
| 294 | + |
| 295 | +function applyStrategy(version, strategy) { |
| 296 | + return strategy; |
| 297 | +} |
| 298 | + |
| 299 | +function validateReleaseDecision(decision, miscUtils) { |
| 300 | + return decision; |
| 301 | +} |
0 commit comments