From 67cdcb0d73848dfcb218f8a69e6c1723bb7262fc Mon Sep 17 00:00:00 2001 From: Igor Bejnarowicz Date: Fri, 24 Nov 2023 10:37:51 +0100 Subject: [PATCH] chore: migrate node localization scripts to ESM --- scripts/localization/missing-check.js | 13 +++++-------- scripts/localization/transform-to.js | 20 ++++++++++++-------- scripts/localization/utils.js | 3 +-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/localization/missing-check.js b/scripts/localization/missing-check.js index d3c660971..f0d3f5c49 100644 --- a/scripts/localization/missing-check.js +++ b/scripts/localization/missing-check.js @@ -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 => { @@ -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 @@ -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 ); diff --git a/scripts/localization/transform-to.js b/scripts/localization/transform-to.js index 1eb77cae2..1b1261336 100644 --- a/scripts/localization/transform-to.js +++ b/scripts/localization/transform-to.js @@ -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); @@ -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); }) ) diff --git a/scripts/localization/utils.js b/scripts/localization/utils.js index 18d913f27..fb5b908fa 100644 --- a/scripts/localization/utils.js +++ b/scripts/localization/utils.js @@ -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)) );