|
| 1 | +module.exports = { |
| 2 | + name: `plugin-ci-version`, |
| 3 | + factory: (require) => { |
| 4 | + const { BaseCommand, WorkspaceRequiredError } = require(`@yarnpkg/cli`); |
| 5 | + const { Cache, Configuration, Project } = require(`@yarnpkg/core`); |
| 6 | + const semver = require("semver"); |
| 7 | + const { Option } = require(`clipanion`); |
| 8 | + |
| 9 | + class CiVersion extends BaseCommand { |
| 10 | + static paths = [[`ci-version`]]; |
| 11 | + |
| 12 | + strategy = Option.String(); |
| 13 | + |
| 14 | + async execute() { |
| 15 | + const configuration = await Configuration.find( |
| 16 | + this.context.cwd, |
| 17 | + this.context.plugins, |
| 18 | + ); |
| 19 | + const { project, workspace } = await Project.find( |
| 20 | + configuration, |
| 21 | + this.context.cwd, |
| 22 | + ); |
| 23 | + const cache = await Cache.find(configuration); |
| 24 | + |
| 25 | + if (!workspace) |
| 26 | + throw new WorkspaceRequiredError(project.cwd, this.context.cwd); |
| 27 | + |
| 28 | + const valid = semver.valid(this.strategy); |
| 29 | + if (!valid) |
| 30 | + throw new Error(`should be a valid semver: ${this.strategy}`); |
| 31 | + |
| 32 | + const currentVersion = workspace.manifest.version; |
| 33 | + if (currentVersion === this.strategy) { |
| 34 | + this.context.stdout.write( |
| 35 | + `workspace ${workspace.manifest.raw.name} already has specified version ${this.strategy}\n`, |
| 36 | + ); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + workspace.manifest.version = this.strategy; |
| 41 | + |
| 42 | + this.context.stdout.write( |
| 43 | + `updating workspace ${workspace.manifest.raw.name} from ${currentVersion} to ${this.strategy}\n`, |
| 44 | + ); |
| 45 | + |
| 46 | + return await project.installWithNewReport( |
| 47 | + { |
| 48 | + json: this.json, |
| 49 | + stdout: this.context.stdout, |
| 50 | + }, |
| 51 | + { |
| 52 | + cache, |
| 53 | + }, |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + commands: [CiVersion], |
| 60 | + }; |
| 61 | + }, |
| 62 | +}; |
0 commit comments