Skip to content

Commit 041a153

Browse files
committed
Refactoring of the promises, to return a promise from the .release() function
1 parent 7ddbef1 commit 041a153

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ There are optional arguments such as:
2828

2929
- `--draft=true` To set the release as a draft. Default: `false`
3030
- `--prerelease=true` To set the release as a prerelease. Default: `false`
31-
- `--prefix=v` Add a prefix to the tag version `e.g. v1.0.1`
31+
- `--prefix=v` Add a prefix to the tag version `e.g. v1.0.1`

src/index.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ function commitMessages(commits) {
5858
* Creates the options to make the release
5959
*
6060
* @param {GithubReleaseNotes} gren The gren object
61-
* @param {Array} tags The collection of tags
62-
* @param {Array} commitMessages The commit messages to create the release body
61+
* @param {Object[]} tags The collection of tags
62+
* @param {string[]} commitMessages The commit messages to create the release body
6363
*/
6464
function prepareRelease(gren, tags, commitMessages) {
65-
var body = commitMessages.slice(0, -1).filter(function (message) {
66-
return !message.match(/^merge/i);
67-
}).map(createBody).join('\n');
65+
var body = commitMessages
66+
.slice(0, -1)
67+
.filter(function (message) {
68+
return !message.match(/^merge/i);
69+
})
70+
.map(createBody)
71+
.join('\n');
6872

6973
var releaseOptions = {
7074
tag_name: tags[0].name,
@@ -93,7 +97,6 @@ function getCommitsBetweenTwo(gren, since, until) {
9397
};
9498

9599
return new Promise(function (resolve, reject) {
96-
97100
gren.repo.getCommits(options, function (err, commits) {
98101
if(err) {
99102
reject(err);
@@ -133,17 +136,17 @@ function getTagDates(gren, lastTag, lastRelease) {
133136
*
134137
* @return {Promise}
135138
*/
136-
function getLastTag(gren, releaseTagName) {
139+
function getLastTags(gren, releaseTagName) {
137140
return new Promise(function (resolve, reject) {
138141
gren.repo.listTags(function (err, tags) {
139142
if(err) {
140143
reject(err);
141144
} else {
142-
resolve(
143-
tags.filter(function(tag, index) {
144-
return (index === 0 || tag.name === releaseTagName);
145-
})
146-
);
145+
var filteredTags = tags.filter(function(tag, index) {
146+
return index === 0 || tag.name === releaseTagName;
147+
});
148+
149+
resolve(filteredTags);
147150
}
148151
});
149152
});
@@ -179,7 +182,9 @@ function getOptions(args) {
179182
var settings = {};
180183

181184
for(var i=2;i<args.length;i++) {
182-
settings[args[i].split('=')[0].replace('--', '')] = args[i].split('=')[1];
185+
var paramArray = args[i].split('=');
186+
187+
settings[paramArray[0].replace('--', '')] = paramArray[1];
183188
}
184189

185190
return settings;
@@ -191,11 +196,11 @@ function getOptions(args) {
191196
* @constructor
192197
*/
193198
function GithubReleaseNotes(options) {
194-
this.options = getOptions(process.argv);
199+
this.options = options || getOptions(process.argv);
195200

196201
var github = new Github({
197202
token: this.options.token,
198-
auth: "oauth"
203+
auth: 'oauth'
199204
});
200205

201206
this.repo = github.getRepo(this.options.username, this.options.repo);
@@ -207,21 +212,26 @@ function GithubReleaseNotes(options) {
207212
GithubReleaseNotes.prototype.release = function() {
208213
var that = this;
209214

210-
getLatestRelease(that).then(function (releaseTagName) {
211-
getLastTag(that, releaseTagName).then(function (tags) {
215+
getLatestRelease(this)
216+
.then(function (releaseTagName) {
217+
return getLastTags(that, releaseTagName);
218+
})
219+
.then(function (tags) {
212220
if(tags.length === 1) {
213-
console.error('The latest tag is the latest release!');
214-
return;
221+
throw new Error('The latest tag is the latest release!');
215222
}
216223

217-
Promise.all(getTagDates(that, tags[0], tags[1]))
218-
.then(function (data) {
219-
getCommitsBetweenTwo(that, data[1], data[0]).then(function(commitMessages) {
220-
prepareRelease(that, tags, commitMessages);
221-
});
222-
});
223-
});
224-
});
224+
return Promise.all(getTagDates(that, tags[0], tags[1]));
225+
})
226+
.then(function (data) {
227+
return getCommitsBetweenTwo(that, data[1], data[0]);
228+
})
229+
.then(function (commitMessages) {
230+
prepareRelease(that, tags, commitMessages);
231+
})
232+
.catch(function (error) {
233+
console.error(error);
234+
});
225235
};
226236

227237
module.exports = GithubReleaseNotes;

0 commit comments

Comments
 (0)