gulp-bump
Advanced tools
Comparing version 0.1.7 to 0.1.8
78
index.js
var gutil = require('gulp-util'); | ||
var map = require('map-stream'); | ||
var through = require('through2'); | ||
var semver = require('semver'); | ||
var setDefaultOptions = function(opts) { | ||
opts = opts || {}; | ||
opts.key = opts.key || 'version'; | ||
opts.indent = opts.indent || 2; | ||
// 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; | ||
}; | ||
module.exports = function(opts) { | ||
if(!opts) opts = {}; | ||
if(!semver.inc('0.0.1', opts.type)) opts.type = false; | ||
if(!opts.indent) opts.indent = 2; | ||
if(!opts.key) opts.key = 'version'; | ||
// set task options | ||
opts = setDefaultOptions(opts); | ||
var key = opts.key; | ||
var version = opts.version; | ||
var indent = opts.indent; | ||
var type = opts.type; | ||
function modifyContents(file, cb) { | ||
var json; | ||
if(file.isNull()) return cb(null, file); | ||
if(file.isStream()) return cb(new Error('gulp-bump: streams not supported')); | ||
return through.obj(function(file, enc, cb) { | ||
if (file.isNull()) { | ||
this.push(file); | ||
return cb(); | ||
} | ||
if (file.isStream()) { | ||
this.emit('error', new gutil.PluginError('gulp-bump', 'Streaming not supported')); | ||
return cb(); | ||
} | ||
try { | ||
json = JSON.parse(file.contents.toString()); | ||
} catch (e) { | ||
this.emit('error', new gutil.PluginError('gulp-bump', 'Problem parsing JSON file ' + file.path)); | ||
return cb(); | ||
} | ||
var json = JSON.parse(file.contents.toString()); | ||
json[opts.key] = semver.valid(opts[opts.key]) || semver.inc(json[opts.key], opts.type || 'patch'); | ||
file.contents = new Buffer(JSON.stringify(json, null, opts.indent) + '\n'); | ||
gutil.log('Bumped to version: '+gutil.colors.cyan(json[opts.key])); | ||
cb(null, file); | ||
} | ||
// just set a version to the key | ||
if (version) { | ||
if (!json[key]) { | ||
// log to user that key didn't exist before | ||
gutil.log('Creating key', gutil.colors.red(key), 'with version:', gutil.colors.cyan(version)); | ||
} | ||
json[key] = version; | ||
} | ||
else if (semver.valid(json[key])) { | ||
// increment the key with type | ||
json[key] = semver.inc(json[key], type); | ||
} | ||
else { | ||
this.emit('error', new gutil.PluginError('gulp-bump', 'Detected invalid semver ' + key + ' in file ' + file.path)); | ||
return cb(); | ||
} | ||
return map(modifyContents); | ||
file.contents = new Buffer(JSON.stringify(json, null, indent) + '\n'); | ||
gutil.log('Bumped ' + gutil.colors.magenta(key) + ' to: ' + gutil.colors.cyan(json[key])); | ||
this.push(file); | ||
return cb(); | ||
}); | ||
}; |
{ | ||
"name": "gulp-bump", | ||
"description": "Bump npm versions with Gulp (gulpjs.com)", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"homepage": "http://github.com/stevelacy/gulp-bump", | ||
@@ -10,5 +10,5 @@ "repository": "git://github.com/stevelacy/gulp-bump.git", | ||
"dependencies": { | ||
"map-stream": "~0.1.0", | ||
"semver": "~2.2.1", | ||
"gulp-util": "~2.2.12" | ||
"gulp-util": "^2.2.14", | ||
"semver": "^2.2.1", | ||
"through2": "^0.4.1" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": { |
@@ -1,6 +0,6 @@ | ||
#gulp-bump | ||
# gulp-bump | ||
[![Build Status](https://travis-ci.org/stevelacy/gulp-bump.png?branch=master)](https://travis-ci.org/stevelacy/gulp-bump) | ||
[![NPM version](https://badge.fury.io/js/gulp-bump.png)](http://badge.fury.io/js/gulp-bump) | ||
> Bump any json file which supports [semver](http://semver.org/) versioning | ||
> Bump any JSON file which supports [semver](http://semver.org/) versioning | ||
@@ -28,3 +28,2 @@ ## Information | ||
## Usage | ||
@@ -35,6 +34,7 @@ | ||
#### Install | ||
npm install gulp-bump --save | ||
```bash | ||
$ npm install gulp-bump --save | ||
``` | ||
## Example | ||
@@ -54,3 +54,2 @@ | ||
// Defined method of updating: | ||
@@ -72,3 +71,2 @@ // Semantic | ||
// Defined method of updating: | ||
@@ -82,3 +80,2 @@ // Set a specific version | ||
// Update bower, component, npm at once: | ||
@@ -91,3 +88,2 @@ gulp.task('bump', function(){ | ||
// Define the tab size for indenting | ||
@@ -108,4 +104,36 @@ gulp.task('bump', function(){ | ||
``` | ||
#### Bumping version and outputting different files | ||
```js | ||
// `fs` is used instead of require to prevent caching in watch (require caches) | ||
var fs = require('fs'); | ||
var getPackageJson = function () { | ||
return JSON.parse(fs.readFileSync('./package.json', 'utf8')); | ||
}; | ||
// bump versions on package/bower/manifest | ||
gulp.task('bump', function () { | ||
// reget package | ||
var pkg = getPackageJson(); | ||
// increment version | ||
var newVer = semver.inc(pkg.version, 'patch'); | ||
// uses gulp-filter | ||
var manifestFilter = tasks.filter(['manifest.json']); | ||
var regularJsons = tasks.filter(['!manifest.json']); | ||
return gulp.src(['./bower.json', './package.json', './src/manifest.json']) | ||
.pipe(tasks.bump({ | ||
version: newVer | ||
})) | ||
.pipe(manifestFilter) | ||
.pipe(gulp.dest('./src')) | ||
.pipe(manifestFilter.restore()) | ||
.pipe(regularJsons) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
// Run the gulp tasks | ||
@@ -115,14 +143,21 @@ gulp.task('default', function(){ | ||
}); | ||
``` | ||
``` | ||
####You can view more examples in the [example folder.](https://github.com/stevelacy/gulp-bump/tree/master/examples) | ||
## Options | ||
### options.type | ||
What type of version to bump to. | ||
Semver version type to bump. | ||
Type: `String` | ||
Default: `patch` | ||
Valid values: `major|minor|patch|prerelease` | ||
Example: | ||
```js | ||
.pipe(bump({type: 'Major'}) | ||
.pipe(bump()) //--> defaults to patch | ||
``` | ||
### options.key | ||
@@ -135,35 +170,39 @@ Set the versioning key | ||
Example: | ||
```js | ||
.pipe(bump({key: 'appversion'})) | ||
.pipe(bump({key: 'build-version'})) | ||
.pipe(bump({key: 'dev-version', type: 'major'})) | ||
``` | ||
### options.version | ||
Set a specific version. | ||
Set a specific version to bump to. | ||
Type: `String` | ||
Default: `none` | ||
Default: `null` | ||
Example: | ||
```js | ||
.pipe(bump({version: '1.2.3'})) | ||
.pipe(bump({version: '1.0.0-alpha'})) | ||
``` | ||
``` | ||
### options.indent | ||
Set the amount of spaces for indentation in the result JSON file. | ||
Type: `Number` | ||
Default: `2` | ||
###Versioning Used: [Semantic](http://semver.org/) | ||
### String, lowercase | ||
## Versioning | ||
#### Versioning Used: [Semantic](http://semver.org/) | ||
#### String, lowercase | ||
- MAJOR ("major") version when you make incompatible API changes | ||
- MINOR ("minor") version when you add functionality in a backwards-compatible manner | ||
- PATCH ("patch") version when you make backwards-compatible bug fixes. | ||
- PRERELEASE ("prerelease") a pre-release version | ||
### Version example | ||
#### Version example | ||
@@ -173,2 +212,3 @@ major: 1.0.0 | ||
patch: 0.0.2 | ||
prerelease: 0.0.1-2 | ||
@@ -175,0 +215,0 @@ |
354
test/main.js
var fs = require('fs'); | ||
var gutil = require('gulp-util'); | ||
var should = require('should'); | ||
var bump = require('../'); | ||
var bump = require('../index'); | ||
require('mocha'); | ||
var fixtureFile = fs.readFileSync('test/fixtures/package.json'); | ||
describe('gulp-bump', function() { | ||
it('should bump minor by default', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fs.readFileSync('test/fixtures/package.json') | ||
describe('gulp-bump - JSON comparison fixtures', function() { | ||
it('should bump patch version by default', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "version": "0.0.1" }') | ||
}); | ||
var bumpS = bump(); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
var bumpS = bump(); | ||
bumpS.once('data', function(newFile){ | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/default.json', 'utf8')); | ||
done(); | ||
it('should bump patch version as default and a key=appversion', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "appversion": "0.0.1" }') | ||
}); | ||
var bumpS = bump({key: 'appversion'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).appversion.should.equal('0.0.2'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should bump major if options.bump = major', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fs.readFileSync('test/fixtures/package.json') | ||
it('should ignore invalid type and use type=patch', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "version": "0.0.1" }') | ||
}); | ||
var bumpS = bump({type: 'invalidType'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
var bumpS = bump({type: 'major'}); | ||
bumpS.once('data', function(newFile){ | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/major.json', 'utf8')); | ||
done(); | ||
it('should set the correct version when supplied', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "version": "0.0.1" }') | ||
}); | ||
var bumpS = bump({version: '0.0.2'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should bump minor if options.bump = minor', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fs.readFileSync('test/fixtures/package.json') | ||
it('should set the correct version when supplied even if key did not exist', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{}') | ||
}); | ||
var bumpS = bump({version: '0.0.2'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
var bumpS = bump({type: 'minor'}); | ||
bumpS.once('data', function(newFile){ | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.json', 'utf8')); | ||
done(); | ||
it('should bump prerelease version', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "version": "0.0.1-0"}') | ||
}); | ||
var bumpS = bump({type: 'prerelease'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).version.should.equal('0.0.1-1'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should ignore and pass "patch" if options.bump is not Semantic', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fs.readFileSync('test/fixtures/package.json') | ||
describe('Test failure cases cases in gulp-bump', function() { | ||
it('should fail when not detect a valid semver version', function(done) { | ||
var file = 'some-dir/dummyfile.js'; | ||
var fakeFile = new gutil.File({ | ||
path: file, | ||
contents: new Buffer('{ "version": "0.A.1" }') | ||
}); | ||
var bumpS = bump(); | ||
bumpS.on('error', function(e) { | ||
should.exist(e); | ||
e.name.should.equal('Error'); | ||
e.message.should.containEql('Detected invalid semver version in file ' + file); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
var bumpS = bump({type: 'invalid'}); | ||
bumpS.once('data', function(newFile){ | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/default.json', 'utf8')); | ||
done(); | ||
it('should fail when not detect a valid semver version and wrong key', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "version": "0.0.1" }') | ||
}); | ||
var bumpS = bump({key: 'appversion'}); | ||
bumpS.on('error', function(e) { | ||
should.exist(e); | ||
e.name.should.equal('Error'); | ||
e.message.should.containEql('Detected invalid semver appversion in file'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should set version to value specified by options.version', function (done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fs.readFileSync('test/fixtures/package.json') | ||
it('should fail when supplied with an invalid JSON', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ invalid json oh no!!!}') | ||
}); | ||
var bumpS = bump(); | ||
bumpS.on('error', function(e) { | ||
should.exist(e); | ||
e.name.should.equal('Error'); | ||
e.message.should.containEql('Problem parsing JSON file'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
var bumpS = bump({version: '1.0.0'}); | ||
bumpS.once('data', function(newFile){ | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/version.json', 'utf8')); | ||
done(); | ||
it('should fallback to defaults when supplied with invalid semver version', function(done) { | ||
var fakeFile = new gutil.File({ | ||
contents: new Buffer('{ "version": "0.0.1" }') | ||
}); | ||
var bumpS = bump({version: '0.A.2'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2'); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should set the key to a custom version', function (done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/key.json", | ||
contents: fs.readFileSync('test/fixtures/key.json') | ||
describe('gulp-bump - JSON File fixtures', function() { | ||
it('should bump minor by default', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fixtureFile | ||
}); | ||
var bumpS = bump(); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/default.json', 'utf8')); | ||
done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
var bumpS = bump({key: "appversion"}); | ||
bumpS.once('data', function(newFile){ | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/key.json', 'utf8')); | ||
done(); | ||
it('should bump major if options.bump = major', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fixtureFile | ||
}); | ||
var bumpS = bump({type: 'major'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/major.json', 'utf8')); | ||
done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
bumpS.write(fakeFile); | ||
it('should bump minor if options.bump = minor', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fixtureFile | ||
}); | ||
var bumpS = bump({type: 'minor'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.json', 'utf8')); | ||
done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should set version to value specified by options.version', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/package.json", | ||
contents: fixtureFile | ||
}); | ||
var bumpS = bump({version: '1.0.0'}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/version.json', 'utf8')); | ||
done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
it('should set the key to a custom version', function(done) { | ||
var fakeFile = new gutil.File({ | ||
base: "test/", | ||
cwd: "test/", | ||
path: "test/fixtures/key.json", | ||
contents: fs.readFileSync('test/fixtures/key.json') | ||
}); | ||
var bumpS = bump({key: "appversion"}); | ||
bumpS.once('data', function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.path); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/key.json', 'utf8')); | ||
done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
24425
521
231
18
+ Addedthrough2@^0.4.1
+ Addedobject-keys@0.4.0(transitive)
+ Addedsemver@2.3.2(transitive)
+ Addedthrough2@0.4.2(transitive)
+ Addedxtend@2.1.2(transitive)
- Removedmap-stream@~0.1.0
- Removedmap-stream@0.1.0(transitive)
- Removedsemver@2.2.1(transitive)
Updatedgulp-util@^2.2.14
Updatedsemver@^2.2.1