Skip to content

Commit 1f7defc

Browse files
updated to ES6 syntax
1 parent c88e015 commit 1f7defc

2 files changed

Lines changed: 95 additions & 93 deletions

File tree

bin/bump.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
var program = require('commander'),
5-
inquirer = require('inquirer'),
6-
chalk = require('chalk'),
7-
api = require('../');
4+
const program = require('commander');
5+
const inquirer = require('inquirer');
6+
const chalk = require('chalk');
7+
const api = require('../');
88

99
program
1010
.version(require('../package').version)
@@ -22,7 +22,7 @@ program
2222
.option('--push', 'Push the Git commit')
2323
.option('--all', 'Commit/tag/push ALL pending files, not just the ones changed by bump')
2424
.option('--grep <filespec...>', 'Files and/or globs to do a text-replace of the old version number with the new one')
25-
.on('--help', function() {
25+
.on('--help', () => {
2626
console.log(
2727
' Examples:\n' +
2828
'\n' +
@@ -39,7 +39,7 @@ if (program.rawArgs.length < 3) {
3939
program.help();
4040
}
4141
else {
42-
var options = program;
42+
let options = program;
4343

4444
if (options.grep && program.args) {
4545
// If multiple --grep files are specified, then they are parsed as separate args
@@ -51,18 +51,18 @@ else {
5151
options.commit = true;
5252
}
5353

54-
var manifests = api.manifests();
54+
let manifests = api.manifests();
5555
bumpManifests(manifests, options)
56-
.then(function() {
56+
.then(() => {
5757
api.grep(manifests, options);
58-
manifests.forEach(function (manifest) {
58+
manifests.forEach((manifest) => {
5959
api.runNpmScriptIfExists(manifest, 'version');
6060
});
6161
})
62-
.then(function() {
62+
.then(() => {
6363
api.git(manifests, options);
6464
})
65-
.catch(function(err) {
65+
.catch((err) => {
6666
console.error(chalk.red(err.message));
6767
process.exit(err.status || 1);
6868
});
@@ -75,13 +75,13 @@ else {
7575
* @param {object} options - CLI options
7676
* @returns {Promise}
7777
*/
78-
function bumpManifests(manifests, options) {
79-
var i = 0;
78+
function bumpManifests (manifests, options) {
79+
let i = 0;
8080

8181
return bumpNext('patch');
8282

83-
function bumpNext(defaultBumpType) {
84-
var manifest = manifests[i++];
83+
function bumpNext (defaultBumpType) {
84+
let manifest = manifests[i++];
8585
if (manifest) {
8686
return bumpManifest(manifest, defaultBumpType, options).then(bumpNext);
8787
}
@@ -99,11 +99,11 @@ function bumpManifests(manifests, options) {
9999
* @param {object} options - CLI options
100100
* @returns {Promise}
101101
*/
102-
function bumpManifest(manifest, defaultBumpType, options) {
103-
return new Promise(function(resolve, reject) {
102+
function bumpManifest (manifest, defaultBumpType, options) {
103+
return new Promise((resolve, reject) => {
104104
if (options.prompt) {
105105
// Prompt the user for the type of bump to perform
106-
var version = api.versionInfo(manifest, options);
106+
let version = api.versionInfo(manifest, options);
107107
console.log('\nCurrent version in %s is %s', manifest, version.current);
108108

109109
inquirer.prompt({
@@ -112,38 +112,39 @@ function bumpManifest(manifest, defaultBumpType, options) {
112112
message: 'How would you like to bump it?',
113113
default: defaultBumpType,
114114
choices: [
115-
{value: 'major', name: 'major (' + version.nextMajor + ')'},
116-
{value: 'minor', name: 'minor (' + version.nextMinor + ')'},
117-
{value: 'patch', name: 'patch (' + version.nextPatch + ')'},
118-
{value: 'premajor', name: 'pre-release major (' + version.nextPreMajor + ')'},
119-
{value: 'preminor', name: 'pre-release minor (' + version.nextPreMinor + ')'},
120-
{value: 'prepatch', name: 'pre-relase patch (' + version.nextPrePatch + ')'},
121-
{value: 'prerelease', name: 'pre-release (' + version.nextPreRelease + ')'}
115+
{ value: 'major', name: 'major (' + version.nextMajor + ')' },
116+
{ value: 'minor', name: 'minor (' + version.nextMinor + ')' },
117+
{ value: 'patch', name: 'patch (' + version.nextPatch + ')' },
118+
{ value: 'premajor', name: 'pre-release major (' + version.nextPreMajor + ')' },
119+
{ value: 'preminor', name: 'pre-release minor (' + version.nextPreMinor + ')' },
120+
{ value: 'prepatch', name: 'pre-relase patch (' + version.nextPrePatch + ')' },
121+
{ value: 'prerelease', name: 'pre-release (' + version.nextPreRelease + ')' }
122122
]
123123
})
124-
.then(function(answer) {
124+
.then((answer) => {
125125
api.runNpmScriptIfExists(manifest, 'preversion');
126126
bump(answer.bump);
127127
});
128128
}
129129
else {
130-
var bumpType =
131-
options.major ? 'major' :
132-
options.minor ? 'minor' :
133-
options.patch ? 'patch' :
134-
options.premajor ? 'premajor' :
135-
options.preminor ? 'preminor' :
136-
options.prepatch ? 'prepatch' :
137-
options.prerelease ? 'prerelease' :
138-
defaultBumpType;
130+
let bumpType =
131+
options.major ? 'major'
132+
: options.minor ? 'minor'
133+
: options.patch ? 'patch'
134+
: options.premajor ? 'premajor'
135+
: options.preminor ? 'preminor'
136+
: options.prepatch ? 'prepatch'
137+
: options.prerelease ? 'prerelease'
138+
: defaultBumpType;
139139
api.runNpmScriptIfExists(manifest, 'preversion');
140140
bump(bumpType);
141141
}
142142

143-
function bump(bumpType) {
143+
function bump (bumpType) {
144144
try {
145145
api.bump(manifest, bumpType, options);
146-
} catch(ex) {
146+
}
147+
catch (ex) {
147148
reject(ex);
148149
}
149150
resolve(bumpType);

lib/index.js

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
'use strict';
22

3-
var SemVer = require('semver'),
4-
spawnSync = require('child_process').spawnSync,
5-
fs = require('fs'),
6-
path = require('path'),
7-
glob = require('glob'),
8-
indent = require('detect-indent'),
9-
logSymbols = require('log-symbols'),
10-
chalk = require('chalk'),
11-
cwd = process.cwd(),
12-
oldVersion, newVersion;
3+
const SemVer = require('semver');
4+
const spawnSync = require('child_process').spawnSync;
5+
const fs = require('fs');
6+
const path = require('path');
7+
const glob = require('glob');
8+
const indent = require('detect-indent');
9+
const logSymbols = require('log-symbols');
10+
const chalk = require('chalk');
11+
const cwd = process.cwd();
12+
13+
let oldVersion, newVersion;
1314

1415
module.exports = {
15-
manifests: manifests,
16-
versionInfo: versionInfo,
17-
bump: bump,
18-
runNpmScriptIfExists: runNpmScriptIfExists,
19-
grep: grep,
20-
git: git
16+
manifests,
17+
versionInfo,
18+
bump,
19+
runNpmScriptIfExists,
20+
grep,
21+
git
2122
};
2223

2324
/**
@@ -26,11 +27,11 @@ module.exports = {
2627
*
2728
* @returns {string[]}
2829
*/
29-
function manifests() {
30-
return ['package.json', 'bower.json', 'component.json'].filter(function(manifest) {
31-
var pkgPath = path.join(cwd, manifest);
30+
function manifests () {
31+
return ['package.json', 'bower.json', 'component.json'].filter((manifest) => {
32+
let pkgPath = path.join(cwd, manifest);
3233
try {
33-
var pkg = require(pkgPath);
34+
const pkg = require(pkgPath);
3435
return pkg.hasOwnProperty('version');
3536
}
3637
catch (err) {
@@ -46,12 +47,12 @@ function manifests() {
4647
* @param {object} options - CLI options
4748
* @returns {VersionInfo}
4849
*/
49-
function versionInfo(manifest, options) {
50-
var pkgPath = path.join(cwd, manifest);
51-
var pkg = require(pkgPath);
50+
function versionInfo (manifest, options) {
51+
let pkgPath = path.join(cwd, manifest);
52+
const pkg = require(pkgPath);
5253

53-
var current = new SemVer(pkg.version || '0.0.0');
54-
var identifier = options.preid || current.prerelease[0] || 'beta';
54+
let current = new SemVer(pkg.version || '0.0.0');
55+
let identifier = options.preid || current.prerelease[0] || 'beta';
5556

5657
/** @name VersionInfo **/
5758
return {
@@ -73,18 +74,18 @@ function versionInfo(manifest, options) {
7374
* @param {string} type - The type of bump to do ("major", "minor", "patch", "premajor", etc.)
7475
* @param {object} options - CLI options
7576
*/
76-
function bump(manifest, type, options) {
77-
var pkgPath = path.join(cwd, manifest);
78-
var pkg = require(pkgPath);
77+
function bump (manifest, type, options) {
78+
let pkgPath = path.join(cwd, manifest);
79+
const pkg = require(pkgPath);
7980

8081
// Increment the version number
8182
oldVersion = pkg.version || '0.0.0';
82-
var current = new SemVer(oldVersion);
83+
let current = new SemVer(oldVersion);
8384
current.inc(type, options.preid || current.prerelease[0] || 'beta');
8485
newVersion = pkg.version = current.version;
8586

8687
// Save the file
87-
var usedIndent = indent(fs.readFileSync(pkgPath, 'utf8')).indent || ' ';
88+
let usedIndent = indent(fs.readFileSync(pkgPath, 'utf8')).indent || ' ';
8889

8990
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, usedIndent));
9091

@@ -97,13 +98,13 @@ function bump(manifest, type, options) {
9798
* @param {string} manifest - The name of the manifest file (e.g. "package.json")
9899
* @param {string} script - Name of the script ("preversion", "postversion", etc.)
99100
*/
100-
function runNpmScriptIfExists(manifest, script) {
101+
function runNpmScriptIfExists (manifest, script) {
101102
if (manifest !== 'package.json') {
102103
return;
103104
}
104-
var pkgPath = path.join(cwd, manifest);
105-
var pkg = require(pkgPath);
106-
var pkgScripts = pkg.scripts;
105+
let pkgPath = path.join(cwd, manifest);
106+
const pkg = require(pkgPath);
107+
let pkgScripts = pkg.scripts;
107108

108109
if (pkgScripts && pkgScripts[script]) {
109110
exec('npm', ['run', script]);
@@ -119,11 +120,11 @@ function runNpmScriptIfExists(manifest, script) {
119120
*
120121
* @param {object} options - CLI options
121122
*/
122-
function grep(manifests, options) {
123+
function grep (manifests, options) {
123124
if (options.grep) {
124125
// Separate the glob patterns into two lists: included and excluded
125-
var included = [], excluded = [];
126-
options.grep.forEach(function(pattern) {
126+
let included = [], excluded = [];
127+
options.grep.forEach((pattern) => {
127128
if (pattern[0] === '!') {
128129
excluded.push(pattern);
129130
}
@@ -133,17 +134,17 @@ function grep(manifests, options) {
133134
});
134135

135136
// Process each glob pattern
136-
included.forEach(function(pattern) {
137-
var files = glob.sync(pattern, {nodir: true, ignore: excluded});
137+
included.forEach((pattern) => {
138+
let files = glob.sync(pattern, { nodir: true, ignore: excluded });
138139

139140
// Process each matched file
140-
files.forEach(function(file) {
141+
files.forEach((file) => {
141142
// Read the file
142-
var fileContents = fs.readFileSync(file, {encoding: 'utf8'});
143+
let fileContents = fs.readFileSync(file, { encoding: 'utf8' });
143144

144145
// Replace the old version number with the new version number
145-
var oldVersionPattern = new RegExp(oldVersion.replace(/\./g, '\\.'), 'g');
146-
var newFileContents = fileContents.replace(oldVersionPattern, newVersion);
146+
let oldVersionPattern = new RegExp(oldVersion.replace(/\./g, '\\.'), 'g');
147+
let newFileContents = fileContents.replace(oldVersionPattern, newVersion);
147148

148149
// Only save the file if there were changes
149150
if (newFileContents !== fileContents) {
@@ -162,12 +163,12 @@ function grep(manifests, options) {
162163
* @param {string[]} manifests - An array of manifest files to bump
163164
* @param {object} options - CLI options
164165
*/
165-
function git(manifests, options) {
166+
function git (manifests, options) {
166167
if (options.commit || options.tag || options.push) {
167168
// Git Commit
168-
var commitArgs = ['commit'];
169+
let commitArgs = ['commit'];
169170
commitArgs = commitArgs.concat(options.all ? '-a' : manifests);
170-
var commitMessage = 'release v' + newVersion;
171+
let commitMessage = 'release v' + newVersion;
171172
if (options.commitMessage) {
172173
commitMessage = 'v' + newVersion + ' ' + options.commitMessage;
173174
}
@@ -181,7 +182,7 @@ function git(manifests, options) {
181182
console.log(logSymbols.success, 'Git tag');
182183
}
183184

184-
manifests.forEach(function (manifest) {
185+
manifests.forEach((manifest) => {
185186
runNpmScriptIfExists(manifest, 'postversion');
186187
});
187188

@@ -203,15 +204,15 @@ function git(manifests, options) {
203204
* @param {object} [opts] - Options to pass to spawnSync
204205
* @param {boolean} [opts.skipCustomErrorLog] - Use to skip logging the error when using try-catch
205206
*/
206-
function exec(command, args, opts) {
207+
function exec (command, args, opts) {
207208
opts = opts || {};
208-
var skipCustomErrorLog = opts.skipCustomErrorLog;
209+
let skipCustomErrorLog = opts.skipCustomErrorLog;
209210
delete opts.skipCustomErrorLog;
210-
//use which to make sure that node spawn can find correct executable, at the moment this is used as
211-
//a windows fallback, until https://github.com/libuv/libuv/pull/358 is fixed
212-
var which = require('npm-which')(process.cwd());
213-
var pathToCommand = which.sync(command);
214-
var result = spawnSync(pathToCommand, args, opts);
211+
// use which to make sure that node spawn can find correct executable, at the moment this is used as
212+
// a windows fallback, until https://github.com/libuv/libuv/pull/358 is fixed
213+
const which = require('npm-which')(process.cwd());
214+
let pathToCommand = which.sync(command);
215+
let result = spawnSync(pathToCommand, args, opts);
215216
if (result.status || result.error) {
216217
if (!skipCustomErrorLog) {
217218
console.error(
@@ -220,9 +221,9 @@ function exec(command, args, opts) {
220221
);
221222
}
222223

223-
var err = result.error;
224+
let err = result.error;
224225
if (!result.error) {
225-
var output = result.stdout.toString() || result.stderr.toString();
226+
let output = result.stdout.toString() || result.stderr.toString();
226227
err = new Error(output);
227228
}
228229
err.status = result.status;

0 commit comments

Comments
 (0)