gulp-bump
Advanced tools
Comparing version 0.2.1 to 0.2.2
90
index.js
@@ -9,37 +9,5 @@ 'use strict'; | ||
var setDefaultOptions = function(opts) { | ||
opts = opts || {}; | ||
opts.key = opts.key || 'version'; | ||
opts.indent = opts.indent || void 0; | ||
// default type bump is patch | ||
if (!opts.type || !semver.inc('0.0.1', opts.type)) { | ||
opts.type = 'patch'; | ||
} | ||
// if passed specific version - validate it | ||
if (opts.version && !semver.valid(opts.version, opts.type)) { | ||
gutil.log('invalid version used as option', gutil.colors.red(opts.version)); | ||
opts.version = null; | ||
} | ||
return opts; | ||
}; | ||
// Preserver new line at the end of a file | ||
var possibleNewline = function (json) { | ||
var lastChar = (json.slice(-1) === '\n') ? '\n' : ''; | ||
return lastChar; | ||
}; | ||
// Figured out which "space" params to be used for JSON.stringfiy. | ||
var space = function space(json) { | ||
var match = json.match(/^(?:(\t+)|( +))"/m); | ||
return match ? (match[1] ? '\t' : match[2].length) : ''; | ||
}; | ||
module.exports = function(opts) { | ||
// set task options | ||
opts = setDefaultOptions(opts); | ||
var key = opts.key; | ||
var version = opts.version; | ||
var indent = opts.indent; | ||
var type = opts.type; | ||
@@ -64,29 +32,57 @@ var content, json, ver; | ||
// just set a version to the key | ||
if (version) { | ||
if (!content[key]) { | ||
if (opts.version) { | ||
if (!content[opts.key]) { | ||
// log to user that key didn't exist before | ||
gutil.log('Creating key', gutil.colors.red(key), 'with version:', gutil.colors.cyan(version)); | ||
gutil.log('Creating key', gutil.colors.red(opts.key), 'with version:', gutil.colors.cyan(opts.version)); | ||
} | ||
content[key] = version; | ||
ver = content[key]; | ||
content[opts.key] = opts.version; | ||
ver = content[opts.key]; | ||
} | ||
else if (semver.valid(content[key])) { | ||
else if (semver.valid(content[opts.key])) { | ||
// increment the key with type | ||
content[key] = semver.inc(content[key], type); | ||
ver = content[key]; | ||
content[opts.key] = semver.inc(content[opts.key], opts.type); | ||
ver = content[opts.key]; | ||
} | ||
else if (key.indexOf('.') > -1) { | ||
else if (opts.key.indexOf('.') > -1) { | ||
var dot = new Dot(); | ||
var value = dot.pick(key, content); | ||
ver = semver.inc(value, type); | ||
dot.str(key, ver, content); | ||
var value = dot.pick(opts.key, content); | ||
ver = semver.inc(value, opts.type); | ||
dot.str(opts.key, ver, content); | ||
} | ||
else { | ||
return cb(new gutil.PluginError('gulp-bump', 'Detected invalid semver ' + key, {fileName: file.path, showStack: false})); | ||
return cb(new gutil.PluginError('gulp-bump', 'Detected invalid semver ' + opts.key, {fileName: file.path, showStack: false})); | ||
} | ||
file.contents = new Buffer(JSON.stringify(content, null, indent || space(json)) + possibleNewline(json)); | ||
file.contents = new Buffer(JSON.stringify(content, null, opts.indent || space(json)) + possibleNewline(json)); | ||
gutil.log('Bumped \'' + gutil.colors.cyan(path.basename(file.path)) + '\' ' + gutil.colors.magenta(key) + ' to: ' + gutil.colors.cyan(ver)); | ||
gutil.log('Bumped \'' + gutil.colors.cyan(path.basename(file.path)) + '\' ' + gutil.colors.magenta(opts.key) + ' to: ' + gutil.colors.cyan(ver)); | ||
cb(null, file); | ||
}); | ||
}; | ||
function setDefaultOptions(opts) { | ||
opts = opts || {}; | ||
opts.key = opts.key || 'version'; | ||
opts.indent = opts.indent || void 0; | ||
// default type bump is patch | ||
if (!opts.type || !semver.inc('0.0.1', opts.type)) { | ||
opts.type = 'patch'; | ||
} | ||
// if passed specific version - validate it | ||
if (opts.version && !semver.valid(opts.version, opts.type)) { | ||
gutil.log('invalid version used as option', gutil.colors.red(opts.version)); | ||
opts.version = null; | ||
} | ||
return opts; | ||
} | ||
// Preserver new line at the end of a file | ||
function possibleNewline(json) { | ||
var lastChar = (json.slice(-1) === '\n') ? '\n' : ''; | ||
return lastChar; | ||
} | ||
// Figured out which "space" params to be used for JSON.stringfiy. | ||
function space(json) { | ||
var match = json.match(/^(?:(\t+)|( +))"/m); | ||
return match ? (match[1] ? '\t' : match[2].length) : ''; | ||
} |
{ | ||
"name": "gulp-bump", | ||
"description": "Bump npm versions with Gulp (gulpjs.com)", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"homepage": "http://github.com/stevelacy/gulp-bump", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/stevelacy/gulp-bump.git", |
30652
716