Comparing version 1.0.0 to 2.0.0
107
index.js
@@ -7,9 +7,14 @@ 'use strict'; | ||
var through = require('through2'); | ||
var bump = require('bump-regex'); | ||
var semver = require('semver'); | ||
var Dot = require('dot-object'); | ||
const PLUGIN_NAME = 'gulp-bump'; | ||
module.exports = function(opts) { | ||
// set task options | ||
opts = setDefaultOptions(opts); | ||
opts = opts || {}; | ||
if (!opts.type || !semver.inc('0.0.1', opts.type)) { | ||
opts.type = 'patch'; | ||
} | ||
return through.obj(function(file, enc, cb) { | ||
@@ -21,94 +26,14 @@ | ||
if (file.isStream()) { | ||
return cb(new pluginError('gulp-bump', 'Streaming not supported')); | ||
return cb(new pluginError(PLUGIN_NAME, 'Streaming not supported')); | ||
} | ||
var content = String(file.contents); | ||
var json; | ||
var ver; | ||
var dot; | ||
try { | ||
json = JSON.parse(content); | ||
} catch (e) { | ||
return cb(new pluginError('gulp-bump', 'Problem parsing JSON file', { | ||
fileName: file.path, | ||
showStack: true | ||
})); | ||
} | ||
// get the version and key | ||
if (opts.key.indexOf('.') > -1) { | ||
dot = new Dot(); | ||
opts.value = dot.pick(opts.key, json); | ||
ver = bump(opts); | ||
} | ||
else { | ||
opts.value = json[opts.key]; | ||
if (!semver.valid(opts.value) && !opts.version) { | ||
return cb(new pluginError('gulp-bump', 'Detected invalid semver ' + opts.key, { | ||
fileName: file.path, | ||
showStack: false | ||
})); | ||
opts.str = String(file.contents); | ||
bump(opts, function(err, res) { | ||
if (err) { | ||
return cb(new pluginError(PLUGIN_NAME, err)); | ||
} | ||
ver = bump(opts); | ||
} | ||
// set key | ||
if (!json[opts.key]) { | ||
// log to user that key didn't exist before | ||
log('Creating key', log.colors.red(opts.key), 'with version:', log.colors.cyan(ver)); | ||
} | ||
if (dot) { | ||
dot.str(opts.key, ver, json); | ||
} | ||
else { | ||
json[opts.key] = ver; | ||
} | ||
file.contents = new Buffer(JSON.stringify(json, null, opts.indent || space(content)) + possibleNewline(content)); | ||
log('Bumped \'' + log.colors.cyan(path.basename(file.path)) + | ||
'\' ' + log.colors.magenta(opts.key) + | ||
' to: ' + log.colors.cyan(ver)); | ||
cb(null, file); | ||
file.contents = new Buffer(res); | ||
cb(null, file); | ||
}); | ||
}); | ||
}; | ||
function bump(opts) { | ||
if (opts.version) { | ||
return opts.version; | ||
} | ||
return semver.inc(opts.value, opts.type, opts.preid); | ||
} | ||
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)) { | ||
log('invalid version used as option', log.colors.red(opts.version)); | ||
opts.version = null; | ||
} | ||
return opts; | ||
} | ||
// Preserve 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": "1.0.0", | ||
"version": "2.0.0", | ||
"homepage": "http://github.com/stevelacy/gulp-bump", | ||
@@ -10,7 +10,7 @@ "repository": "git://github.com/stevelacy/gulp-bump.git", | ||
"dependencies": { | ||
"dot-object": "^1.2.0", | ||
"plugin-error": "^0.1.2", | ||
"plugin-log": "^0.1.0", | ||
"semver": "^5.0.3", | ||
"through2": "^0.5.1" | ||
"through2": "^0.5.1", | ||
"bump-regex": "^1.0.0" | ||
}, | ||
@@ -17,0 +17,0 @@ "devDependencies": { |
@@ -5,3 +5,3 @@ # gulp-bump | ||
> Bump any JSON file which supports [semver](http://semver.org/) versioning | ||
> Bump any version in any file which supports [semver](http://semver.org/) versioning | ||
@@ -16,3 +16,3 @@ ## Information | ||
<td>Description</td> | ||
<td>Bump any Semver version json file | ||
<td>Bump any Semver version in any file | ||
with gulp (gulpjs.com)</td> | ||
@@ -39,3 +39,6 @@ </tr> | ||
``` | ||
#### Breaking changes | ||
`gulp-bump` v2 supports Any valid semver in any filetype | ||
## Example | ||
@@ -66,3 +69,3 @@ | ||
gulp.task('bump', function(){ | ||
gulp.src('./bower.json') | ||
gulp.src('./package.yml') | ||
.pipe(bump({type:'major'})) | ||
@@ -87,10 +90,2 @@ .pipe(gulp.dest('./')); | ||
// Override the tab size for indenting | ||
// (or simply omit to keep the current formatting) | ||
gulp.task('bump', function(){ | ||
gulp.src('./package.json') | ||
.pipe(bump({type:'major', indent: 4 })) | ||
.pipe(gulp.dest('./')); | ||
}); | ||
// Define the key for versioning off | ||
@@ -107,7 +102,6 @@ gulp.task('bump', function(){ | ||
```js | ||
// `fs` is used instead of require to prevent caching in watch (require caches) | ||
var fs = require('fs'); | ||
var semver = require('semver'); | ||
var fs = require('fs'); | ||
var getPackageJson = function () { | ||
@@ -148,76 +142,5 @@ return JSON.parse(fs.readFileSync('./package.json', 'utf8')); | ||
## Options | ||
### options.type | ||
Semver version type to bump. | ||
Type: `String` | ||
Default: `patch` | ||
Valid values: `major|minor|patch|prerelease` | ||
All options are passed to [bump-regex](https://github.com/stevelacy/bump-regex) | ||
Example: | ||
```js | ||
.pipe(bump({type: 'Major'}) | ||
.pipe(bump()) //--> defaults to patch | ||
``` | ||
### options.key | ||
Set the versioning key | ||
Type: `String` | ||
Default: `version` | ||
Example: | ||
```js | ||
.pipe(bump({key: 'appversion'})) | ||
.pipe(bump({key: 'build-version'})) | ||
.pipe(bump({key: 'dev-version', type: 'major'})) | ||
``` | ||
##### Dot notation is supported for nested versions: | ||
```js | ||
.pipe(bump({key: {'sublevel.version'}})) | ||
/* | ||
{ | ||
"sublevel": { | ||
"version": "<semver>" | ||
} | ||
} | ||
*/ | ||
``` | ||
### options.version | ||
Set a specific version to bump to. | ||
Type: `String` | ||
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: Same as original source file | ||
### options.preid | ||
Set the prerelase tag to use | ||
Type: `String` | ||
Default: `null` | ||
Example: | ||
```js | ||
bump({type: 'prerelease', preid : 'alphaWhateverTheYWant'}); | ||
// => '0.0.2-alphaWhateverTheYWant.0' | ||
``` | ||
## Versioning | ||
@@ -224,0 +147,0 @@ #### Versioning Used: [Semantic](http://semver.org/) |
@@ -13,9 +13,7 @@ 'use strict'; | ||
path: 'some-dir/dummyfile.js', | ||
contents: new Buffer('{ "version": "0.0.0" }') | ||
contents: new Buffer('{ "version": "0.A.0" }') | ||
}); | ||
it('should fail when not detect a valid semver version', function(done) { | ||
it('should fail with invalid semver version', function(done) { | ||
fakeFile.contents = new Buffer('{ "version": "0.A.1" }'); | ||
var bumpS = bump(); | ||
@@ -25,4 +23,3 @@ | ||
should.exist(e); | ||
e.message.should.equal('Detected invalid semver version'); | ||
e.fileName.should.containEql(fakeFile.path); | ||
e.message.should.equal('Invalid semver'); | ||
return done(); | ||
@@ -33,48 +30,2 @@ }); | ||
}); | ||
it('should fail with an invalid semver version', function(done) { | ||
fakeFile.contents = new Buffer('{ "version": "0.0.1" }'); | ||
var bumpS = bump({key: 'appversion'}); | ||
bumpS.on('error', function(e) { | ||
should.exist(e); | ||
e.message.should.containEql('Detected invalid semver appversion'); | ||
e.fileName.should.containEql(fakeFile.path); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
it('should fail when supplied with an invalid JSON', function(done) { | ||
fakeFile.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'); | ||
e.fileName.should.containEql(fakeFile.path); | ||
return done(); | ||
}); | ||
bumpS.write(fakeFile); | ||
bumpS.end(); | ||
}); | ||
it('should fallback to defaults when supplied with invalid semver version', function(done) { | ||
fakeFile.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(); | ||
}); | ||
}); |
@@ -58,4 +58,4 @@ 'use strict'; | ||
cwd: 'test/', | ||
path: 'test/fixtures/package.json', | ||
contents: fixtureFile | ||
path: 'test/fixtures/minor.yml', | ||
contents: fs.readFileSync('test/fixtures/minor.yml') | ||
}); | ||
@@ -69,3 +69,3 @@ | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.json', 'utf8')); | ||
String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.yml', 'utf8')); | ||
done(); | ||
@@ -72,0 +72,0 @@ }); |
@@ -10,3 +10,3 @@ { | ||
"dependencies": { | ||
"semver":"*" | ||
"semver": "*" | ||
}, | ||
@@ -13,0 +13,0 @@ "devDependencies": { |
@@ -10,3 +10,3 @@ { | ||
"dependencies": { | ||
"semver":"*" | ||
"semver": "*" | ||
}, | ||
@@ -16,3 +16,3 @@ "devDependencies": { | ||
"should": "*", | ||
"gulp-util":"*" | ||
"gulp-util": "*" | ||
}, | ||
@@ -19,0 +19,0 @@ "scripts": { |
@@ -84,20 +84,2 @@ 'use strict'; | ||
it('should set the correct version when supplied even if key did not exist', function(done) { | ||
var fakeFile = new File({ | ||
contents: new Buffer('{}'), | ||
path: 'test/fixtures/test.json' | ||
}); | ||
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(); | ||
}); | ||
it('should bump prerelease version', function(done) { | ||
@@ -104,0 +86,0 @@ var fakeFile = new File({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22267
23
490
181
+ Addedbump-regex@^1.0.0
+ Addedbump-regex@1.0.0(transitive)
- Removeddot-object@^1.2.0
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcommander@2.20.3(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddot-object@1.9.0(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedwrappy@1.0.2(transitive)