Skip to content

Instantly share code, notes, and snippets.

@SavageCore
Created June 21, 2023 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavageCore/f91b49a77ccdd2107730cfe47ff5cfbb to your computer and use it in GitHub Desktop.
Save SavageCore/f91b49a77ccdd2107730cfe47ff5cfbb to your computer and use it in GitHub Desktop.

What?

I recently wanted to convert a bunch of .jsx i18n files to json to compare the total bundle size of the GP2040-CE firmware. This is my hacky attempt.

export default {
    "header-text": "GP2040-CE",
};
{
  "header-text": "GP2040-CE"
}

Usage

Edit directoryPath at the bottom of the file and run with node index.js

const fs = require('fs');
function convertJsxFilesToJSON(directory) {
const jsxFiles = fs.readdirSync(directory).filter(file => {
if (file.endsWith('.jsx') && file !== 'Index.jsx') {
return true;
}
});
console.log(`Found ${jsxFiles.length} .jsx files in ${directory}\n`);
jsxFiles.forEach(jsxFile => {
const jsxFilePath = `${directory}\\${jsxFile}`;
const jsonFilePath = jsxFilePath.replace('.jsx', '.json');
const jsxContent = fs.readFileSync(jsxFilePath, 'utf8');
// Remove leading and trailing whitespace
const trimmedContent = jsxContent.trim();
// Remove the "export default" part
const contentWithoutExport = trimmedContent.replace('export default', '');
// Remove the trailing semicolon
const contentWithoutSemicolon = contentWithoutExport.replace(/;$/, '');
// Convert javascript object string to object
eval('var contentObj = ' + contentWithoutSemicolon);
const jsonContent = JSON.stringify(contentObj, null, 2);
// Write the JSON to a file
fs.writeFileSync(jsonFilePath, jsonContent);
console.log(`Converted ${jsxFile} to ${jsonFilePath}`);
});
}
const directoryPath = 'C:\\path\\to\\jsx';
convertJsxFilesToJSON(directoryPath);
@SavageCore
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment