From 3d70b8f1c1f0435089308bdb2a2159102935979b Mon Sep 17 00:00:00 2001 From: antelle Date: Sun, 24 May 2020 18:57:17 +0200 Subject: [PATCH] prettier --- scripts/download-translations.js | 104 +++++++++++++++++++++---------- scripts/rotate-key.js | 28 +++++---- scripts/sign.js | 7 ++- 3 files changed, 92 insertions(+), 47 deletions(-) diff --git a/scripts/download-translations.js b/scripts/download-translations.js index 1e96a16..80f992d 100644 --- a/scripts/download-translations.js +++ b/scripts/download-translations.js @@ -16,7 +16,10 @@ const PHRASE_COUNT_THRESHOLD_PERCENT = 75; const ts = Math.floor(new Date() / 1000); const hashStr = ts + keys.secret; -const hash = crypto.createHash('md5').update(hashStr).digest('hex'); +const hash = crypto + .createHash('md5') + .update(hashStr) + .digest('hex'); const urlParams = { 'api_key': keys.public, 'timestamp': ts, @@ -27,7 +30,8 @@ const urlParams = { const pluginManifest = fs.readFileSync('tmpl/manifest.json', 'utf8'); const pluginIndexPage = fs.readFileSync('tmpl/language-index.html', 'utf8'); -const publicKey = fs.readFileSync('keys/public-key.pem', 'utf8') +const publicKey = fs + .readFileSync('keys/public-key.pem', 'utf8') .match(/-+BEGIN PUBLIC KEY-+([\s\S]+?)-+END PUBLIC KEY-+/)[1] .replace(/\n/g, ''); const defaultCountries = { 'SE': 'sv' }; @@ -35,9 +39,7 @@ const defaultCountries = { 'SE': 'sv' }; module.exports = function() { return new Promise(resolve => { loadLanguages(languages => - loadTranslations(translations => - resolve(processData(languages, translations)) - ) + loadTranslations(translations => resolve(processData(languages, translations))) ); }); @@ -46,8 +48,12 @@ module.exports = function() { return callback(JSON.parse(fs.readFileSync('./data/languages.json', 'utf8'))); } console.log('Loading language names...'); - const url = API_URL_LANGUAGES.replace(':project_id', PROJECT_ID) + '?' + - Object.keys(urlParams).map(param => param + '=' + urlParams[param]).join('&'); + const url = + API_URL_LANGUAGES.replace(':project_id', PROJECT_ID) + + '?' + + Object.keys(urlParams) + .map(param => param + '=' + urlParams[param]) + .join('&'); https.get(url, res => { if (res.statusCode !== 200) { console.error(`API error ${res.statusCode}`); @@ -66,13 +72,17 @@ module.exports = function() { }); } - async function loadTranslations (callback) { + async function loadTranslations(callback) { if (USE_FILES) { return callback(JSON.parse(fs.readFileSync('./data/translations.json', 'utf8'))); } console.log('Loading translations...'); - const url = API_URL.replace(':project_id', PROJECT_ID) + '?' + - Object.keys(urlParams).map(param => param + '=' + urlParams[param]).join('&'); + const url = + API_URL.replace(':project_id', PROJECT_ID) + + '?' + + Object.keys(urlParams) + .map(param => param + '=' + urlParams[param]) + .join('&'); https.get(url, res => { if (res.statusCode !== 200) { console.error(`API error ${res.statusCode}`); @@ -91,7 +101,7 @@ module.exports = function() { }); } - async function processData (languages, translations) { + async function processData(languages, translations) { let langCount = 0; let skipCount = 0; const enUs = translations['en-US'].translation; @@ -104,12 +114,14 @@ module.exports = function() { continue; } const langPhraseCount = Object.keys(languageTranslations).length; - const percentage = Math.round(langPhraseCount / totalPhraseCount * 100); + const percentage = Math.round((langPhraseCount / totalPhraseCount) * 100); let skip = percentage >= PHRASE_COUNT_THRESHOLD_PERCENT ? null : 'SKIP'; let languageJson = JSON.stringify(languageTranslations, null, 2); if (!skip && fs.existsSync(`docs/translations/${lang}/${lang}.json`)) { - const oldJson = fs.readFileSync(`docs/translations/${lang}/${lang}.json`, { encoding: 'utf8' }); + const oldJson = fs.readFileSync(`docs/translations/${lang}/${lang}.json`, { + encoding: 'utf8' + }); if (oldJson === languageJson) { skip = 'NO CHANGES'; } @@ -117,18 +129,33 @@ module.exports = function() { const action = skip ? `\x1b[35m${skip}\x1b[0m` : '\x1b[36mOK\x1b[0m'; - console.log(`[${lang}] ${langPhraseCount} / ${totalPhraseCount} (${percentage}%) -> ${action}`); + console.log( + `[${lang}] ${langPhraseCount} / ${totalPhraseCount} (${percentage}%) -> ${action}` + ); const langInfo = languages.data.filter(x => x.code === lang)[0]; const region = (defaultCountries[langInfo.region] || langInfo.region).toLowerCase(); - const langName = langInfo.locale === region ? langInfo.local_name.replace(/\s*\(.*\)/, '') : langInfo.local_name; - const langNameEn = langInfo.locale === region ? langInfo.english_name.replace(/\s*\(.*\)/, '') : langInfo.english_name; + const langName = + langInfo.locale === region + ? langInfo.local_name.replace(/\s*\(.*\)/, '') + : langInfo.local_name; + const langNameEn = + langInfo.locale === region + ? langInfo.english_name.replace(/\s*\(.*\)/, '') + : langInfo.english_name; if (skip) { skipCount++; if (skip !== 'SKIP') { - const manifest = JSON.parse(fs.readFileSync(`docs/translations/${lang}/manifest.json`, 'utf8')); - meta[lang] = {name: langName, nameEn: langNameEn, count: langPhraseCount, version: manifest.version}; + const manifest = JSON.parse( + fs.readFileSync(`docs/translations/${lang}/manifest.json`, 'utf8') + ); + meta[lang] = { + name: langName, + nameEn: langNameEn, + count: langPhraseCount, + version: manifest.version + }; } } else { langCount++; @@ -151,9 +178,9 @@ module.exports = function() { continue; } const textMatches = text.match(/"/g); - const textMatchesCount = textMatches && textMatches.length || 0; + const textMatchesCount = (textMatches && textMatches.length) || 0; const enTextMatches = enText.match(/"/g); - const enTextMatchesCount = enTextMatches && enTextMatches.length || 0; + const enTextMatchesCount = (enTextMatches && enTextMatches.length) || 0; if (enTextMatchesCount !== textMatchesCount) { const textHl = text.replace(/"/g, '\x1b[33m"\x1b[0m'); console.warn(`[${lang}] \x1b[33mWARN:"\x1b[0m ${name}: ${textHl}`); @@ -170,7 +197,9 @@ module.exports = function() { } if (enText.indexOf('{}') >= 0 && text.indexOf('{}') < 0) { const enTextHl = enText.replace(/{}/g, '\x1b[31m{}\x1b[0m'); - console.error(`[${lang}] \x1b[31mERROR:NO{}\x1b[0m ${name}: ${text} <--> ${enTextHl}`); + console.error( + `[${lang}] \x1b[31mERROR:NO{}\x1b[0m ${name}: ${text} <--> ${enTextHl}` + ); errors++; } } @@ -183,32 +212,39 @@ module.exports = function() { process.exit(1); }); - meta[lang] = {name: langName, nameEn: langNameEn, count: langPhraseCount}; + meta[lang] = { name: langName, nameEn: langNameEn, count: langPhraseCount }; if (fs.existsSync(`docs/translations/${lang}`)) { - const manifest = JSON.parse(fs.readFileSync(`docs/translations/${lang}/manifest.json`, 'utf8')); + const manifest = JSON.parse( + fs.readFileSync(`docs/translations/${lang}/manifest.json`, 'utf8') + ); if (manifest.resources.loc !== signature) { const parts = manifest.version.split('.'); manifest.version = parts[0] + '.' + (+parts[1] + 1) + '.0'; manifest.resources.loc = signature; - fs.writeFileSync(`docs/translations/${lang}/manifest.json`, JSON.stringify(manifest, null, 2)); + fs.writeFileSync( + `docs/translations/${lang}/manifest.json`, + JSON.stringify(manifest, null, 2) + ); fs.writeFileSync(`docs/translations/${lang}/${lang}.json`, languageJson); } meta[lang].version = manifest.version; } else { fs.mkdirSync(`docs/translations/${lang}`); - fs.writeFileSync(`docs/translations/${lang}/manifest.json`, pluginManifest - .replace(/{lang}/g, lang) - .replace(/{name_ascii}/g, langNameEn.replace(/\W+/g, '-').toLowerCase()) - .replace(/{name_en}/g, langNameEn) - .replace(/{name}/g, langName) - .replace(/{signature}/g, signature) - .replace(/{key}/g, publicKey) + fs.writeFileSync( + `docs/translations/${lang}/manifest.json`, + pluginManifest + .replace(/{lang}/g, lang) + .replace(/{name_ascii}/g, langNameEn.replace(/\W+/g, '-').toLowerCase()) + .replace(/{name_en}/g, langNameEn) + .replace(/{name}/g, langName) + .replace(/{signature}/g, signature) + .replace(/{key}/g, publicKey) ); fs.writeFileSync(`docs/translations/${lang}/${lang}.json`, languageJson); - fs.writeFileSync(`docs/translations/${lang}/index.html`, pluginIndexPage - .replace(/{lang}/g, lang) - .replace(/{name}/g, langName) + fs.writeFileSync( + `docs/translations/${lang}/index.html`, + pluginIndexPage.replace(/{lang}/g, lang).replace(/{name}/g, langName) ); meta[lang].version = '1.0.0'; } diff --git a/scripts/rotate-key.js b/scripts/rotate-key.js index e111b15..0785616 100644 --- a/scripts/rotate-key.js +++ b/scripts/rotate-key.js @@ -3,10 +3,12 @@ const fs = require('fs'); const path = require('path'); const ps = require('child_process'); -const oldKey = fs.readFileSync('keys/public-key-old.pem', 'utf8') +const oldKey = fs + .readFileSync('keys/public-key-old.pem', 'utf8') .match(/-+BEGIN PUBLIC KEY-+([\s\S]+?)-+END PUBLIC KEY-+/)[1] .replace(/\s+/g, ''); -const newKey = fs.readFileSync('keys/public-key.pem', 'utf8') +const newKey = fs + .readFileSync('keys/public-key.pem', 'utf8') .match(/-+BEGIN PUBLIC KEY-+([\s\S]+?)-+END PUBLIC KEY-+/)[1] .replace(/\s+/g, ''); @@ -21,15 +23,19 @@ for (const pluginDir of pluginDirs) { } manifest.publicKey = newKey; fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2)); - const result = ps.spawnSync('node', [ - '../keeweb/plugins/keeweb-plugin/keeweb-plugin.js', - 'sign', - path.join(pluginDir, pluginName), - '--signer-module=../../../keeweb-plugins/scripts/sign', - '--bump-version' - ], { - stdio: 'inherit' - }); + const result = ps.spawnSync( + 'node', + [ + '../keeweb/plugins/keeweb-plugin/keeweb-plugin.js', + 'sign', + path.join(pluginDir, pluginName), + '--signer-module=../../../keeweb-plugins/scripts/sign', + '--bump-version' + ], + { + stdio: 'inherit' + } + ); if (result.status) { throw 'Sign error'; } diff --git a/scripts/sign.js b/scripts/sign.js index 27c12ef..72cf46c 100644 --- a/scripts/sign.js +++ b/scripts/sign.js @@ -20,6 +20,9 @@ function getPin() { } module.exports = function sign(data) { - return getPin().then(pin => signer.sign({ data, verifyKey, pin, ...signerOptions }) - .then(data => data.toString('base64'))); + return getPin().then(pin => + signer + .sign({ data, verifyKey, pin, ...signerOptions }) + .then(data => data.toString('base64')) + ); };