-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
ui.js
173 lines (153 loc) · 4.92 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
'use strict';
const execa = require('execa');
const inquirer = require('inquirer');
const chalk = require('chalk');
const githubUrlFromGit = require('github-url-from-git');
const util = require('./util');
const version = require('./version');
function prettyVersionDiff(oldVersion, inc) {
const newVersion = version.getNewVersion(oldVersion, inc).split('.');
oldVersion = oldVersion.split('.');
let firstVersionChange = false;
const output = [];
for (let i = 0; i < newVersion.length; i++) {
if ((newVersion[i] !== oldVersion[i] && !firstVersionChange)) {
output.push(`${chalk.dim.cyan(newVersion[i])}`);
firstVersionChange = true;
} else if (newVersion[i].indexOf('-') >= 1) {
let preVersion = [];
preVersion = newVersion[i].split('-');
output.push(`${chalk.dim.cyan(`${preVersion[0]}-${preVersion[1]}`)}`);
} else {
output.push(chalk.reset.dim(newVersion[i]));
}
}
return output.join(chalk.reset.dim('.'));
}
function printCommitLog(repositoryUrl) {
return execa.stdout('git', ['rev-list', '--tags', '--max-count=1'])
.then(latestHash => execa.stdout('git', ['log', '--format=%s %h', `${latestHash}..HEAD`]))
.then(result => {
if (!result) {
return false;
}
const history = result.split('\n')
.map(commit => {
const splitIndex = commit.lastIndexOf(' ');
const commitMessage = util.linkifyIssues(repositoryUrl, commit.substring(0, splitIndex));
const commitId = util.linkifyCommit(repositoryUrl, commit.substring(splitIndex + 1));
return `- ${commitMessage} ${commitId}`;
})
.join('\n');
console.log(`${chalk.bold('Commits:')}\n${history}\n`);
return true;
});
}
module.exports = options => {
const pkg = util.readPkg();
const oldVersion = pkg.version;
const extraBaseUrls = ['gitlab.com'];
const repositoryUrl = pkg.repository && githubUrlFromGit(pkg.repository.url, {extraBaseUrls});
console.log(`\nPublish a new version of ${chalk.bold.magenta(pkg.name)} ${chalk.dim(`(current: ${oldVersion})`)}\n`);
const prompts = [
{
type: 'list',
name: 'version',
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
}))
.concat([
new inquirer.Separator(),
{
name: 'Other (specify)',
value: null
}
]),
filter: input => version.isValidVersionInput(input) ? version.getNewVersion(oldVersion, input) : input
},
{
type: 'input',
name: 'version',
message: 'Version',
when: answers => !answers.version,
filter: input => version.isValidVersionInput(input) ? version.getNewVersion(pkg.version, input) : input,
validate: input => {
if (!version.isValidVersionInput(input)) {
return 'Please specify a valid semver, for example, `1.2.3`. See http://semver.org';
}
if (!version.isVersionGreater(oldVersion, input)) {
return `Version must be greater than ${oldVersion}`;
}
return true;
}
},
{
type: 'list',
name: 'tag',
message: 'How should this pre-release version be tagged in npm?',
when: answers => !pkg.private && version.isPrereleaseVersion(answers.version) && !options.tag,
choices: () => execa.stdout('npm', ['view', '--json', pkg.name, 'dist-tags'])
.then(stdout => {
const existingPrereleaseTags = Object.keys(JSON.parse(stdout))
.filter(tag => tag !== 'latest');
if (existingPrereleaseTags.length === 0) {
existingPrereleaseTags.push('next');
}
return existingPrereleaseTags
.concat([
new inquirer.Separator(),
{
name: 'Other (specify)',
value: null
}
]);
})
},
{
type: 'input',
name: 'tag',
message: 'Tag',
when: answers => !pkg.private && version.isPrereleaseVersion(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;
}
},
{
type: 'confirm',
name: 'confirm',
message: answers => {
const tag = answers.tag || options.tag;
const tagPart = tag ? ` and tag this release in npm as ${tag}` : '';
return `Will bump from ${chalk.cyan(oldVersion)} to ${chalk.cyan(answers.version + tagPart)}. Continue?`;
}
}
];
return printCommitLog(repositoryUrl)
.then(hasCommits => {
if (!hasCommits) {
return inquirer.prompt([{
type: 'confirm',
name: 'confirm',
message: 'No commits found since previous release, continue?',
default: false
}]);
}
})
.then(answers => {
if (answers && !answers.confirm) {
return answers;
}
return inquirer.prompt(prompts);
})
.then(answers => Object.assign({}, options, answers));
};