Skip to content

Commit 9fd7b2f

Browse files
navarroaxelSethFalco
authored andcommitted
add jsdocs to tldr and cache
1 parent de3d41f commit 9fd7b2f

File tree

4 files changed

+141
-16
lines changed

4 files changed

+141
-16
lines changed

lib/cache.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ class Cache {
1414
this.cacheFolder = path.join(config.cache, 'cache');
1515
}
1616

17+
/**
18+
* Fetch stats from the cache folder.
19+
* @returns {Promise<any>} A promise with the stats of the cache folder.
20+
*/
1721
lastUpdated() {
1822
return fs.stat(this.cacheFolder);
1923
}
2024

25+
/**
26+
* Fetch a page from cache using preferred language and preferred platform.
27+
* @param page {} A
28+
* @returns {Promise<unknown>}
29+
*/
2130
getPage(page) {
2231
let preferredPlatform = platforms.getPreferredPlatformFolder(this.config);
2332
const preferredLanguage = process.env.LANG || 'en';
@@ -34,10 +43,18 @@ class Cache {
3443
});
3544
}
3645

46+
/**
47+
* Clean the cache folder.
48+
* @returns {Promise<any>} A promise when the remove is completed.
49+
*/
3750
clear() {
3851
return fs.remove(this.cacheFolder);
3952
}
4053

54+
/**
55+
* Update the cache folder and returns the English entries.
56+
* @returns {Promise<any>} A promise with the index.
57+
*/
4158
update() {
4259
// Temporary folder path: /tmp/tldr/{randomName}
4360
const tempFolder = path.join(os.tmpdir(), 'tldr', utils.uniqueId());

lib/index.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,24 @@ function hasLang(targets, preferredLanguage) {
9191
});
9292
}
9393

94-
// hasPage is always called after the index is created,
95-
// hence just return the variable in memory.
96-
// There is no need to re-read the index file again.
94+
/**
95+
* Check if a page is in the index.
96+
* @returns {boolean} A boolean that indicates if the page is present.
97+
*/
9798
function hasPage(page) {
99+
// hasPage is always called after the index is created,
100+
// hence just return the variable in memory.
101+
// There is no need to re-read the index file again.
98102
if (!shortIndex) {
99103
return false;
100104
}
101105
return page in shortIndex;
102106
}
103107

104-
// Return all commands available in the local cache.
108+
/**
109+
* Return all commands available in the local cache.
110+
* @returns {Promise<string[]>} A promise with the commands from cache.
111+
*/
105112
function commands() {
106113
return getShortIndex().then((idx) => {
107114
return Object.keys(idx).sort();
@@ -110,6 +117,13 @@ function commands() {
110117

111118
// Return all commands for a given platform.
112119
// P.S. - The platform 'common' is always included.
120+
/**
121+
* Return all commands for a given platform.
122+
*
123+
* The 'common' platform is always included.
124+
* @param platform {string} The platform.
125+
* @returns {Promise<string[]>} A promise with the commands for a given platform.
126+
*/
113127
function commandsFor(platform) {
114128
return getShortIndex()
115129
.then((idx) => {
@@ -124,7 +138,11 @@ function commandsFor(platform) {
124138
});
125139
}
126140

127-
// Delete the index file.
141+
/**
142+
* Delete the index file.
143+
*
144+
* @returns {Promise<any>} A promise when the remove is completed.
145+
*/
128146
function clearPagesIndex() {
129147
return fs.unlink(shortIndexFile)
130148
.then(() => {
@@ -139,7 +157,9 @@ function clearPagesIndex() {
139157
});
140158
}
141159

142-
// Set the shortIndex variable to null.
160+
/**
161+
* Set the shortIndex variable to null.
162+
*/
143163
function clearRuntimeIndex() {
144164
shortIndex = null;
145165
}
@@ -150,18 +170,26 @@ function rebuildPagesIndex() {
150170
});
151171
}
152172

153-
// If the variable is not set, read the file and set it.
154-
// Else, just return the variable.
173+
/**
174+
* Return the index.
175+
*
176+
* If the index is not loaded, read the file and load it.
177+
* @returns {Promise<any>} The index entries.
178+
*/
155179
function getShortIndex() {
156180
if (shortIndex) {
157181
return Promise.resolve(shortIndex);
158182
}
159183
return readShortPagesIndex();
160184
}
161185

162-
// Read the index file, and load it into memory.
163-
// If the file does not exist, create the data structure, write the file,
164-
// and load it into memory.
186+
/**
187+
* Read the index file, and load it into memory.
188+
*
189+
* If the file does not exist, create the data structure, write the file,
190+
* and load it into memory.
191+
* @returns {Promise<any>} The index entries.
192+
*/
165193
function readShortPagesIndex() {
166194
return fs.readJson(shortIndexFile)
167195
.then((idx) => {

lib/remote.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const unzip = require('adm-zip');
66
const config = require('./config');
77
const axios = require('axios');
88

9-
// Downloads the zip file from github and extracts it to folder
9+
/**
10+
* Download the zip file from GitHub and extract it to folder.
11+
* @param path {string} Path to destination folder.
12+
* @returns {Promise<void>} A promise when the operation is completed.
13+
*/
1014
exports.download = (loc, lang) => {
1115
// If the lang is english then keep the url simple, otherwise add language.
1216
const suffix = (lang === 'en' ? '' : '.' + lang);
@@ -26,7 +30,7 @@ exports.download = (loc, lang) => {
2630

2731
const writer = fs.createWriteStream(fileName);
2832
response.data.pipe(writer);
29-
33+
3034
writer.on('finish', () => {
3135
writer.end();
3236
const zip = new unzip(fileName);
@@ -41,4 +45,4 @@ exports.download = (loc, lang) => {
4145
}).catch((err) => {
4246
return Promise.reject(err);
4347
});
44-
};
48+
};

lib/tldr.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Tldr {
1919
this.cache = new Cache(this.config);
2020
}
2121

22+
/**
23+
* Print pages for a given platform..
24+
* @param singleColumn {boolean} A boolean to print one command per line.
25+
* @returns {Promise<void>} A promise when the operation is completed.
26+
*/
2227
list(singleColumn) {
2328
let platform = platforms.getPreferredPlatformFolder(this.config);
2429
return index.commandsFor(platform)
@@ -27,17 +32,33 @@ class Tldr {
2732
});
2833
}
2934

35+
/**
36+
* Print all pages in the cache.
37+
* @param singleColumn {boolean} A boolean to print one command per line.
38+
* @returns {Promise<void>} A promise when the operation is completed.
39+
*/
3040
listAll(singleColumn) {
3141
return index.commands()
3242
.then((commands) => {
3343
return this.printPages(commands, singleColumn);
3444
});
3545
}
3646

47+
/**
48+
* Print a command page.
49+
* @param commands {string[]} A given command to be printed.
50+
* @param options {object} The options for the render.
51+
* @returns {Promise<void>} A promise when the operation is completed.
52+
*/
3753
get(commands, options) {
3854
return this.printBestPage(commands.join('-'), options);
3955
}
4056

57+
/**
58+
* Print a random page for the current platform.
59+
* @param options {object} The options for the render.
60+
* @returns {Promise<void>} A promise when the operation is completed.
61+
*/
4162
random(options) {
4263
let platform = platforms.getPreferredPlatformFolder(this.config);
4364
return index.commandsFor(platform)
@@ -54,6 +75,10 @@ class Tldr {
5475
});
5576
}
5677

78+
/**
79+
* Print a random page.
80+
* @returns {Promise<void>} A promise when the opration is completed.
81+
*/
5782
randomExample() {
5883
let platform = platforms.getPreferredPlatformFolder(this.config);
5984
return index.commandsFor(platform)
@@ -70,6 +95,11 @@ class Tldr {
7095
});
7196
}
7297

98+
/**
99+
* Print a markdown file.
100+
* @param file {string} The path to the file.
101+
* @returns {Promise<void>} A promise when the operation is completed.
102+
*/
73103
render(file) {
74104
if (typeof file !== 'string') {
75105
throw new MissingRenderPathError();
@@ -83,24 +113,41 @@ class Tldr {
83113
});
84114
}
85115

116+
/**
117+
* Clear the cache folder.
118+
* @returns {Promise<void>} A promise when the cache is deleted.
119+
*/
86120
clearCache() {
87121
return this.cache.clear().then(() => {
88122
console.log('Done');
89123
});
90124
}
91125

126+
/**
127+
* Update the cache.
128+
* @returns {Promise<any>} A promise with the index.
129+
*/
92130
updateCache() {
93131
return spinningPromise('Updating...', () => {
94132
return this.cache.update();
95133
});
96134
}
97135

136+
/**
137+
* Update the index.
138+
* @returns {Promise<any>} A promise with the index.
139+
*/
98140
updateIndex() {
99141
return spinningPromise('Creating index...', () => {
100142
return search.createIndex();
101143
});
102144
}
103145

146+
/**
147+
* Search some keywords in the index and print the results.
148+
* @param keywords {string[]} The given keywords.
149+
* @returns {Promise<any>} A promise when the operation is completed.
150+
*/
104151
search(keywords) {
105152
return search.getResults(keywords.join(' '))
106153
.then((results) => {
@@ -109,6 +156,12 @@ class Tldr {
109156
});
110157
}
111158

159+
/**
160+
* Print all pages.
161+
* @param pages {string[]} A list of pages to be printed.
162+
* @param singleColumn {boolean} A boolean to print one command per line.
163+
* @returns {Promise<void>} A promise when the operation is completed.
164+
*/
112165
printPages(pages, singleColumn) {
113166
if (pages.length === 0) {
114167
throw new EmptyCacheError();
@@ -121,7 +174,15 @@ class Tldr {
121174
});
122175
}
123176

124-
printBestPage(command, options={}) {
177+
/**
178+
* Print a page from the cache.
179+
*
180+
* If the page is not present, the cache is updated.
181+
* @param command {string} The given command to be printed.
182+
* @param options {object} The options for the render.
183+
* @returns {Promise<void>} A promise when the operation is completed.
184+
*/
185+
printBestPage(command, options= {}) {
125186
// Trying to get the page from cache first
126187
return this.cache.getPage(command)
127188
.then((content) => {
@@ -157,6 +218,10 @@ class Tldr {
157218
});
158219
}
159220

221+
/**
222+
* Print a warning message if the cache is 30 days old.
223+
* @returns {Promise<void>} A promise when the operation is completed.
224+
*/
160225
checkStale() {
161226
return this.cache.lastUpdated()
162227
.then((stats) => {
@@ -166,7 +231,12 @@ class Tldr {
166231
});
167232
}
168233

169-
renderContent(content, options={}) {
234+
/**
235+
* Print the page content.
236+
* @param content {string} The content of a page.
237+
* @param options {object<{markdown: boolean, randomExample: boolean}>} The options for the render.
238+
*/
239+
renderContent(content, options= {}) {
170240
if (options.markdown) {
171241
return console.log(content);
172242
}
@@ -181,6 +251,12 @@ class Tldr {
181251
}
182252
}
183253

254+
/**
255+
* Display a spinner while a task is running.
256+
* @param text {string} The text of the spinner.
257+
* @param factory {Function} A task to be run.
258+
* @returns {Promise} A promise with the result of the task.
259+
*/
184260
function spinningPromise(text, factory) {
185261
const spinner = ora();
186262
spinner.start(text);

0 commit comments

Comments
 (0)