-
Notifications
You must be signed in to change notification settings - Fork 7
/
bumpup.js
169 lines (151 loc) · 4.59 KB
/
bumpup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* grunt-bumpup
* https://github.com/Darsain/grunt-bumpup
*
* Licensed under the MIT license.
* http://www.opensource.org/licenses/MIT
*/
'use strict';
var semver = require('semver');
var moment = require('moment');
/**
* Detect and return the indentation.
*
* @param {String} string
*
* @return {Mixed} Indentation used, or undefined.
*/
function detectIndentation(string) {
var tabs = string.match(/^[\t]+/gm) || [];
var spaces = string.match(/^[ ]+/gm) || [];
// Pick the smallest indentation level of a prevalent type
var prevalent = tabs.length >= spaces.length ? tabs : spaces;
var indentation;
for (var i = 0, il = prevalent.length; i < il; i++) {
if (!indentation || prevalent[i].length < indentation.length) {
indentation = prevalent[i];
}
}
return indentation;
}
/**
* Return type of the value.
*
* @param {Mixed} value
*
* @return {String}
*/
function type(value) {
return Object.prototype.toString.call(value).match(/^\[object ([a-z]+)\]$/i)[1].toLowerCase();
}
// Export the module
module.exports = function(grunt) {
// Error handler
function failed(error, message) {
if (error) {
grunt.log.error(error);
}
grunt.fail.warn(message || 'Tagrelease task failed.');
}
// Task definition
grunt.registerTask('bumpup', 'Bumping up version & date properties.', function (releaseType, buildMeta) {
// Normalize the release type
if (typeof releaseType === 'string') {
releaseType = releaseType.toLowerCase();
if (!/^(major|minor|patch|prerelease|premajor|preminor|prepatch|build)$/i.test(releaseType) && !semver.valid(releaseType)) {
failed(null, '"' + releaseType + '" is not a valid release type, or a semantic version.');
return;
}
} else {
releaseType = 'patch';
}
// Get configuration and set the options
var taskConfig = grunt.config('bumpup');
var config = ['string', 'array'].indexOf(type(taskConfig)) !== -1 ? { files: taskConfig } : taskConfig;
var files = type(config.files) === 'array' ? config.files : [config.file || config.files];
var o = grunt.util._.extend({
dateformat: 'YYYY-MM-DD HH:mm:ss Z',
normalize: true,
newlineEof: false,
updateProps: {}
}, config.options || {});
var norm = {};
if (!files.length) {
grunt.log.warn('Nothing to bump up.');
return;
}
// Create an object of property setters
var setters = {
version: function (old, type, o, buildMeta) {
if (semver.valid(type)) {
return type;
}
var oldVersion = semver.valid(old);
if (!oldVersion) {
grunt.log.warn('Version "' + old + '" is not a valid semantic version.');
return;
}
var newVersion = type !== 'build' ? semver.inc(oldVersion, type) : oldVersion;
if (buildMeta) {
newVersion += '+' + buildMeta;
}
return newVersion;
},
date: function (old, type, o) {
return moment.utc().format(o.dateformat);
}
};
Object.keys(config.setters || {}).forEach(function (key) {
setters[key] = config.setters[key];
});
// Flip updateProps map for easier usage
var updatePropsMap = {};
Object.keys(o.updateProps).forEach(function (key) {
updatePropsMap[o.updateProps[key]] = key;
});
// Bumpup the files
files.filter(function (filepath) {
// Remove nonexistent files.
if (!grunt.file.exists(filepath)) {
grunt.log.warn('File "' + filepath.cyan + '" not found.');
return false;
} else {
return true;
}
}).forEach(function (filepath) {
try {
var file = grunt.file.read(filepath);
var meta = JSON.parse(file);
var indentation = detectIndentation(file);
grunt.log.verbose.writeln('Bumping "' + filepath.cyan + '":');
// Update properties with defined setters
Object.keys(setters).forEach(function (key) {
if (!Object.prototype.hasOwnProperty.call(meta, key)) {
return;
}
var newValue;
if (o.normalize && norm[key] != null) {
newValue = norm[key];
} else {
norm[key] = newValue = setters[key](meta[key], releaseType, o, buildMeta);
}
if (newValue != null) {
meta[key] = newValue;
grunt.log.verbose.writeln(grunt.util.repeat(Math.max(16 - key.length, 0), ' ') + key + ' : ' + newValue);
}
});
// Stringify new metafile and save
if (!grunt.file.write(filepath, JSON.stringify(meta, null, indentation) + (o.newlineEof ? "\n" : ''))) {
grunt.log.warn('Couldn\'t write to "' + filepath + '"');
}
// Update config property
if (updatePropsMap[filepath]) {
grunt.config.set(updatePropsMap[filepath], meta);
}
} catch (error) {
failed(error, 'Bumpup failed.');
}
}, this);
grunt.log.writeln('Bumped to: ' + norm.version);
});
};