-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin-release.js
251 lines (223 loc) · 11.3 KB
/
plugin-release.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*!
Copyright (C) 2020 Evolved Binary
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module.exports = {
name: `plugin-addition`,
factory: require => {
const {BaseCommand, WorkspaceRequiredError} = require("@yarnpkg/cli");
const {Configuration, Project} = require("@yarnpkg/core");
const {execute} = require("@yarnpkg/shell");
const {Command, Option} = require("clipanion");
const {access, copyFile, rm} = require("fs/promises");
const t = require("typanion");
const semver2Regex = "(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:\\-([1-9A-Za-z-][0-9A-Za-z-]*(?:\\.[1-9A-Za-z-][0-9A-Za-z-]*)*))?(?:\\+([1-9A-Za-z-][0-9A-Za-z-]*(?:\\.[1-9A-Za-z-][0-9A-Za-z-]*)*))?";
const yarnAutoVersionsRegex = "(major|minor|patch)";
class ReleaseCommand extends BaseCommand {
static paths = [[`release`]];
// Show descriptive usage for a --help argument passed to this command
static usage = Command.Usage({
description: `Publish a new release`,
details: `
This command will create a new release for the project, which involves the following steps:
1. Check project status - Requires no unstaged changes, and no un-pushed commits.
2. Pre-release testing - Executes the \`lint\` and \`test\` scripts.
3. Update the version numbers of all packages.
4. git commit the version update.
5. git tag and sign the release tag.
6. git push the updates - Version commit and tag.
7. Publish the packages to npm.js.
`,
examples: [
[
`Release the next major version`,
`yarn release major`,
],
[
`Release the next version as 2.1.0`,
`yarn release 2.1.0`,
]
],
});
version = Option.String({
validator: t.matchesRegExp(new RegExp("^" + yarnAutoVersionsRegex + "|(?:" + semver2Regex + ")$"))
});
// b = Option.String({validator: t.isNumber()});
async execute() {
const configuration = await Configuration.find(this.context.cwd, this.context.plugins);
const {project, workspace} = await Project.find(configuration, this.context.cwd);
if (!workspace) {
throw new WorkspaceRequiredError(project.cwd, this.context.cwd);
}
// process exit code for the last command
let exitCode = 0;
this.context.stdout.write(`Preparing to release version: ${this.version}...\n`);
const executeOptions = {"cwd": project.cwd};
// Step 1 - Check project status - requires no unstaged changes, and no un-pushed commits
// TODO(AR) figure out how to capture stdout from the git commands below and check the content
this.context.stdout.write("1. Checking project status...\n");
this.context.stdout.write("1.1. Checking for git 'main' branch...\n");
// await execute('git', ['branch', '--show-current'], executeOptions);
this.context.stdout.write("1.2. Checking for unstaged changes...\n");
// await execute('git', ['status', '--porcelain', '--untracked-files=no'], executeOptions);
this.context.stdout.write("1.3. Checking for un-pushed commits...\n");
// await execute('git', ['log', 'origin/main..HEAD'], [], executeOptions);
// Step 2 - Pre-release testing - Executes the `lint` and `test` scripts
this.context.stdout.write("2. Performing pre-release testing...\n");
exitCode = await this.cli.run(["clean"]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: 'yarn clean' failed with code: ${exitCode}\n`);
return;
}
this.context.stdout.write("2.1. Performing build...\n");
exitCode = await this.cli.run(["build"]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: 'yarn build' failed with code: ${exitCode}\n`);
return;
}
this.context.stdout.write("2.2. Performing lint...\n");
exitCode = await this.cli.run(["lint"]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: 'yarn lint' failed with code: ${exitCode}\n`);
return;
}
// this.context.stdout.write("2.3. Running tests...\n");
exitCode = await this.cli.run(["test"]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: 'yarn test' failed with code: ${exitCode}\n`);
return;
}
// Cleanup after Step 2 - Pre-release testing
exitCode = await this.cli.run(["clean"]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: 'yarn clean' failed with code: ${exitCode}\n`);
return;
}
// Step 3 - Increment the versions of all packages (including the project root)
let packageFiles = [];
this.context.stdout.write(`3. Running \`yarn version\` to bump versions of all workspaces to ${this.version}...\n`);
for (let i = 0; i < project.workspaces.length; i++) {
const projectWorkspace = project.workspaces[i];
const projectWorkspaceName = `@${projectWorkspace.manifest.name.scope}/${projectWorkspace.manifest.name.name}`;
this.context.stdout.write(`3.1 Running \`yarn version ${this.version}\` for workspace ${projectWorkspaceName}...\n`);
if (projectWorkspace.cwd == project.cwd) {
exitCode = await this.cli.run(["version", this.version]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: Incrementing project version failed with code: ${exitCode}\n`);
return;
}
} else {
exitCode = await this.cli.run(["workspace", projectWorkspaceName, "version", this.version]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: Incrementing workspace '` + projectWorkspaceName + `' version failed with code: ${exitCode}\n`);
return;
}
}
packageFiles.push(`${projectWorkspace.cwd}/package.json`);
}
// Step 4 - git commit the version update.
this.context.stdout.write("4. git Committing the version update...\n");
exitCode = await execute("git", ["commit", `--message=[release] Release version: ${this.version}`].concat(packageFiles), executeOptions);
if (exitCode !== 0) {
this.context.stderr.write(`Error: git commit failed with code: ${exitCode}\n`);
return;
} else {
this.context.stdout.write("git commit OK!\n");
}
// Step 5 - git tag and sign the release tag.
this.context.stdout.write("5. git Committing the version update...\n");
exitCode = await execute("git", ["tag", `--message=[release] Release version: ${this.version}`, "--sign", `v${this.version}`], executeOptions);
if (exitCode !== 0) {
this.context.stderr.write(`Error: git commit failed with code: ${exitCode}\n`);
return;
} else {
this.context.stdout.write("git tag OK!\n");
}
// Step 6 - git push the updates.
this.context.stdout.write("6. git push the version update...\n");
this.context.stdout.write("6.1. git pushing the branch...\n");
exitCode = await execute("git", ["push"], executeOptions);
if (exitCode !== 0) {
this.context.stderr.write(`Error: git push failed with code: ${exitCode}\n`)
return;
} else {
this.context.stdout.write("git push OK!\n");
}
this.context.stdout.write("6.2. git pushing the tag...\n");
exitCode = await execute("git", ["push", "--tags"], executeOptions);
if (exitCode !== 0) {
this.context.stderr.write("Error: git push tags failed with code: ${exitCode}\n");
return;
} else {
this.context.stdout.write("git push tags OK!\n");
}
// Step 7 - Publish the packages to npm.js.
this.context.stdout.write("7. Running `yarn npm login` to publish packages to npm.js...\n");
exitCode = await this.cli.run(["npm", "login"]);
if (exitCode !== 0) {
this.context.stderr.write("Error: npm login failed with code: ${exitCode}\n");
return;
} else {
this.context.stdout.write("npm login OK!\n");
}
// Copy any temporarily files we need in the dists
let tempCopiedFiles = [];
for (let i = 0; i < project.workspaces.length; i++) {
const projectWorkspace = project.workspaces[i];
if (projectWorkspace.cwd == project.cwd) {
this.context.stdout.write("NOTE: Skipping publish of project root workspace!\n")
} else {
const projectWorkspaceName = `@${projectWorkspace.manifest.name.scope}/${projectWorkspace.manifest.name.name}`;
const licenseFileExists = await access(`${projectWorkspace.cwd}/LICENSE`).then(_x => true).catch(_e => false);
if (!licenseFileExists) {
// Make a copy of the LICENSE file to the workspace so that it is published as part of the package
try {
await copyFile(`${project.cwd}/LICENSE`, `${projectWorkspace.cwd}/LICENSE`);
tempCopiedFiles.push(`${projectWorkspace.cwd}/LICENSE`);
} catch(error) {
this.context.stderr.write(`Error: Copying LICENSE to workspace '` + projectWorkspaceName+ `' failed: ${error}\n`);
return;
}
}
}
}
// Publish the workspace package
this.context.stdout.write("8. Running `yarn publish` to publish packages to npm.js...\n");
exitCode = await this.cli.run(["publish"]);
if (exitCode !== 0) {
this.context.stderr.write(`Error: npm publish failed with code: ${exitCode}\n`);
return;
} else {
this.context.stdout.write(`npm publish OK!\n`);
}
// Remove any temporarily copied files
for (let i = 0; i < tempCopiedFiles.length; i++) {
try {
await rm(tempCopiedFiles[i]);
} catch(error) {
this.context.stderr.write(`Error: Removing ${tempCopiedFiles[i]} from workspace failed: ${error}\n`);
return;
}
}
this.context.stdout.write(`Released version: ${this.version} OK!\n`);
}
async catch(error) {
throw error;
}
}
return {
commands: [
ReleaseCommand,
],
};
},
};