Skip to content

Commit

Permalink
Add logic to sync strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Sep 3, 2019
1 parent 72fde2f commit 4dfc702
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
97 changes: 97 additions & 0 deletions sync-strings.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4dfc702

Please sign in to comment.