Skip to content

Commit a68f2eb

Browse files
authored
Merge pull request #387 from WatWowMap/never-update-masterfile-again
Sync Masterfile & Locales With GitHub
2 parents 1467f05 + 2aafd9c commit a68f2eb

File tree

6 files changed

+61
-44939
lines changed

6 files changed

+61
-44939
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,6 @@ static/locales/*
125125

126126
# No docker-compose.yml files
127127
docker-compose.yml
128+
129+
# Ignore Masterfile
130+
masterfile.json

package-lock.json

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"scripts": {
77
"convert": "node src/geofenceToGeoJSON.js",
88
"convert-poracle": "node src/poracleToGeoJSON.js",
9-
"create-locales": "node src/createLocales.js",
9+
"create-locales": "node -e 'require(\"./src/createLocales\").locales()'",
10+
"generate": "node -e 'require(\"./src/generateMasterfile\").generate()'",
1011
"start": "node src/index.js",
1112
"test": "npx eslint src/ --ext .js,.jsx,.ts,.tsx",
1213
"test-fix": "npx eslint src/ --ext .js,.jsx,.ts,.tsx --fix",
13-
"update": "npm install && npm update pogo-translations && npm run create-locales"
14+
"update": "npm install && npm run generate && npm run create-locales"
1415
},
1516
"repository": {
1617
"type": "git",
@@ -39,7 +40,6 @@
3940
"mustache-express": "^1.3.0",
4041
"mysql": "^2.18.1",
4142
"nodes2ts": "^2.0.0",
42-
"pogo-translations": "git+https://github.com/bschultz/pogo-translations.git",
4343
"require-from-string": "^2.0.2",
4444
"sanitizer": "^0.1.3"
4545
}

src/createLocales.js

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,44 @@
22

33
const fs = require('fs');
44
const path = require('path');
5+
const axios = require('axios');
56

67
const appLocalesFolder = path.resolve(__dirname, '../static/locales');
7-
const pogoLocalesFolder = path.resolve(__dirname, '../node_modules/pogo-translations/static/locales');
88

9-
fs.readdir(appLocalesFolder, (err, files) => {
10-
let pogoLocalesFiles = [];
11-
12-
if (fs.existsSync(pogoLocalesFolder)) {
13-
pogoLocalesFiles = fs.readdirSync(pogoLocalesFolder);
14-
}
15-
16-
files.filter(file => { return file.startsWith('_'); }).forEach(file => {
17-
const locale = path.basename(file, '.json').replace('_', '');
18-
const localeFile = locale + '.json';
19-
let translations = {};
20-
21-
console.log('Creating locale', locale);
22-
23-
if (pogoLocalesFiles.includes(localeFile)) {
24-
console.log('Found pogo-translations for locale', locale);
25-
26-
const pogoTranslations = fs.readFileSync(
27-
path.resolve(pogoLocalesFolder, localeFile),
28-
{ encoding: 'utf8', flag: 'r' }
9+
module.exports.locales = async function locales() {
10+
const localTranslations = await fs.promises.readdir(appLocalesFolder);
11+
const englishRef = fs.readFileSync(path.resolve(appLocalesFolder, '_en.json'), { encoding: 'utf8', flag: 'r' });
12+
13+
await Promise.all(localTranslations.map(async locale => {
14+
if (locale.startsWith('_')) {
15+
const mapJsTranslations = fs.readFileSync(path.resolve(appLocalesFolder, locale), { encoding: 'utf8', flag: 'r' });
16+
const baseName = locale.replace('.json', '').replace('_', '');
17+
const trimmedRemoteFiles = {};
18+
19+
try {
20+
const { data } = await axios.get(`https://raw.githubusercontent.com/WatWowMap/pogo-translations/master/static/locales/${baseName}.json`);
21+
22+
Object.keys(data).forEach(key => {
23+
if (!key.startsWith('desc_') && !key.startsWith('pokemon_category_')) {
24+
trimmedRemoteFiles[key] = data[key];
25+
}
26+
});
27+
} catch (e) {
28+
console.warn(e, '\n', locale);
29+
}
30+
31+
const finalTranslations = {
32+
...JSON.parse(englishRef),
33+
...JSON.parse(mapJsTranslations),
34+
...trimmedRemoteFiles,
35+
};
36+
fs.writeFile(
37+
path.resolve(appLocalesFolder, `${baseName}.json`),
38+
JSON.stringify(finalTranslations, null, 2),
39+
'utf8',
40+
() => { },
2941
);
30-
translations = JSON.parse(pogoTranslations.toString());
42+
console.log('localeFile', 'file saved.');
3143
}
32-
33-
if (locale !== 'en') {
34-
// include en as fallback first
35-
const appTransFallback = fs.readFileSync(
36-
path.resolve(appLocalesFolder, '_en.json'),
37-
{ encoding: 'utf8', flag: 'r' }
38-
);
39-
translations = Object.assign(translations, JSON.parse(appTransFallback.toString()));
40-
}
41-
42-
const appTranslations = fs.readFileSync(path.resolve(appLocalesFolder, file), { encoding: 'utf8', flag: 'r' });
43-
translations = Object.assign(translations, JSON.parse(appTranslations.toString()));
44-
45-
fs.writeFile(
46-
path.resolve(appLocalesFolder, localeFile),
47-
JSON.stringify(translations, null, 2),
48-
'utf8',
49-
() => {}
50-
);
51-
console.log(localeFile, 'file saved.');
52-
});
53-
});
44+
}));
45+
};

src/generateMasterfile.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require('fs');
2+
const axios = require('axios');
3+
4+
module.exports.generate = async function generate() {
5+
try {
6+
const { data } = await axios.get('https://raw.githubusercontent.com/WatWowMap/Masterfile-Generator/master/master-latest.json');
7+
8+
fs.writeFile(
9+
'static/data/masterfile.json',
10+
JSON.stringify(data, null, 2),
11+
'utf8',
12+
() => { },
13+
);
14+
console.log('New masterfile generated');
15+
} catch (e) {
16+
console.warn('Unable to generate new masterfile, using existing.');
17+
}
18+
};

0 commit comments

Comments
 (0)