Skip to content

Commit

Permalink
Only download cache for system languages and English (#420)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Peveler <matt.peveler@gmail.com>
  • Loading branch information
vivekjoshi556 and MasterOdin authored Oct 24, 2023
1 parent 7ad3c5a commit d3f0309
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 213 deletions.
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;

// 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

0 comments on commit d3f0309

Please sign in to comment.