-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
ui.js
303 lines (258 loc) · 9.08 KB
/
ui.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import inquirer from 'inquirer';
import chalk from 'chalk';
import githubUrlFromGit from 'github-url-from-git';
import {htmlEscape} from 'escape-goat';
import isScoped from 'is-scoped';
import isInteractive from 'is-interactive';
import * as util from './util.js';
import * as git from './git-util.js';
import * as npm from './npm/util.js';
import Version from './version.js';
import prettyVersionDiff from './pretty-version-diff.js';
const printCommitLog = async (repoUrl, registryUrl, fromLatestTag, releaseBranch) => {
const revision = fromLatestTag ? await git.latestTagOrFirstCommit() : await git.previousTagOrFirstCommit();
if (!revision) {
throw new Error('The package has not been published yet.');
}
const log = await git.commitLogFromRevision(revision);
if (!log) {
return {
hasCommits: false,
hasUnreleasedCommits: false,
releaseNotes() {},
};
}
let hasUnreleasedCommits = false;
let commitRangeText = `${revision}...${releaseBranch}`;
let commits = log.split('\n')
.map(commit => {
const splitIndex = commit.lastIndexOf(' ');
return {
message: commit.slice(0, splitIndex),
id: commit.slice(splitIndex + 1),
};
});
if (!fromLatestTag) {
const latestTag = await git.latestTag();
// Version bump commit created by np, following the semver specification.
const versionBumpCommitName = latestTag.match(/v\d+\.\d+\.\d+/) && latestTag.slice(1); // Name v1.0.1 becomes 1.0.1
const versionBumpCommitIndex = commits.findIndex(commit => commit.message === versionBumpCommitName);
if (versionBumpCommitIndex > 0) {
commitRangeText = `${revision}...${latestTag}`;
hasUnreleasedCommits = true;
}
if (await git.isHeadDetached()) {
commitRangeText = `${revision}...${latestTag}`;
}
// Get rid of unreleased commits and of the version bump commit.
commits = commits.slice(versionBumpCommitIndex + 1);
}
const history = commits.map(commit => {
const commitMessage = util.linkifyIssues(repoUrl, commit.message);
const commitId = util.linkifyCommit(repoUrl, commit.id);
return `- ${commitMessage} ${commitId}`;
}).join('\n');
const releaseNotes = nextTag => commits.map(commit =>
`- ${htmlEscape(commit.message)} ${commit.id}`,
).join('\n') + `\n\n${repoUrl}/compare/${revision}...${nextTag}`;
const commitRange = util.linkifyCommitRange(repoUrl, commitRangeText);
console.log(`${chalk.bold('Commits:')}\n${history}\n\n${chalk.bold('Commit Range:')}\n${commitRange}\n\n${chalk.bold('Registry:')}\n${registryUrl}\n`);
return {
hasCommits: true,
hasUnreleasedCommits,
releaseNotes,
};
};
const checkNewFilesAndDependencies = async (pkg, rootDir) => {
const newFiles = await util.getNewFiles(rootDir);
const newDependencies = await util.getNewDependencies(pkg, rootDir);
const noNewUnpublishedFiles = !newFiles.unpublished || newFiles.unpublished.length === 0;
const noNewFirstTimeFiles = !newFiles.firstTime || newFiles.firstTime.length === 0;
const noNewFiles = noNewUnpublishedFiles && noNewFirstTimeFiles;
const noNewDependencies = !newDependencies || newDependencies.length === 0;
if (noNewFiles && noNewDependencies) {
return true;
}
const messages = [];
if (newFiles.unpublished.length > 0) {
messages.push(`The following new files will not be part of your published package:\n${util.joinList(newFiles.unpublished)}`);
}
if (newFiles.firstTime.length > 0) {
messages.push(`The following new files will be published for the first time:\n${util.joinList(newFiles.firstTime)}`);
}
if (newDependencies.length > 0) {
messages.push(`The following new dependencies will be part of your published package:\n${util.joinList(newDependencies)}`);
}
if (!isInteractive()) {
console.log(messages.join('\n'));
return true;
}
const answers = await inquirer.prompt([{
type: 'confirm',
name: 'confirm',
message: `${messages.join('\n')}\nContinue?`,
default: false,
}]);
return answers.confirm;
};
// eslint-disable-next-line complexity
const ui = async (options, {pkg, rootDir}) => {
const oldVersion = pkg.version;
const extraBaseUrls = ['gitlab.com'];
const repoUrl = pkg.repository && githubUrlFromGit(pkg.repository.url, {extraBaseUrls});
const pkgManager = options.yarn ? 'yarn' : 'npm';
const registryUrl = await npm.getRegistryUrl(pkgManager, pkg);
const releaseBranch = options.branch;
if (options.runPublish) {
npm.checkIgnoreStrategy(pkg, rootDir);
const answerIgnoredFiles = await checkNewFilesAndDependencies(pkg, rootDir);
if (!answerIgnoredFiles) {
return {
...options,
confirm: answerIgnoredFiles,
};
}
}
if (options.releaseDraftOnly) {
console.log(`\nCreate a release draft on GitHub for ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
} else {
const newVersion = options.version ? Version.getAndValidateNewVersionFrom(options.version, oldVersion) : undefined;
const versionText = chalk.dim(`(current: ${oldVersion}${newVersion ? `, next: ${prettyVersionDiff(oldVersion, newVersion)}` : ''}${chalk.dim(')')}`);
console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${versionText}\n`);
}
const useLatestTag = !options.releaseDraftOnly;
const {hasCommits, hasUnreleasedCommits, releaseNotes} = await printCommitLog(repoUrl, registryUrl, useLatestTag, releaseBranch);
if (hasUnreleasedCommits && options.releaseDraftOnly) {
const answers = await inquirer.prompt({
confirm: {
type: 'confirm',
message: 'Unreleased commits found. They won\'t be included in the release draft. Continue?',
default: false,
},
});
if (!answers.confirm) {
return {
...options,
...answers,
};
}
}
if (options.version) {
return {
...options,
confirm: true,
repoUrl,
releaseNotes,
};
}
if (!hasCommits) {
const answers = await inquirer.prompt({
confirm: {
type: 'confirm',
message: 'No commits found since previous release, continue?',
default: false,
},
});
if (!answers.confirm) {
return {
...options,
...answers,
};
}
}
if (options.availability.isUnknown) {
const answers = await inquirer.prompt({
confirm: {
type: 'confirm',
when: isScoped(pkg.name) && options.runPublish,
message: `Failed to check availability of scoped repo name ${chalk.bold.magenta(pkg.name)}. Do you want to try and publish it anyway?`,
default: false,
},
});
if (!answers.confirm) {
return {
...options,
...answers,
};
}
}
const answers = await inquirer.prompt({
version: {
type: 'list',
message: 'Select semver increment or specify new version',
pageSize: Version.SEMVER_INCREMENTS.length + 2,
choices: [...Version.SEMVER_INCREMENTS
.map(inc => ({
name: `${inc} ${prettyVersionDiff(oldVersion, inc)}`,
value: inc,
})),
new inquirer.Separator(),
{
name: 'Other (specify)',
value: null,
}],
filter: input => Version.isValidInput(input) ? new Version(oldVersion).getNewVersionFrom(input) : input,
},
customVersion: {
type: 'input',
message: 'Version',
when: answers => !answers.version,
filter: input => Version.isValidInput(input) ? new Version(pkg.version).getNewVersionFrom(input) : input,
validate(input) {
if (!Version.isValidInput(input)) {
return 'Please specify a valid semver, for example, `1.2.3`. See https://semver.org';
}
if (new Version(oldVersion).isLowerThanOrEqualTo(input)) {
return `Version must be greater than ${oldVersion}`;
}
return true;
},
},
tag: {
type: 'list',
message: 'How should this pre-release version be tagged in npm?',
when: answers => options.runPublish && (Version.isPrereleaseOrIncrement(answers.customVersion) || Version.isPrereleaseOrIncrement(answers.version)) && !options.tag,
async choices() {
const existingPrereleaseTags = await npm.prereleaseTags(pkg.name);
return [
...existingPrereleaseTags,
new inquirer.Separator(),
{
name: 'Other (specify)',
value: null,
},
];
},
},
customTag: {
type: 'input',
message: 'Tag',
when: answers => options.runPublish && (Version.isPrereleaseOrIncrement(answers.customVersion) || Version.isPrereleaseOrIncrement(answers.version)) && !options.tag && !answers.tag,
validate(input) {
if (input.length === 0) {
return 'Please specify a tag, for example, `next`.';
}
if (input.toLowerCase() === 'latest') {
return 'It\'s not possible to publish pre-releases under the `latest` tag. Please specify something else, for example, `next`.';
}
return true;
},
},
publishScoped: {
type: 'confirm',
when: isScoped(pkg.name) && options.availability.isAvailable && !options.availability.isUnknown && options.runPublish && (pkg.publishConfig && pkg.publishConfig.access !== 'restricted') && !npm.isExternalRegistry(pkg),
message: `This scoped repo ${chalk.bold.magenta(pkg.name)} hasn't been published. Do you want to publish it publicly?`,
default: false,
},
});
return {
...options,
version: answers.version || answers.customVersion || options.version,
tag: answers.tag || answers.customTag || options.tag,
publishScoped: answers.publishScoped,
confirm: true,
repoUrl,
releaseNotes,
};
};
export default ui;