Skip to content

Commit 3d7969f

Browse files
Added a "custom..." option when --prompt is used, which allows the user to manually enter a version number
1 parent f69a3b1 commit 3d7969f

2 files changed

Lines changed: 125 additions & 86 deletions

File tree

bin/bump.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const program = require('commander');
5+
const SemVer = require('semver');
56
const inquirer = require('inquirer');
67
const chalk = require('chalk');
78
const api = require('../');
@@ -108,29 +109,45 @@ function bumpManifests (manifests, options) {
108109
*/
109110
function bumpManifest (manifest, defaultBumpType, options) {
110111
return new Promise((resolve, reject) => {
112+
api.runNpmScriptIfExists(manifest, 'preversion');
113+
111114
if (options.prompt) {
112115
// Prompt the user for the type of bump to perform
113116
let version = api.versionInfo(manifest, options);
114117
console.log('\nCurrent version in %s is %s', manifest, version.current);
115118

116-
inquirer.prompt({
117-
type: 'list',
118-
name: 'bump',
119-
message: 'How would you like to bump it?',
120-
default: defaultBumpType,
121-
choices: [
122-
{ value: 'major', name: 'major (' + version.nextMajor + ')' },
123-
{ value: 'minor', name: 'minor (' + version.nextMinor + ')' },
124-
{ value: 'patch', name: 'patch (' + version.nextPatch + ')' },
125-
{ value: 'premajor', name: 'pre-release major (' + version.nextPreMajor + ')' },
126-
{ value: 'preminor', name: 'pre-release minor (' + version.nextPreMinor + ')' },
127-
{ value: 'prepatch', name: 'pre-relase patch (' + version.nextPrePatch + ')' },
128-
{ value: 'prerelease', name: 'pre-release (' + version.nextPreRelease + ')' }
129-
]
130-
})
131-
.then((answer) => {
132-
api.runNpmScriptIfExists(manifest, 'preversion');
133-
bump(answer.bump);
119+
inquirer.prompt([
120+
{
121+
type: 'list',
122+
name: 'bumpType',
123+
message: 'How would you like to bump it?',
124+
default: defaultBumpType,
125+
choices: [
126+
{ value: 'major', name: 'major (' + version.nextMajor + ')' },
127+
{ value: 'minor', name: 'minor (' + version.nextMinor + ')' },
128+
{ value: 'patch', name: 'patch (' + version.nextPatch + ')' },
129+
{ value: 'premajor', name: 'pre-release major (' + version.nextPreMajor + ')' },
130+
{ value: 'preminor', name: 'pre-release minor (' + version.nextPreMinor + ')' },
131+
{ value: 'prepatch', name: 'pre-relase patch (' + version.nextPrePatch + ')' },
132+
{ value: 'prerelease', name: 'pre-release (' + version.nextPreRelease + ')' },
133+
new inquirer.Separator(),
134+
{ value: 'custom', name: 'custom...' },
135+
]
136+
},
137+
{
138+
type: 'input',
139+
name: 'newVersion',
140+
message: 'Enter the new version number:',
141+
default: version.current,
142+
when: answers => answers.bumpType === 'custom',
143+
filter: SemVer.clean,
144+
validate: answer => {
145+
return SemVer.valid(answer) ? true : "That's not a valid version number";
146+
},
147+
}
148+
])
149+
.then((answers) => {
150+
bump(answers.bumpType, answers.newVersion);
134151
});
135152
}
136153
else {
@@ -143,12 +160,13 @@ function bumpManifest (manifest, defaultBumpType, options) {
143160
: options.prepatch ? 'prepatch'
144161
: options.prerelease ? 'prerelease'
145162
: defaultBumpType;
146-
api.runNpmScriptIfExists(manifest, 'preversion');
163+
147164
bump(bumpType);
148165
}
149166

150-
function bump (bumpType) {
167+
function bump (bumpType, newVersion) {
151168
try {
169+
options.newVersion = newVersion;
152170
api.bump(manifest, bumpType, options);
153171
}
154172
catch (ex) {

lib/index.js

Lines changed: 87 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,27 @@ function bump (manifest, type, options) {
7878
let pkgPath = path.join(cwd, manifest);
7979
const pkg = require(pkgPath);
8080

81-
// Increment the version number
8281
oldVersion = pkg.version || '0.0.0';
83-
let current = new SemVer(oldVersion);
84-
current.inc(type, options.preid || current.prerelease[0] || 'beta');
85-
newVersion = pkg.version = current.version;
8682

87-
// Save the file
88-
let usedIndent = indent(fs.readFileSync(pkgPath, 'utf8')).indent || ' ';
83+
if (type === 'custom') {
84+
newVersion = options.newVersion;
85+
}
86+
else {
87+
// Increment the version number
88+
let current = new SemVer(oldVersion);
89+
current.inc(type, options.preid || current.prerelease[0] || 'beta');
90+
newVersion = current.version;
91+
}
92+
93+
if (newVersion !== oldVersion) {
94+
// Save the file
95+
let usedIndent = indent(fs.readFileSync(pkgPath, 'utf8')).indent || ' ';
8996

90-
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, usedIndent));
97+
pkg.version = newVersion;
98+
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, usedIndent));
9199

92-
console.log('%s Updated %s to %s', logSymbols.success, manifest, current.version);
100+
console.log('%s Updated %s to %s', logSymbols.success, manifest, newVersion);
101+
}
93102
}
94103

95104
/**
@@ -121,40 +130,46 @@ function runNpmScriptIfExists (manifest, script) {
121130
* @param {object} options - CLI options
122131
*/
123132
function grep (manifests, options) {
124-
if (options.grep) {
125-
// Separate the glob patterns into two lists: included and excluded
126-
let included = [], excluded = [];
127-
options.grep.forEach((pattern) => {
128-
if (pattern[0] === '!') {
129-
excluded.push(pattern);
130-
}
131-
else {
132-
included.push(pattern);
133-
}
134-
});
133+
if (newVersion === oldVersion) {
134+
return;
135+
}
135136

136-
// Process each glob pattern
137-
included.forEach((pattern) => {
138-
let files = glob.sync(pattern, { nodir: true, ignore: excluded });
139-
140-
// Process each matched file
141-
files.forEach((file) => {
142-
// Read the file
143-
let fileContents = fs.readFileSync(file, { encoding: 'utf8' });
144-
145-
// Replace the old version number with the new version number
146-
let oldVersionPattern = new RegExp(oldVersion.replace(/\./g, '\\.'), 'g');
147-
let newFileContents = fileContents.replace(oldVersionPattern, newVersion);
148-
149-
// Only save the file if there were changes
150-
if (newFileContents !== fileContents) {
151-
fs.writeFileSync(file, newFileContents);
152-
manifests.push(file);
153-
console.log('%s Updated %s to %s', logSymbols.success, file, newVersion);
154-
}
155-
});
156-
});
137+
if (!options.grep) {
138+
return;
157139
}
140+
141+
// Separate the glob patterns into two lists: included and excluded
142+
let included = [], excluded = [];
143+
options.grep.forEach((pattern) => {
144+
if (pattern[0] === '!') {
145+
excluded.push(pattern);
146+
}
147+
else {
148+
included.push(pattern);
149+
}
150+
});
151+
152+
// Process each glob pattern
153+
included.forEach((pattern) => {
154+
let files = glob.sync(pattern, { nodir: true, ignore: excluded });
155+
156+
// Process each matched file
157+
files.forEach((file) => {
158+
// Read the file
159+
let fileContents = fs.readFileSync(file, { encoding: 'utf8' });
160+
161+
// Replace the old version number with the new version number
162+
let oldVersionPattern = new RegExp(oldVersion.replace(/\./g, '\\.'), 'g');
163+
let newFileContents = fileContents.replace(oldVersionPattern, newVersion);
164+
165+
// Only save the file if there were changes
166+
if (newFileContents !== fileContents) {
167+
fs.writeFileSync(file, newFileContents);
168+
manifests.push(file);
169+
console.log('%s Updated %s to %s', logSymbols.success, file, newVersion);
170+
}
171+
});
172+
});
158173
}
159174

160175
/**
@@ -164,34 +179,40 @@ function grep (manifests, options) {
164179
* @param {object} options - CLI options
165180
*/
166181
function git (manifests, options) {
167-
if (options.commit || options.tag || options.push) {
168-
// Git Commit
169-
let commitArgs = ['commit'];
170-
commitArgs = commitArgs.concat(options.all ? '-a' : manifests);
171-
let commitMessage = 'release v' + newVersion;
172-
if (options.commitMessage) {
173-
commitMessage = 'v' + newVersion + ' ' + options.commitMessage;
174-
}
175-
commitArgs = commitArgs.concat(['-m', commitMessage]);
176-
exec('git', commitArgs);
177-
console.log(logSymbols.success, 'Git commit');
178-
179-
// Git Tag
180-
if (options.tag) {
181-
exec('git', ['tag', '-a', 'v' + newVersion, '-m', newVersion]);
182-
console.log(logSymbols.success, 'Git tag');
183-
}
182+
if (newVersion === oldVersion) {
183+
return;
184+
}
184185

185-
manifests.forEach((manifest) => {
186-
runNpmScriptIfExists(manifest, 'postversion');
187-
});
186+
if (!(options.commit || options.tag || options.push)) {
187+
return;
188+
}
188189

189-
// Git Push
190-
if (options.push) {
191-
exec('git', ['push']);
192-
options.tag && exec('git', ['push', '--tags']);
193-
console.log(logSymbols.success, 'Git push');
194-
}
190+
// Git Commit
191+
let commitArgs = ['commit'];
192+
commitArgs = commitArgs.concat(options.all ? '-a' : manifests);
193+
let commitMessage = 'release v' + newVersion;
194+
if (options.commitMessage) {
195+
commitMessage = 'v' + newVersion + ' ' + options.commitMessage;
196+
}
197+
commitArgs = commitArgs.concat(['-m', commitMessage]);
198+
exec('git', commitArgs);
199+
console.log(logSymbols.success, 'Git commit');
200+
201+
// Git Tag
202+
if (options.tag) {
203+
exec('git', ['tag', '-a', 'v' + newVersion, '-m', newVersion]);
204+
console.log(logSymbols.success, 'Git tag');
205+
}
206+
207+
manifests.forEach((manifest) => {
208+
runNpmScriptIfExists(manifest, 'postversion');
209+
});
210+
211+
// Git Push
212+
if (options.push) {
213+
exec('git', ['push']);
214+
options.tag && exec('git', ['push', '--tags']);
215+
console.log(logSymbols.success, 'Git push');
195216
}
196217
}
197218

0 commit comments

Comments
 (0)