Skip to content

Commit

Permalink
chore: migrate node localization scripts to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbej authored and josebui committed Nov 24, 2023
1 parent f283a1c commit 67cdcb0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
13 changes: 5 additions & 8 deletions scripts/localization/missing-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@
*/

import { readFile } from 'fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'path';
import { flatten } from 'flat';
import _ from 'lodash/fp.js';

import { filesInFolder } from './utils.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const SOURCE_LOCALE = 'en-US';

const LOCALE_FILES_FOLDER = path.join(
__dirname,
'../../src/localization/locales/'
const LOCALE_FILES_FOLDER = new URL(
'../../src/localization/locales/',
import.meta.url
);

const getKeys = content => {
Expand All @@ -39,7 +36,7 @@ const getKeys = content => {
};

const checkMissingKeys = () =>
readFile(path.join(LOCALE_FILES_FOLDER, `${SOURCE_LOCALE}.json`))
readFile(new URL(`${SOURCE_LOCALE}.json`, LOCALE_FILES_FOLDER))
// Get source locale keys
.then(sourceContent => getKeys(sourceContent))
// Get all locale files
Expand All @@ -56,7 +53,7 @@ const checkMissingKeys = () =>
return null;
}
console.log(
`Missing keys for ${path.parse(filePath).name}.`,
`Missing keys for ${path.parse(filePath.pathname).name}.`,
'Missing:',
localeDiff
);
Expand Down
20 changes: 12 additions & 8 deletions scripts/localization/transform-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

const path = require('path');
const { readFileSync } = require('fs');
const { writeFile } = require('fs').promises;
const { i18nextToPo, gettextToI18next } = require('i18next-conv');
import { readFileSync } from 'fs';
import { writeFile } from 'fs/promises';
import path from 'path';
import { gettextToI18next, i18nextToPo } from 'i18next-conv';

const { filesInFolder } = require('./utils');
import { filesInFolder } from './utils.js';

// Script arguments
const args = process.argv.slice(2);
Expand All @@ -37,14 +37,18 @@ const save = target => result => writeFile(target, result);

// Base transform function
const transform = (process, from, i18Transform) =>
filesInFolder(path.join(__dirname, from))
filesInFolder(new URL(from, import.meta.url))
.then(files => {
console.log(`${process} transform starting.`, 'Files:', files);
console.log(
`${process} transform starting.`,
'Files:',
files.map(f => f.pathname)
);
return files;
})
.then(files =>
files.map(filePath => {
const locale = path.parse(filePath).name;
const locale = path.parse(filePath.pathname).name;
return i18Transform(locale, filePath).then(() => locale);
})
)
Expand Down
3 changes: 1 addition & 2 deletions scripts/localization/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/

import { readdir } from 'fs/promises';
import path from 'path';

export const filesInFolder = dirname =>
readdir(dirname).then(filenames =>
filenames.map(filename => path.join(dirname, filename))
filenames.map(filename => new URL(filename, dirname))
);

0 comments on commit 67cdcb0

Please sign in to comment.