Skip to content

Commit

Permalink
Use ora as task loader and update the code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcanessa committed Oct 16, 2017
1 parent 21bda67 commit 3d9427b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 68 deletions.
70 changes: 27 additions & 43 deletions lib/src/Gren.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,9 @@ class Gren {
const loaded = utils.task(this, 'Updating latest release');
const { data: release } = await this.repo.updateRelease(releaseId, releaseOptions);

loaded();
loaded(chalk.green(`${release.name} has been successfully updated!`));

console.log(chalk.green(`\n${release.name} has been successfully updated!`));
console.log(chalk.blue(`See the results here: ${release.html_url}`));
console.log(chalk.blue(`\nSee the results here: ${release.html_url}`));

return release;
}
Expand All @@ -189,9 +188,8 @@ class Gren {
const loaded = utils.task(this, 'Preparing the release');
const { data: release } = await this.repo.createRelease(releaseOptions);

loaded();
loaded(chalk.green(`\n${release.name} has been successfully created!`));

console.log(chalk.green(`\n${release.name} has been successfully created!`));
console.log(chalk.blue(`See the results here: ${release.html_url}`));

return release;
Expand Down Expand Up @@ -383,15 +381,13 @@ class Gren {
page
});

loaded();

const totalPages = this._getLastPage(link);

if (this.options.tags.indexOf('all') >= 0 && totalPages && +page < totalPages) {
return this._getListReleases(page + 1).then(moreReleases => moreReleases.concat(releases));
}

process.stdout.write(releases.length + ' releases found\n');
loaded(`Releases found: ${releases.length}`);

return releases;
}
Expand Down Expand Up @@ -590,8 +586,6 @@ class Gren {
* @return {Promise} The promise which resolves the [Array] commit messages
*/
async _getCommitsBetweenTwo(since, until) {
process.stdout.write(chalk.green('Get commits between ' + utils.formatDate(new Date(since)) + ' and ' + utils.formatDate(new Date(until)) + '\n'));

const options = {
since: since,
until: until,
Expand All @@ -613,12 +607,16 @@ class Gren {
*
* @return {Promise[]}
*/
_getCommitBlocks(releaseRanges) {
console.log(chalk.blue('\nCreating the body blocks from commits:'));
async _getCommitBlocks(releaseRanges) {
const taskName = 'Creating the body blocks from commits';
const loaded = utils.task(this, taskName);

return Promise.all(
const ranges = await Promise.all(
releaseRanges
.map(async range => {
const [{ date: since }, { date: until }] = range;

this.tasks[taskName].text = `Get commits between ${utils.formatDate(new Date(since))} and ${utils.formatDate(new Date(until))}`;
const commits = await this._getCommitsBetweenTwo(range[1].date, range[0].date);

return {
Expand All @@ -630,6 +628,10 @@ class Gren {
};
})
);

loaded(`Commit ranges loaded: ${ranges.length}`);

return ranges;
}

/**
Expand Down Expand Up @@ -666,16 +668,20 @@ class Gren {
* @return {Promise} The promise which resolves the list of the issues
*/
async _getClosedIssues(releaseRanges) {
const loaded = utils.task(this, 'Getting all closed issues');

const { data } = await this.issues.listIssues({
const type = {
issues: 'Issues',
milestones: 'Issues',
prs: 'Pull Requests'
}[this.options.dataSource];
const loaded = utils.task(this, `Getting all closed ${type}`);
const { data: issues } = await this.issues.listIssues({
state: 'closed',
since: releaseRanges[releaseRanges.length - 1][1].date
});

loaded();
loaded(`${type} found: ${issues.length}`);

return data;
return issues;
}

/**
Expand Down Expand Up @@ -739,7 +745,6 @@ class Gren {
}

const allLabels = Object.values(groupBy).reduce((carry, group) => carry.concat(group), []);

const groups = Object.keys(groupBy).reduce((carry, group) => {
const groupIssues = issues.filter(issue => {
if (!issue.labels.length && this.options.template.noLabel) {
Expand Down Expand Up @@ -815,24 +820,14 @@ class Gren {
* @return {Promise[]}
*/
async _getIssueBlocks(releaseRanges) {
console.log('Creating the body blocks from releases:');

let totalIssues = 0;
const issues = await this._getClosedIssues(releaseRanges);
const type = {
'issues': 'issues',
'prs': 'pull requests',
'milestones': 'issues'
}[this.options.dataSource];
const release = releaseRanges
.map(range => {
const filteredIssues = Array.from(issues)
.filter(this._filterIssue.bind(this))
.filter(this._filterBlockIssue.bind(this, range));
const body = (!range[0].body || this.options.override) && this._groupBy(filteredIssues);

totalIssues += filteredIssues.length;

return {
id: range[0].id,
release: range[0].name,
Expand All @@ -842,8 +837,6 @@ class Gren {
};
});

process.stdout.write(`${totalIssues} ${type} found.\n`);

return release;
}

Expand Down Expand Up @@ -897,29 +890,20 @@ class Gren {
* @return {Promise} Resolving the release blocks
*/
async _getReleaseBlocks() {
let loaded;
const loaded = utils.task(this, 'Getting releases');
const dataSource = {
issues: this._getIssueBlocks.bind(this),
commits: this._getCommitBlocks.bind(this),
milestones: this._getIssueBlocks.bind(this),
prs: this._getIssueBlocks.bind(this)
};

const releases = await this._getListReleases();

loaded = utils.task(this, 'Getting tags');
this.tasks['Getting releases'].text = 'Getting tags';

const tags = await this._getLastTags(releases.length ? releases : false);

loaded();

console.log('Tags found: ' + tags.map(({ tag: { name } }) => name).join(', '));

loaded = utils.task(this, 'Getting the tag dates ranges');

const releaseDates = await Promise.all(this._getTagDates(tags));

loaded();
loaded(`Tags found: ${tags.map(({ tag: { name } }) => name).join(', ')}`);

return dataSource[this.options.dataSource](
this._createReleaseRanges(releaseDates)
Expand Down
21 changes: 7 additions & 14 deletions lib/src/_utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const chalk = require('chalk');
const fs = require('fs');
const ora = require('ora');
require('require-yaml');

/**
Expand Down Expand Up @@ -44,21 +45,13 @@ function printTask(name) {
* @return {Function} The function to be fired when is loaded
*/// istanbul ignore next
function task(gren, taskName) {
const time = process.hrtime();
process.stdout.write(`\n${chalk.green(taskName)} : .`);
const spinner = ora(taskName);
gren.tasks[taskName] = spinner;

gren.tasks[taskName] = setInterval(() => {
process.stdout.write('.');
}, 100);
spinner.start();

return function(message) {
const diff = process.hrtime(time);
const seconds = ((diff[0] * 1e9 + diff[1]) * 1e-9).toFixed(2);

process.stdout.write(message || '' + chalk.yellow(` (${seconds} secs)\n`));
clearInterval(gren.tasks[taskName]);

gren.tasks[taskName] = seconds;
return message => {
spinner.succeed(message);
};
}

Expand All @@ -76,7 +69,7 @@ function clearTasks(gren) {
}

Object.keys(gren.tasks).forEach((taskName) => {
clearInterval(gren.tasks[taskName]);
gren.tasks[taskName].stop();
});

process.stdout.write(chalk.red('\nTask(s) stopped because of the following error:\n'));
Expand Down
86 changes: 75 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"github-api": "^3.0.0",
"minimist": "^1.2.0",
"object-assign-deep": "^0.3.1",
"ora": "^1.3.0",
"require-yaml": "0.0.1"
},
"devDependencies": {
Expand Down

0 comments on commit 3d9427b

Please sign in to comment.