Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 4 additions & 33 deletions lib/tldr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const sample = require('lodash/sample');
const fs = require('fs-extra');
const ms = require('ms');
const ora = require('ora');
const { EmptyCacheError, MissingPageError, MissingRenderPathError } = require('./errors');
const Cache = require('./cache');
const search = require('./search');
Expand Down Expand Up @@ -140,19 +139,15 @@ class Tldr {
* @returns {Promise<any>} The index.
*/
updateCache() {
return spinningPromise('Updating...', () => {
return this.cache.update();
});
return this.cache.update();
}

/**
* Update the index.
* @returns {Promise<any>} The index.
*/
updateIndex() {
return spinningPromise('Creating index...', () => {
return search.createIndex();
});
return search.createIndex();
}

/**
Expand Down Expand Up @@ -204,13 +199,9 @@ class Tldr {
if (this.config.skipUpdateWhenPageNotFound === true) {
return '';
}
return spinningPromise('Page not found. Updating cache...', () => {
return this.cache.update();
})
return this.cache.update()
.then(() => {
return spinningPromise('Creating index...', () => {
return search.createIndex();
});
return search.createIndex();
})
.then(() => {
// And then, try to check in cache again
Expand Down Expand Up @@ -263,24 +254,4 @@ class Tldr {
}
}

/**
* Display a spinner while a task is running.
* @param {string} text The text of the spinner.
* @param {Function} factory A task to be run.
* @returns {Promise<unknown>} A promise with the result of the task.
*/
function spinningPromise(text, factory) {
const spinner = ora();
spinner.start(text);
return factory()
.then((val) => {
spinner.succeed();
return val;
})
.catch((err) => {
spinner.fail();
throw err;
});
}

module.exports = Tldr;
Loading