UNPKG

3.54 kBtext/coffeescriptView Raw
1'use strict'
2
3path = require 'path'
4async = require 'async'
5semver = require 'semver'
6util = require './Bumped.util'
7DEFAULT = require './Bumped.default'
8MSG = require './Bumped.messages'
9Animation = require './Bumped.animation'
10
11module.exports = class Semver
12
13 constructor: (bumped) ->
14 @bumped = bumped
15
16 ###*
17 * Get the high project version across config files declared.
18 * @return {[type]} [description]
19 ###
20 sync: =>
21 [opts, cb] = DEFAULT.args arguments
22 async.compose(@max, @versions) (err, max) =>
23 @bumped._version = max
24 cb()
25
26 versions: (cb) =>
27 async.reduce @bumped.config.rc.files, [], (accumulator, file, next) ->
28 version = require(path.resolve file).version
29 accumulator.push(version) if version?
30 next(null, accumulator)
31 , cb
32
33 max: (versions, cb) ->
34 initial = versions.shift()
35 async.reduce versions, initial, (max, version, next) ->
36 max = version if semver.gt version, max
37 next null, max
38 , cb
39
40 release: =>
41 [opts, cb] = DEFAULT.args arguments
42 return @bumped.logger.errorHandler MSG.NOT_VALID_VERSION(opts.version), cb unless opts.version
43
44 @bumped._version ?= '0.0.0'
45 semverStyle = @detect opts.version
46 releaseVersion = @releaseBasedOn semverStyle
47
48 tasks = [
49 (next) =>
50 opts.type = 'prerelease'
51 @bumped.plugin.exec opts, next
52 (next) ->
53 releaseVersion
54 version: opts.version
55 prefix: opts.prefix
56 , next
57 (newVersion, next) =>
58 @bumped._oldVersion = @bumped._version
59 @update version: newVersion, next
60 (next) =>
61 opts.type = 'postrelease'
62 @bumped.plugin.exec opts, next
63 ]
64
65 async.waterfall tasks, (err) =>
66 return @bumped.logger.errorHandler err, cb if err
67 cb null, @bumped._version
68
69 update: ->
70 [opts, cb] = DEFAULT.args arguments
71
72 @bumped._version = opts.version
73
74 async.forEachOf @bumped.config.rc.files, @save, (err) =>
75 return @bumped.logger.errorHandler err, cb if err
76
77 Animation.end
78 logger : @bumped.logger
79 version : @bumped._version
80
81 cb()
82
83 save: (file, index, cb) =>
84 util.updateJSON
85 filename : file
86 property : 'version'
87 value : @bumped._version
88 force : index is 0
89 , cb
90
91 ###*
92 * Print the current synchronized version.
93 ###
94 version: =>
95 [opts, cb] = DEFAULT.args arguments
96
97 if @bumped._version?
98 @bumped.logger.success MSG.CURRENT_VERSION @bumped._version
99 else
100 @bumped.logger.errorHandler MSG.NOT_CURRENT_VERSION(), lineBreak:false
101
102 return cb null, @bumped._version
103
104 detect: (word) ->
105 return 'semver' if util.includes DEFAULT.keywords.semver, word
106 return 'nature' if util.includes DEFAULT.keywords.nature, word
107 'numeric'
108
109 releaseBasedOn: (type) =>
110 return @_releasesBasedOnSemver if type is 'semver'
111 return @_releaseBasedOnNatureSemver if type is 'nature'
112 @_releasesBasedOnVersion
113
114 _releaseBasedOnNatureSemver: (opts, cb) =>
115 cb null, semver.inc(@bumped._version, DEFAULT.keywords.adapter[opts.version])
116
117 _releasesBasedOnSemver: (opts, cb) =>
118 cb null, semver.inc(@bumped._version, opts.version, opts.prefix)
119
120 _releasesBasedOnVersion: (opts, cb) =>
121 version = semver.clean opts.version
122 version = semver.valid version
123 return cb MSG.NOT_VALID_VERSION version unless version?
124 return cb MSG.NOT_GREATER_VERSION(version, @bumped._version) unless semver.gt version, @bumped._version
125 cb null, version