Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 0 additions & 4 deletions .env

This file was deleted.

66 changes: 66 additions & 0 deletions .github/scripts/fetch-frenglish-configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const fs = require('fs');

// Use dynamic import
(async () => {
const { FrenglishSDK } = await import('@frenglish/sdk');

async function fetchConfigAndSetOutputs() {
const apiKey = process.env.FRENGLISH_API_KEY;

if (!apiKey) {
console.error('::error::FRENGLISH_API_KEY secret is not set.');
process.exit(1);
}

// Check if GITHUB_OUTPUT path is available
if (!process.env.GITHUB_OUTPUT) {
console.error('::error::GITHUB_OUTPUT environment variable is not set. Are you running this script in GitHub Actions?');
process.exit(1);
}

try {
console.log('Initializing Frenglish SDK...');
const frenglish = FrenglishSDK(apiKey); // Assuming this is correct based on your usage

console.log('Fetching default configuration from Frenglish SDK instance...');
const config = await frenglish.getDefaultConfiguration();

if (!config || !config.originLanguage || !config.languages || !Array.isArray(config.languages)) {
console.error(`::error::Failed to retrieve a valid configuration object from SDK. Received: ${JSON.stringify(config)}`);
process.exit(1);
}

const originLanguage = config.originLanguage;
const targetLanguages = config.languages; // Assuming 'languages' contains ALL languages including origin

// It's safer to check if originLanguage is actually in the languages array before filtering
const actualTargetLanguages = targetLanguages.filter(lang => lang !== originLanguage);

if (actualTargetLanguages.length === 0) {
console.warn('::warning::No target languages found in the configuration after filtering out the origin language.');
}

const targetLangsString = actualTargetLanguages.join(' '); // Create space-separated string

console.log(`Source Language Determined: ${originLanguage}`);
console.log(`Target Languages Determined: ${targetLangsString}`);

// --- Use GITHUB_OUTPUT to set outputs ---
// Write outputs to the file specified by GITHUB_OUTPUT
fs.appendFileSync(process.env.GITHUB_OUTPUT, `source_lang=${originLanguage}\n`);
fs.appendFileSync(process.env.GITHUB_OUTPUT, `target_langs=${targetLangsString}\n`);
// --- End of GITHUB_OUTPUT usage ---

console.log('Outputs set successfully.');

} catch (error) {
console.error(`::error::Error during Frenglish configuration fetch: ${error.message}`);
process.exit(1);
}
}

await fetchConfigAndSetOutputs();
})().catch(error => {
console.error('::error::Fatal error executing script:', error);
process.exit(1);
});
108 changes: 108 additions & 0 deletions .github/scripts/format-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");

const LOCALES_DIR = "."; // Root directory to start search
const EXCLUDE_DIR = path.resolve(LOCALES_DIR, "node_modules"); // Absolute path to node_modules

function formatJsonFile(filePath) {
try {
console.log(`Formatting ${filePath}...`);

// Read the file
const content = fs.readFileSync(filePath, "utf-8");

// Parse and reformat the JSON
// Ensure content is not empty before parsing
if (content.trim() === "") {
console.warn(`✓ Skipping empty file: ${filePath}`);
return true; // Treat empty files as success (nothing to format)
}

const parsedJson = JSON.parse(content);

// Sort keys - handle non-object JSON (e.g., arrays, primitives)
let formattedContent;
if (typeof parsedJson === 'object' && parsedJson !== null && !Array.isArray(parsedJson)) {
// Sort keys only for non-null, non-array objects
const sortedKeys = Object.keys(parsedJson).sort();
const sortedJson = {};
sortedKeys.forEach(key => {
sortedJson[key] = parsedJson[key];
});
formattedContent = JSON.stringify(sortedJson, null, 2);
} else {
// For arrays or primitives, just stringify with indentation
formattedContent = JSON.stringify(parsedJson, null, 2);
}


// Write the formatted content back to the file
// Add newline at the end for POSIX compatibility
fs.writeFileSync(filePath, formattedContent + "\n", "utf-8");

console.log(`✓ Formatted ${filePath}`);
return true;
} catch (error) {
// Provide more context on parsing errors
console.error(`✗ Failed to format ${filePath}: ${error.name} - ${error.message}`);
return false;
}
}

function formatAllLocalesFiles() {
try {
// Find all JSON files recursively starting from LOCALES_DIR
const command = `find ${LOCALES_DIR} -type f -name "*.json"`;
console.log(`Running command: ${command}`);
const output = execSync(command).toString().trim();
const allFiles = output.split("\n").filter(Boolean);

// Filter out files within the node_modules directory
const filesToFormat = allFiles.filter((filePath) => {
const absoluteFilePath = path.resolve(filePath);
// Check if the file path starts with the node_modules path
return !absoluteFilePath.startsWith(EXCLUDE_DIR + path.sep) && // Exclude files *inside* node_modules
absoluteFilePath !== EXCLUDE_DIR; // Exclude node_modules itself if it were a file somehow
});

console.log(`Found ${allFiles.length} total JSON files.`);
console.log(`Excluded ${allFiles.length - filesToFormat.length} files within node_modules.`);
console.log("Files to format:", filesToFormat);

if (filesToFormat.length === 0) {
return []; // Return empty array if no files left after filtering
}

return filesToFormat.map(formatJsonFile);
} catch (error) {
// Handle errors from the 'find' command itself
console.error("Error finding JSON files:", error.message);
if (error.stderr) {
console.error("Find command stderr:", error.stderr.toString());
}
return [];
}
}

function main() {
const results = formatAllLocalesFiles(); // results is now an array of booleans (true for success, false for failure)

if (results.length === 0) {
console.log("No relevant JSON files found or processed. Nothing to format.");
// Decide if this should be an error or not; exiting 0 is fine if it's expected
process.exit(0);
}

// Count failures
const failedCount = results.filter((success) => !success).length;

if (failedCount > 0) {
console.error(`\nFormatting complete, but failed for ${failedCount} file(s). See logs above.`);
process.exit(1);
}

console.log("\nAll relevant JSON files formatted successfully!");
}

main();
Loading