Skip to content

Commit 3d9427b

Browse files
committed
Use ora as task loader and update the code
1 parent 21bda67 commit 3d9427b

File tree

4 files changed

+110
-68
lines changed

4 files changed

+110
-68
lines changed

lib/src/Gren.js

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@ class Gren {
159159
const loaded = utils.task(this, 'Updating latest release');
160160
const { data: release } = await this.repo.updateRelease(releaseId, releaseOptions);
161161

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

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

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

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

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

197195
return release;
@@ -383,15 +381,13 @@ class Gren {
383381
page
384382
});
385383

386-
loaded();
387-
388384
const totalPages = this._getLastPage(link);
389385

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

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

396392
return releases;
397393
}
@@ -590,8 +586,6 @@ class Gren {
590586
* @return {Promise} The promise which resolves the [Array] commit messages
591587
*/
592588
async _getCommitsBetweenTwo(since, until) {
593-
process.stdout.write(chalk.green('Get commits between ' + utils.formatDate(new Date(since)) + ' and ' + utils.formatDate(new Date(until)) + '\n'));
594-
595589
const options = {
596590
since: since,
597591
until: until,
@@ -613,12 +607,16 @@ class Gren {
613607
*
614608
* @return {Promise[]}
615609
*/
616-
_getCommitBlocks(releaseRanges) {
617-
console.log(chalk.blue('\nCreating the body blocks from commits:'));
610+
async _getCommitBlocks(releaseRanges) {
611+
const taskName = 'Creating the body blocks from commits';
612+
const loaded = utils.task(this, taskName);
618613

619-
return Promise.all(
614+
const ranges = await Promise.all(
620615
releaseRanges
621616
.map(async range => {
617+
const [{ date: since }, { date: until }] = range;
618+
619+
this.tasks[taskName].text = `Get commits between ${utils.formatDate(new Date(since))} and ${utils.formatDate(new Date(until))}`;
622620
const commits = await this._getCommitsBetweenTwo(range[1].date, range[0].date);
623621

624622
return {
@@ -630,6 +628,10 @@ class Gren {
630628
};
631629
})
632630
);
631+
632+
loaded(`Commit ranges loaded: ${ranges.length}`);
633+
634+
return ranges;
633635
}
634636

635637
/**
@@ -666,16 +668,20 @@ class Gren {
666668
* @return {Promise} The promise which resolves the list of the issues
667669
*/
668670
async _getClosedIssues(releaseRanges) {
669-
const loaded = utils.task(this, 'Getting all closed issues');
670-
671-
const { data } = await this.issues.listIssues({
671+
const type = {
672+
issues: 'Issues',
673+
milestones: 'Issues',
674+
prs: 'Pull Requests'
675+
}[this.options.dataSource];
676+
const loaded = utils.task(this, `Getting all closed ${type}`);
677+
const { data: issues } = await this.issues.listIssues({
672678
state: 'closed',
673679
since: releaseRanges[releaseRanges.length - 1][1].date
674680
});
675681

676-
loaded();
682+
loaded(`${type} found: ${issues.length}`);
677683

678-
return data;
684+
return issues;
679685
}
680686

681687
/**
@@ -739,7 +745,6 @@ class Gren {
739745
}
740746

741747
const allLabels = Object.values(groupBy).reduce((carry, group) => carry.concat(group), []);
742-
743748
const groups = Object.keys(groupBy).reduce((carry, group) => {
744749
const groupIssues = issues.filter(issue => {
745750
if (!issue.labels.length && this.options.template.noLabel) {
@@ -815,24 +820,14 @@ class Gren {
815820
* @return {Promise[]}
816821
*/
817822
async _getIssueBlocks(releaseRanges) {
818-
console.log('Creating the body blocks from releases:');
819-
820-
let totalIssues = 0;
821823
const issues = await this._getClosedIssues(releaseRanges);
822-
const type = {
823-
'issues': 'issues',
824-
'prs': 'pull requests',
825-
'milestones': 'issues'
826-
}[this.options.dataSource];
827824
const release = releaseRanges
828825
.map(range => {
829826
const filteredIssues = Array.from(issues)
830827
.filter(this._filterIssue.bind(this))
831828
.filter(this._filterBlockIssue.bind(this, range));
832829
const body = (!range[0].body || this.options.override) && this._groupBy(filteredIssues);
833830

834-
totalIssues += filteredIssues.length;
835-
836831
return {
837832
id: range[0].id,
838833
release: range[0].name,
@@ -842,8 +837,6 @@ class Gren {
842837
};
843838
});
844839

845-
process.stdout.write(`${totalIssues} ${type} found.\n`);
846-
847840
return release;
848841
}
849842

@@ -897,29 +890,20 @@ class Gren {
897890
* @return {Promise} Resolving the release blocks
898891
*/
899892
async _getReleaseBlocks() {
900-
let loaded;
893+
const loaded = utils.task(this, 'Getting releases');
901894
const dataSource = {
902895
issues: this._getIssueBlocks.bind(this),
903896
commits: this._getCommitBlocks.bind(this),
904897
milestones: this._getIssueBlocks.bind(this),
905898
prs: this._getIssueBlocks.bind(this)
906899
};
907-
908900
const releases = await this._getListReleases();
909-
910-
loaded = utils.task(this, 'Getting tags');
901+
this.tasks['Getting releases'].text = 'Getting tags';
911902

912903
const tags = await this._getLastTags(releases.length ? releases : false);
913-
914-
loaded();
915-
916-
console.log('Tags found: ' + tags.map(({ tag: { name } }) => name).join(', '));
917-
918-
loaded = utils.task(this, 'Getting the tag dates ranges');
919-
920904
const releaseDates = await Promise.all(this._getTagDates(tags));
921905

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

924908
return dataSource[this.options.dataSource](
925909
this._createReleaseRanges(releaseDates)

lib/src/_utils.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const chalk = require('chalk');
22
const fs = require('fs');
3+
const ora = require('ora');
34
require('require-yaml');
45

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

50-
gren.tasks[taskName] = setInterval(() => {
51-
process.stdout.write('.');
52-
}, 100);
51+
spinner.start();
5352

54-
return function(message) {
55-
const diff = process.hrtime(time);
56-
const seconds = ((diff[0] * 1e9 + diff[1]) * 1e-9).toFixed(2);
57-
58-
process.stdout.write(message || '' + chalk.yellow(` (${seconds} secs)\n`));
59-
clearInterval(gren.tasks[taskName]);
60-
61-
gren.tasks[taskName] = seconds;
53+
return message => {
54+
spinner.succeed(message);
6255
};
6356
}
6457

@@ -76,7 +69,7 @@ function clearTasks(gren) {
7669
}
7770

7871
Object.keys(gren.tasks).forEach((taskName) => {
79-
clearInterval(gren.tasks[taskName]);
72+
gren.tasks[taskName].stop();
8073
});
8174

8275
process.stdout.write(chalk.red('\nTask(s) stopped because of the following error:\n'));

package-lock.json

Lines changed: 75 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"github-api": "^3.0.0",
5151
"minimist": "^1.2.0",
5252
"object-assign-deep": "^0.3.1",
53+
"ora": "^1.3.0",
5354
"require-yaml": "0.0.1"
5455
},
5556
"devDependencies": {

0 commit comments

Comments
 (0)