Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 079ccbb

Browse files
committed
refactor - switch to regex parsing
1 parent f2ac137 commit 079ccbb

File tree

13 files changed

+38
-442
lines changed

13 files changed

+38
-442
lines changed

README.md

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Build Status](https://travis-ci.org/stevelacy/gulp-bump.png?branch=master)](https://travis-ci.org/stevelacy/gulp-bump)
33
[![NPM version](https://badge.fury.io/js/gulp-bump.png)](http://badge.fury.io/js/gulp-bump)
44

5-
> Bump any JSON file which supports [semver](http://semver.org/) versioning
5+
> Bump any version in any file which supports [semver](http://semver.org/) versioning
66
77
## Information
88

@@ -12,7 +12,7 @@
1212
</tr>
1313
<tr>
1414
<td>Description</td>
15-
<td>Bump any Semver version json file
15+
<td>Bump any Semver version in any file
1616
with gulp (gulpjs.com)</td>
1717
</tr>
1818
<tr>
@@ -34,6 +34,9 @@ If you are just requiring a bump for npm, consider using [npm version](https://d
3434
```bash
3535
$ npm install gulp-bump --save
3636
```
37+
#### Breaking changes
38+
39+
`gulp-bump` v2 supports Any valid semver in any filetype
3740

3841
## Example
3942

@@ -60,7 +63,7 @@ gulp.task('bump', function(){
6063
// Defined method of updating:
6164
// Semantic major
6265
gulp.task('bump', function(){
63-
gulp.src('./bower.json')
66+
gulp.src('./package.yml')
6467
.pipe(bump({type:'major'}))
6568
.pipe(gulp.dest('./'));
6669
});
@@ -80,14 +83,6 @@ gulp.task('bump', function(){
8083
.pipe(gulp.dest('./'));
8184
});
8285

83-
// Override the tab size for indenting
84-
// (or simply omit to keep the current formatting)
85-
gulp.task('bump', function(){
86-
gulp.src('./package.json')
87-
.pipe(bump({type:'major', indent: 4 }))
88-
.pipe(gulp.dest('./'));
89-
});
90-
9186
// Define the key for versioning off
9287
gulp.task('bump', function(){
9388
gulp.src('./package.json')
@@ -138,79 +133,8 @@ gulp.task('default', function(){
138133
####You can view more examples in the [example folder.](https://github.com/stevelacy/gulp-bump/tree/master/examples)
139134

140135
## Options
141-
### options.type
142-
Semver version type to bump.
143-
144-
Type: `String`
145-
Default: `patch`
146-
Valid values: `major|minor|patch|prerelease`
147-
148-
Example:
149-
150-
```js
151-
.pipe(bump({type: 'Major'})
152-
.pipe(bump()) //--> defaults to patch
153-
```
154-
155-
### options.key
156-
Set the versioning key
157-
158-
Type: `String`
159-
Default: `version`
160-
161-
Example:
162136

163-
```js
164-
.pipe(bump({key: 'appversion'}))
165-
.pipe(bump({key: 'build-version'}))
166-
.pipe(bump({key: 'dev-version', type: 'major'}))
167-
```
168-
##### Dot notation is supported for nested versions:
169-
170-
```js
171-
172-
.pipe(bump({key: {'sublevel.version'}}))
173-
174-
/*
175-
{
176-
"sublevel": {
177-
"version": "<semver>"
178-
}
179-
}
180-
*/
181-
```
182-
183-
### options.version
184-
Set a specific version to bump to.
185-
186-
Type: `String`
187-
Default: `null`
188-
189-
Example:
190-
191-
```js
192-
.pipe(bump({version: '1.2.3'}))
193-
.pipe(bump({version: '1.0.0-alpha'}))
194-
```
195-
196-
### options.indent
197-
Set the amount of spaces for indentation in the result JSON file.
198-
199-
Type: `Number`
200-
Default: Same as original source file
201-
202-
### options.preid
203-
Set the prerelase tag to use
204-
205-
Type: `String`
206-
Default: `null`
207-
208-
Example:
209-
210-
```js
211-
bump({type: 'prerelease', preid : 'alphaWhateverTheYWant'});
212-
// => '0.0.2-alphaWhateverTheYWant.0'
213-
```
137+
All options are passed to [bump-regex](https://github.com/stevelacy/bump-regex)
214138

215139
## Versioning
216140
#### Versioning Used: [Semantic](http://semver.org/)

index.js

Lines changed: 16 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,109 +4,34 @@ var path = require('path');
44
var pluginError = require('plugin-error');
55
var log = require('plugin-log');
66
var through = require('through2');
7+
var bump = require('bump-regex');
78
var semver = require('semver');
8-
var Dot = require('dot-object');
9+
10+
const PLUGIN_NAME = 'gulp-bump';
911

1012
module.exports = function(opts) {
11-
// set task options
12-
opts = setDefaultOptions(opts);
13+
14+
opts = opts || {};
15+
if (!opts.type || !semver.inc('0.0.1', opts.type)) {
16+
opts.type = 'patch';
17+
}
1318

1419
return through.obj(function(file, enc, cb) {
1520

1621
if (file.isNull()) {
1722
return cb(null, file);
1823
}
1924
if (file.isStream()) {
20-
return cb(new pluginError('gulp-bump', 'Streaming not supported'));
21-
}
22-
23-
var content = String(file.contents);
24-
var json;
25-
var ver;
26-
var dot;
27-
28-
try {
29-
json = JSON.parse(content);
30-
} catch (e) {
31-
return cb(new pluginError('gulp-bump', 'Problem parsing JSON file', {
32-
fileName: file.path,
33-
showStack: true
34-
}));
25+
return cb(new pluginError(PLUGIN_NAME, 'Streaming not supported'));
3526
}
3627

37-
// get the version and key
38-
if (opts.key.indexOf('.') > -1) {
39-
dot = new Dot();
40-
opts.value = dot.pick(opts.key, json);
41-
ver = bump(opts);
42-
}
43-
else {
44-
opts.value = json[opts.key];
45-
if (!semver.valid(opts.value) && !opts.version) {
46-
return cb(new pluginError('gulp-bump', 'Detected invalid semver ' + opts.key, {
47-
fileName: file.path,
48-
showStack: false
49-
}));
28+
opts.str = String(file.contents);
29+
bump(opts, function(err, res) {
30+
if (err) {
31+
return cb(new pluginError(PLUGIN_NAME, err));
5032
}
51-
ver = bump(opts);
52-
}
53-
54-
// set key
55-
if (!json[opts.key]) {
56-
// log to user that key didn't exist before
57-
log('Creating key', log.colors.red(opts.key), 'with version:', log.colors.cyan(ver));
58-
}
59-
60-
if (dot) {
61-
dot.str(opts.key, ver, json);
62-
}
63-
64-
else {
65-
json[opts.key] = ver;
66-
}
67-
68-
file.contents = new Buffer(JSON.stringify(json, null, opts.indent || space(content)) + possibleNewline(content));
69-
70-
log('Bumped \'' + log.colors.cyan(path.basename(file.path)) +
71-
'\' ' + log.colors.magenta(opts.key) +
72-
' to: ' + log.colors.cyan(ver));
73-
74-
cb(null, file);
33+
file.contents = new Buffer(res);
34+
cb(null, file);
35+
});
7536
});
7637
};
77-
78-
function bump(opts) {
79-
if (opts.version) {
80-
return opts.version;
81-
}
82-
83-
return semver.inc(opts.value, opts.type, opts.preid);
84-
}
85-
86-
function setDefaultOptions(opts) {
87-
opts = opts || {};
88-
opts.key = opts.key || 'version';
89-
opts.indent = opts.indent || void 0;
90-
// default type bump is patch
91-
if (!opts.type || !semver.inc('0.0.1', opts.type)) {
92-
opts.type = 'patch';
93-
}
94-
// if passed specific version - validate it
95-
if (opts.version && !semver.valid(opts.version, opts.type)) {
96-
log('invalid version used as option', log.colors.red(opts.version));
97-
opts.version = null;
98-
}
99-
return opts;
100-
}
101-
102-
// Preserve new line at the end of a file
103-
function possibleNewline(json) {
104-
var lastChar = (json.slice(-1) === '\n') ? '\n' : '';
105-
return lastChar;
106-
}
107-
108-
// Figured out which "space" params to be used for JSON.stringfiy.
109-
function space(json) {
110-
var match = json.match(/^(?:(\t+)|( +))"/m);
111-
return match ? (match[1] ? '\t' : match[2].length) : '';
112-
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
"author": "Steve Lacy <[email protected]> (http://slacy.me)",
88
"main": "./index.js",
99
"dependencies": {
10-
"dot-object": "^1.2.0",
1110
"plugin-error": "^0.1.2",
1211
"plugin-log": "^0.1.0",
1312
"semver": "^5.0.3",
14-
"through2": "^0.5.1"
13+
"through2": "^0.5.1",
14+
"bump-regex": "^1.0.0"
1515
},
1616
"devDependencies": {
1717
"mocha": "*",

test/dotNotation.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)