diff --git a/index.js b/index.js new file mode 100644 index 0000000..fb7dd92 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +// Usage: yarn run sync-strings ~/projects/projects/brave/ethereum-remote-client ~/projects/MetaMask/metamask-extension +// Will write available strings from brave-dir to metmask-dir without adding/removing existing string identifiers in metmask-dir. + +const program = require('commander') +const syncStrings = require('./sync-strings') + +program + .command('sync-strings') + .arguments('[sourceDir] [destDir]') + .action(syncStrings) + +program + .parse(process.argv) diff --git a/sync-strings.js b/sync-strings.js new file mode 100644 index 0000000..af51924 --- /dev/null +++ b/sync-strings.js @@ -0,0 +1,97 @@ +const path = require('path') +const fs = require('fs') + +const assertDirExists = (dir) => { + if (!fs.existsSync(dir)) { + console.error('Source dir does not exist') + process.exit(1) + } +} + +const getLangStringsMap = (dir) => { + const langStringsMap = {} + fs.readdirSync(dir) + .filter(filterLangs) + .forEach((lang) => { + const langPath = path.join(dir, lang, 'messages.json') + const langMap = JSON.parse(fs.readFileSync(langPath, 'utf8')) + langStringsMap[lang] = {} + Object.keys(langMap).forEach((key) => { + langStringsMap[lang][key] = langMap[key].message + }) + }) + return langStringsMap +} + +const filterLangs = (lang) => + !['index.json', 'en'].includes(lang) + +const syncLangStrings = (destDir, sourceLangStringsMap) => { + fs.readdirSync(destDir) + .filter(filterLangs) + .forEach((lang) => { + const langPath = path.join(destDir, lang, 'messages.json') + // Create a lang map which is the entire dest dir content + // this might get overwritten if the dest file already exists + let langMap = Object.keys(sourceLangStringsMap[lang]).reduce((acc, cur) => { + acc[cur] = { + message: sourceLangStringsMap[lang][cur] + } + return acc + }, {}) + let newFile = true + if (fs.existsSync(langPath)) { + langMap = JSON.parse(fs.readFileSync(langPath, 'utf8')) + newFile = false + } + Object.keys(langMap).forEach((key) => { + if (sourceLangStringsMap[lang][key] || newFile) { + langMap[key].message = + formattingFixer(brandingFixer(sourceLangStringsMap[lang][key])) + } + }) + const data = JSON.stringify(langMap, null, 2) + console.log('writing lang path is: ', langPath) + fs.writeFileSync(langPath, data + '\n', 'utf8') + }) +} + +const brandingFixer = (sourceString) => + (sourceString || '') + .replace(/Brave Software/g, 'Google') + .replace(/Brave/g, 'Chrome') + +const formattingFixer = (sourceString) => + (sourceString || '') + .replace(/\n$/g, '') + +const syncDirs = (destDir, sourceLangs) => { + sourceLangs.forEach((lang) => { + const dir = path.join(destDir, lang) + if (!fs.existsSync(dir)) { + console.log('creating new dir: ', dir) + fs.mkdirSync(dir) + } + }) +} + +const syncStrings = (sourceDir, destDir) => { + sourceDir = path.join(sourceDir, 'app', '_locales') + destDir = path.join(destDir, 'app', '_locales') + + assertDirExists(sourceDir) + assertDirExists(destDir) + + const sourceLangStringsMap = getLangStringsMap(sourceDir) + const sourceLangs = Object.keys(sourceLangStringsMap) + + syncDirs(destDir, sourceLangs) + syncLangStrings(destDir, sourceLangStringsMap) + + console.log(`Syncing from sourceDir: ${sourceDir} to destDir: ${destDir}`) + + // Read in all strings for all languages from sourceDir + // Read in all strings for all languges from destDir +} + +module.exports = syncStrings