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
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"pagesRepository": "https://github.com/tldr-pages/tldr",
"repository": "https://tldr.sh/assets/tldr.zip",
"repositoryBase": "https://tldr.sh/assets/tldr-pages",
"skipUpdateWhenPageNotFound": false,
"themes": {
"simple": {
Expand Down
8 changes: 5 additions & 3 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ class Cache {
return Promise.all([
// Create new temporary folder
fs.ensureDir(tempFolder),
fs.ensureDir(this.cacheFolder)
fs.ensureDir(this.cacheFolder),
])
.then(() => {
// Download and extract cache data to temporary folder
return remote.download(tempFolder);
return Promise.allSettled(this.config.languages.map((lang) => {
return remote.download(tempFolder, lang);
}));
})
.then(() => {
// Copy data to cache folder
Expand All @@ -62,7 +64,7 @@ class Cache {
return Promise.all([
// Remove temporary folder
fs.remove(tempFolder),
index.rebuildPagesIndex()
index.rebuildPagesIndex(),
]);
})
// eslint-disable-next-line no-unused-vars
Expand Down
17 changes: 17 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const defaults = require('lodash/defaults');
const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const osHomedir = require('os').homedir;

exports.get = () => {
Expand Down Expand Up @@ -35,6 +36,22 @@ exports.get = () => {
if (errors.length > 0) {
throw new Error('Error in .tldrrc configuration:\n' + errors.join('\n'));
}

// Setting correct languages
merged.languages = ['en'];
// Get the primary & secondary language.
let langs = utils.localeToLang(process.env.LANG);
merged.languages = merged.languages.concat(langs);

if(process.env.LANGUAGE !== undefined) {
let langs = process.env.LANGUAGE.split(':');

merged.languages.push(...langs.map((lang) => {
return utils.localeToLang(lang);
}));
}
merged.languages = [...new Set(merged.languages)];

return merged;
};

Expand Down
52 changes: 31 additions & 21 deletions lib/remote.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
'use strict';

const unzip = require('node-unzip-2');
const path = require('path');
const fs = require('fs-extra');
const unzip = require('adm-zip');
const config = require('./config');
const axios = require('axios');

// Downloads the zip file from github and extracts it to folder
exports.download = (path) => {
const url = config.get().repository;
exports.download = (loc, lang) => {
// If the lang is english then keep the url simple, otherwise add language.
const suffix = (lang === 'en' ? '' : '.' + lang);
const url = config.get().repositoryBase + suffix + '.zip';
const folderName = path.join(loc, 'pages' + suffix);
const REQUEST_TIMEOUT = 10000;
Comment on lines +11 to +15
Copy link
Member

@kbdharun kbdharun Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding this, we recently added support for the English archive in the same format tldr-pages-en.zip (after a request from tealdeer's maintainer). Can it be implemented here instead? (This will officially land in the upcoming client spec 2.1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure should be easy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that only the archive name should be generalized here, while still leaving in place that the english archive is extracted to the pages directory. Changing that behavior is a much larger one, which should be done in a separate PR that's focused purely on that, as will also have to take into account backwards compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, won't it be better to handle this entirely in the same (other) PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say so, but not sure how much @kbdharun wants to see the archive name changed here. The old zip will continue to work anyway for the time being that it's not exactly urgent to make that change either.


// Creating the extractor
const extractor = unzip.Extract({ path });

let req = axios({
return axios({
method: 'get',
url: url,
responseType: 'stream',
headers: { 'User-Agent' : 'tldr-node-client' }
}).then(function (response) {
response.data.pipe(extractor);
});
headers: { 'User-Agent' : 'tldr-node-client' },
timeout: REQUEST_TIMEOUT,
}).then((response) => {
return new Promise((resolve, reject) => {
let fileName = path.join(loc, 'download_' + lang + '.zip');

return new Promise((resolve, reject) => {
req.catch((err) => {
reject(err);
});
extractor.on('error', () => {
reject(new Error('Cannot update from ' + url));
});
extractor.on('close', () => {
resolve();
const writer = fs.createWriteStream(fileName);
response.data.pipe(writer);

writer.on('finish', () => {
writer.end();
const zip = new unzip(fileName);

zip.extractAllTo(folderName, true);
fs.unlinkSync(fileName);
resolve();
}).on('error', (err) => {
reject(err);
});
});
}).catch((err) => {
return Promise.reject(err);
});
};
};
25 changes: 25 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ module.exports = {
return langParts[1];
},

localeToLang(locale) {
if(locale === undefined || locale.startsWith('en')) return [];

const withDialect = ['pt', 'zh'];

let lang = locale;
if(lang.includes('.')) {
lang = lang.substring(0, lang.indexOf('.'));
}

// Check for language code & country code.
let ll = lang, cc = '';
if(lang.includes('_')) {
cc = lang.substring(lang.indexOf('_') + 1);
ll = lang.substring(0, lang.indexOf('_'));
}

// If we have dialect for this language take dialect as well.
if(withDialect.indexOf(ll) !== -1 && cc !== '') {
return [ll, ll + '_' + cc];
}

return [ll];
},

isPage(file) {
return path.extname(file) === '.md';
},
Expand Down
Loading