11'use strict' ;
22
33var path = require ( 'path' ) ;
4- var gutil = require ( 'gulp-util' ) ;
4+ var pluginError = require ( 'plugin-error' ) ;
5+ var log = require ( 'plugin-log' ) ;
56var through = require ( 'through2' ) ;
67var semver = require ( 'semver' ) ;
78var Dot = require ( 'dot-object' ) ;
@@ -10,53 +11,78 @@ module.exports = function(opts) {
1011 // set task options
1112 opts = setDefaultOptions ( opts ) ;
1213
13- var content , json , ver ;
14-
1514 return through . obj ( function ( file , enc , cb ) {
15+
1616 if ( file . isNull ( ) ) {
1717 return cb ( null , file ) ;
1818 }
1919 if ( file . isStream ( ) ) {
20- return cb ( new gutil . PluginError ( 'gulp-bump' , 'Streaming not supported' ) ) ;
20+ return cb ( new pluginError ( 'gulp-bump' , 'Streaming not supported' ) ) ;
2121 }
2222
23- json = file . contents . toString ( ) ;
23+ var content = String ( file . contents ) ;
24+ var json ;
25+ var ver ;
26+ var dot ;
27+
2428 try {
25- content = JSON . parse ( json ) ;
29+ json = JSON . parse ( content ) ;
2630 } catch ( e ) {
27- return cb ( new gutil . PluginError ( 'gulp-bump' , 'Problem parsing JSON file' , { fileName : file . path , showStack : true } ) ) ;
31+ return cb ( new pluginError ( 'gulp-bump' , 'Problem parsing JSON file' , {
32+ fileName : file . path ,
33+ showStack : true
34+ } ) ) ;
2835 }
2936
30- // just set a version to the key
31- if ( opts . version ) {
32- if ( ! content [ opts . key ] ) {
33- // log to user that key didn't exist before
34- gutil . log ( 'Creating key' , gutil . colors . red ( opts . key ) , 'with version:' , gutil . colors . cyan ( opts . version ) ) ;
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+ } ) ) ;
3550 }
36- content [ opts . key ] = opts . version ;
37- ver = content [ opts . key ] ;
51+ ver = bump ( opts ) ;
3852 }
39- else if ( semver . valid ( content [ opts . key ] ) ) {
40- // increment the key with type
41- content [ opts . key ] = semver . inc ( content [ opts . key ] , opts . type , opts . preid ) ;
42- ver = content [ opts . key ] ;
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 ) ) ;
4358 }
44- else if ( opts . key . indexOf ( '.' ) > - 1 ) {
45- var dot = new Dot ( ) ;
46- var value = dot . pick ( opts . key , content ) ;
47- ver = semver . inc ( value , opts . type ) ;
48- dot . str ( opts . key , ver , content ) ;
59+
60+ if ( dot ) {
61+ dot . str ( opts . key , ver , json ) ;
4962 }
63+
5064 else {
51- return cb ( new gutil . PluginError ( 'gulp-bump' , 'Detected invalid semver ' + opts . key , { fileName : file . path , showStack : false } ) ) ;
65+ json [ opts . key ] = ver ;
5266 }
53- file . contents = new Buffer ( JSON . stringify ( content , null , opts . indent || space ( json ) ) + possibleNewline ( json ) ) ;
5467
55- gutil . log ( 'Bumped \'' + gutil . colors . cyan ( path . basename ( file . path ) ) + '\' ' + gutil . colors . magenta ( opts . key ) + ' to: ' + gutil . colors . cyan ( ver ) ) ;
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+
5674 cb ( null , file ) ;
5775 } ) ;
5876} ;
5977
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+
6086function setDefaultOptions ( opts ) {
6187 opts = opts || { } ;
6288 opts . key = opts . key || 'version' ;
@@ -67,13 +93,13 @@ function setDefaultOptions(opts) {
6793 }
6894 // if passed specific version - validate it
6995 if ( opts . version && ! semver . valid ( opts . version , opts . type ) ) {
70- gutil . log ( 'invalid version used as option' , gutil . colors . red ( opts . version ) ) ;
96+ log ( 'invalid version used as option' , log . colors . red ( opts . version ) ) ;
7197 opts . version = null ;
7298 }
7399 return opts ;
74100}
75101
76- // Preserver new line at the end of a file
102+ // Preserve new line at the end of a file
77103function possibleNewline ( json ) {
78104 var lastChar = ( json . slice ( - 1 ) === '\n' ) ? '\n' : '' ;
79105 return lastChar ;
0 commit comments