-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (52 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import chalk from 'chalk'
import { readFileSync, writeFileSync, readdirSync } from 'fs'
const AT_SYMBOL_REGEXP = /[{']*@['}]*/g
const AT_REPLACEMENT = "{'@'}"
console.log("Current directory filenames:", process.env.GITHUB_WORKSPACE, import.meta.dirname);
readdirSync(process.env.GITHUB_WORKSPACE).forEach(file => {
console.log('file', file);
});
const filePaths = ['src/i18n/locale/en.json', 'src/i18n/locale/pt.json', 'src/i18n/locale/es.json']
filePaths.forEach((path) => {
// For each given path, read in the json file
const resolvedPath = `${process.env.GITHUB_WORKSPACE}/${path}`
console.log('Trying to read file at path:', resolvedPath)
const json = JSON.parse(readFileSync(resolvedPath))
// fix all anomolies in the given file
const fixedValues = fixValuesInJson(json)
if (fixedValues.length) {
// write the results back to the file on disk
const pretty = JSON.stringify(json, null, '\t')
writeFileSync(resolvedPath, pretty)
}
// output any changes to the console
printSuccesses(path, fixedValues)
})
function fixValuesInJson(json) {
const fixes = []
Object.entries(json).forEach(([key, value]) => {
if (!value) return
if (typeof value === 'object') return fixValuesInJson(value)
if (value.includes('@')) {
json[key] = value.replaceAll(AT_SYMBOL_REGEXP, AT_REPLACEMENT)
if (value !== json[key]) {
fixes.push({ key, value: json[key], prev: value })
}
}
return json
})
return fixes
}
function printSuccesses(path, fixedValues) {
if (!fixedValues.length) {
return console.log(chalk.green(`Fixed 0 strings in ${path} ✅`))
}
console.log(
chalk.magenta(`Fixed ${fixedValues.length} strings in ${path} ✅`)
)
fixedValues.forEach(({ key, value, prev }) => {
console.log(chalk.cyan(` ${key}:`))
console.log(chalk.yellow(' From:'), chalk.white(prev))
console.log(chalk.yellow(' To:'), chalk.white(value))
})
}