-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
229 lines (196 loc) · 5.13 KB
/
index.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var semver = require('semver'),
path = require('path'),
through = require('through2'),
fs = require('vinyl-fs'),
fUtil = require('./lib/files'),
git = require('./lib/git');
exports.get = function(callback) {
var result = fUtil.loadFiles();
var ret = {};
var errors = [];
return result
.on('data', function(file) {
try {
var contents = JSON.parse(file.contents.toString());
ret[path.basename(file.path)] = contents.version;
} catch (e) {
errors.push(file.relative + ': ' + e.message);
}
})
.on('end', function() {
if (errors.length) {
return callback(new Error(errors.join('\n')), ret);
}
return callback(null, ret);
});
};
exports.isPackageFile = fUtil.isPackageFile;
var versionAliases = (exports.versionAliases = {
pa: 'patch',
pr: 'prerelease',
ma: 'major',
mi: 'minor',
// one char might be controversial, but it saves key strokes
m: 'major',
p: 'patch',
i: 'minor'
});
var updateJSON = (exports.updateJSON = function(obj, ver) {
ver = ver.toString().toLowerCase();
// check for aliases
if (ver in versionAliases) {
ver = versionAliases[ver];
}
var validVer = semver.valid(ver);
obj = obj || {};
var currentVer = obj.version;
if (validVer === null) {
validVer = semver.inc(currentVer, ver);
}
if (validVer === null) {
return false;
}
obj.version = validVer;
return obj;
});
exports.update = function(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
if (typeof options === 'string') {
options = {
version: options,
noPrefix: false,
precommit: void 0,
commitMessage: void 0
};
}
if (!options.tagName) {
options.tagName = (options.noPrefix ? '' : 'v') + '%s';
}
var ver = options.version || 'minor';
var noPrefix = !!options.noPrefix;
var commitMessage = options.commitMessage || void 0;
var precommitCallback = options.precommit;
callback = callback || noop();
(function(done) {
if (commitMessage) {
return git.isRepositoryClean(done);
}
return done(null);
})(function(err) {
if (err) {
callback(err);
return void 0;
}
var files = [],
errors = [],
fileStream = fUtil.loadFiles(),
versionList = {},
updated = null,
hasSet = false;
var stored = fileStream
.pipe(
through.obj(function(file, e, next) {
if (file == null || file.isNull()) {
this.push(null);
next();
return;
}
var json = file.contents.toString(),
contents = null;
try {
contents = JSON.parse(json);
} catch (e) {
errors.push(new Error(file.relative + ': ' + e.message));
next();
return;
}
if (!hasSet) {
hasSet = true;
updated = updateJSON(contents, ver);
if (!updated) {
this.emit(
'error',
new Error(
'Version bump failed, ' + ver + ' is not valid version.'
)
);
return void 0;
}
}
contents.version = updated.version;
file.contents = Buffer.from(
JSON.stringify(contents, null, fUtil.space(json)) +
fUtil.getLastChar(json)
);
versionList[path.basename(file.path)] = updated.version;
this.push(file);
next();
})
)
.on('error', function(err) {
callback(err);
})
.pipe(fs.dest('./'));
stored.on('data', function(file) {
files.push(file.path);
});
stored.on('end', function() {
var errorMessage = null;
if (errors.length) {
errorMessage = errors
.map(function(e) {
return ' * ' + e.message;
})
.join('\n');
}
updated = updated || { version: 'N/A' };
var ret = {
newVersion: updated.version,
versions: versionList,
message: files
.map(function(file) {
return 'Updated ' + path.basename(file);
})
.join('\n'),
updatedFiles: files
};
if (!commitMessage || errorMessage) {
callback(errorMessage ? new Error(errorMessage) : null, ret);
return void 0;
}
if (!precommitCallback) {
return doCommit();
}
precommitCallback(function(err) {
if (err) {
return git.checkout();
}
doCommit();
});
function doCommit() {
var tagName = options.tagName
.replace('%s', updated.version)
.replace('"', '')
.replace("'", '');
git.commit(files, commitMessage, updated.version, tagName, function(
err
) {
if (err) {
callback(err, null);
return void 0;
}
ret.message += '\nCommited to git and created tag ' + tagName;
callback(null, ret);
});
}
});
});
return this;
};
function noop() {
return function() {};
}