-
-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add jsdocs to tldr and cache #355
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,10 +16,19 @@ class Cache { | |||||
this.cacheFolder = path.join(config.cache, 'cache'); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Fetch stats from the cache folder. | ||||||
* @returns {Promise<any>} A promise with the stats of the cache folder. | ||||||
*/ | ||||||
lastUpdated() { | ||||||
return fs.stat(this.cacheFolder); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Fetch a page from cache using preferred language and preferred platform. | ||||||
* @param page {} A | ||||||
* @returns {Promise<unknown>} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between |
||||||
*/ | ||||||
getPage(page) { | ||||||
let preferredPlatform = platform.getPreferredPlatformFolder(this.config); | ||||||
const preferredLanguage = process.env.LANG || 'en'; | ||||||
|
@@ -36,10 +45,18 @@ class Cache { | |||||
}); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Clean the cache folder. | ||||||
* @returns {Promise<any>} A promise when the remove is completed. | ||||||
*/ | ||||||
clear() { | ||||||
return fs.remove(this.cacheFolder); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Update the cache folder and returns the English entries. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @returns {Promise<any>} A promise with the index. | ||||||
*/ | ||||||
update() { | ||||||
// Temporary folder path: /tmp/tldr/{randomName} | ||||||
const tempFolder = path.join(os.tmpdir(), 'tldr', utils.uniqueId()); | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -91,17 +91,24 @@ function hasLang(targets, preferredLanguage) { | |||||
}); | ||||||
} | ||||||
|
||||||
// hasPage is always called after the index is created, | ||||||
// hence just return the variable in memory. | ||||||
// There is no need to re-read the index file again. | ||||||
/** | ||||||
* Check if a page is in the index. | ||||||
* @returns {boolean} A boolean that indicates if the page is present. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
function hasPage(page) { | ||||||
// hasPage is always called after the index is created, | ||||||
// hence just return the variable in memory. | ||||||
// There is no need to re-read the index file again. | ||||||
if (!shortIndex) { | ||||||
return false; | ||||||
} | ||||||
return page in shortIndex; | ||||||
} | ||||||
|
||||||
// Return all commands available in the local cache. | ||||||
/** | ||||||
* Return all commands available in the local cache. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @returns {Promise<string[]>} A promise with the commands from cache. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
function commands() { | ||||||
return getShortIndex().then((idx) => { | ||||||
return Object.keys(idx).sort(); | ||||||
|
@@ -110,6 +117,13 @@ function commands() { | |||||
|
||||||
// Return all commands for a given platform. | ||||||
// P.S. - The platform 'common' is always included. | ||||||
Comment on lines
118
to
119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove this then? Seems repeated. |
||||||
/** | ||||||
* Return all commands for a given platform. | ||||||
* | ||||||
* The 'common' platform is always included. | ||||||
* @param platform {string} The platform. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @returns {Promise<string[]>} A promise with the commands for a given platform. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think we need to specify that it is a promise again |
||||||
*/ | ||||||
function commandsFor(platform) { | ||||||
return getShortIndex() | ||||||
.then((idx) => { | ||||||
|
@@ -124,7 +138,11 @@ function commandsFor(platform) { | |||||
}); | ||||||
} | ||||||
|
||||||
// Delete the index file. | ||||||
/** | ||||||
* Delete the index file. | ||||||
* | ||||||
* @returns {Promise<any>} A promise when the remove is completed. | ||||||
*/ | ||||||
function clearPagesIndex() { | ||||||
return fs.unlink(shortIndexFile) | ||||||
.then(() => { | ||||||
|
@@ -139,7 +157,9 @@ function clearPagesIndex() { | |||||
}); | ||||||
} | ||||||
|
||||||
// Set the shortIndex variable to null. | ||||||
/** | ||||||
* Set the shortIndex variable to null. | ||||||
*/ | ||||||
function clearRuntimeIndex() { | ||||||
shortIndex = null; | ||||||
} | ||||||
|
@@ -150,18 +170,26 @@ function rebuildPagesIndex() { | |||||
}); | ||||||
} | ||||||
|
||||||
// If the variable is not set, read the file and set it. | ||||||
// Else, just return the variable. | ||||||
/** | ||||||
* Return the index. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* If the index is not loaded, read the file and load it. | ||||||
* @returns {Promise<any>} The index entries. | ||||||
*/ | ||||||
function getShortIndex() { | ||||||
if (shortIndex) { | ||||||
return Promise.resolve(shortIndex); | ||||||
} | ||||||
return readShortPagesIndex(); | ||||||
} | ||||||
|
||||||
// Read the index file, and load it into memory. | ||||||
// If the file does not exist, create the data structure, write the file, | ||||||
// and load it into memory. | ||||||
/** | ||||||
* Read the index file, and load it into memory. | ||||||
* | ||||||
* If the file does not exist, create the data structure, write the file, | ||||||
* and load it into memory. | ||||||
* @returns {Promise<any>} The index entries. | ||||||
*/ | ||||||
function readShortPagesIndex() { | ||||||
return fs.readJson(shortIndexFile) | ||||||
.then((idx) => { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,11 @@ class Tldr { | |||||||||
this.cache = new Cache(this.config); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print pages for a given platform.. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiple periods at the end of the sentence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @param singleColumn {boolean} A boolean to print one command per line. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
list(singleColumn) { | ||||||||||
let os = platform.getPreferredPlatformFolder(this.config); | ||||||||||
return index.commandsFor(os) | ||||||||||
|
@@ -29,17 +34,33 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print all pages in the cache. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @param singleColumn {boolean} A boolean to print one command per line. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
IMO "a boolean to" is redundant. |
||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
listAll(singleColumn) { | ||||||||||
return index.commands() | ||||||||||
.then((commands) => { | ||||||||||
return this.printPages(commands, singleColumn); | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print a command page. | ||||||||||
* @param commands {string[]} A given command to be printed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "A given command" is incorrect when we are passing an array. Let's change this to plural. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @param options {object} The options for the render. | ||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
get(commands, options) { | ||||||||||
return this.printBestPage(commands.join('-'), options); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print a random page for the current platform. | ||||||||||
* @param options {object} The options for the render. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
random(options) { | ||||||||||
let os = platform.getPreferredPlatformFolder(this.config); | ||||||||||
return index.commandsFor(os) | ||||||||||
|
@@ -56,6 +77,10 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print a random page. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @returns {Promise<void>} A promise when the opration is completed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
*/ | ||||||||||
randomExample() { | ||||||||||
let os = platform.getPreferredPlatformFolder(this.config); | ||||||||||
return index.commandsFor(os) | ||||||||||
|
@@ -72,6 +97,11 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print a markdown file. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @param file {string} The path to the file. | ||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
render(file) { | ||||||||||
return fs.readFile(file, 'utf8') | ||||||||||
.then((content) => { | ||||||||||
|
@@ -82,24 +112,41 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Clear the cache folder. | ||||||||||
* @returns {Promise<void>} A promise when the cache is deleted. | ||||||||||
*/ | ||||||||||
clearCache() { | ||||||||||
return this.cache.clear().then(() => { | ||||||||||
console.log('Done'); | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Update the cache. | ||||||||||
* @returns {Promise<any>} A promise with the index. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be "A promise with the cache"? |
||||||||||
*/ | ||||||||||
updateCache() { | ||||||||||
return spinningPromise('Updating...', () => { | ||||||||||
return this.cache.update(); | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Update the index. | ||||||||||
* @returns {Promise<any>} A promise with the index. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
*/ | ||||||||||
updateIndex() { | ||||||||||
return spinningPromise('Creating index...', () => { | ||||||||||
return search.createIndex(); | ||||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Search some keywords in the index and print the results. | ||||||||||
* @param keywords {string[]} The given keywords. | ||||||||||
* @returns {Promise<any>} A promise when the operation is completed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This promise doesn't have any value. |
||||||||||
*/ | ||||||||||
search(keywords) { | ||||||||||
return search.getResults(keywords.join(' ')) | ||||||||||
.then((results) => { | ||||||||||
|
@@ -108,6 +155,12 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print all pages. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
* @param pages {string[]} A list of pages to be printed. | ||||||||||
* @param singleColumn {boolean} A boolean to print one command per line. | ||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
printPages(pages, singleColumn) { | ||||||||||
if (pages.length === 0) { | ||||||||||
throw new EmptyCacheError(); | ||||||||||
|
@@ -120,7 +173,15 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
printBestPage(command, options={}) { | ||||||||||
/** | ||||||||||
* Print a page from the cache. | ||||||||||
* | ||||||||||
* If the page is not present, the cache is updated. | ||||||||||
* @param command {string} The given command to be printed. | ||||||||||
* @param options {object} The options for the render. | ||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
printBestPage(command, options= {}) { | ||||||||||
// Trying to get the page from cache first | ||||||||||
return this.cache.getPage(command) | ||||||||||
.then((content) => { | ||||||||||
|
@@ -153,6 +214,10 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Print a warning message if the cache is 30 days old. | ||||||||||
* @returns {Promise<void>} A promise when the operation is completed. | ||||||||||
*/ | ||||||||||
checkStale() { | ||||||||||
return this.cache.lastUpdated() | ||||||||||
.then((stats) => { | ||||||||||
|
@@ -162,7 +227,12 @@ class Tldr { | |||||||||
}); | ||||||||||
} | ||||||||||
|
||||||||||
renderContent(content, options={}) { | ||||||||||
/** | ||||||||||
* Print the page content. | ||||||||||
* @param content {string} The content of a page. | ||||||||||
* @param options {object<{markdown: boolean, randomExample: boolean}>} The options for the render. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
*/ | ||||||||||
renderContent(content, options= {}) { | ||||||||||
if (options.markdown) { | ||||||||||
return console.log(content); | ||||||||||
} | ||||||||||
|
@@ -177,6 +247,12 @@ class Tldr { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* Display a spinner while a task is running. | ||||||||||
* @param text {string} The text of the spinner. | ||||||||||
* @param factory {Function} A task to be run. | ||||||||||
* @returns {Promise} A promise with the result of the task. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Any result can be returned here, because any function (task) can be passed. |
||||||||||
*/ | ||||||||||
function spinningPromise(text, factory) { | ||||||||||
const spinner = ora(); | ||||||||||
spinner.start(text); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.