Skip to content

Commit 1a31dfd

Browse files
committed
Merge branch 'master' into unreal/v2
2 parents 78357b3 + 1523cfd commit 1a31dfd

File tree

712 files changed

+178603
-2725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

712 files changed

+178603
-2725
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const fs = require('fs');
2+
3+
// Use dynamic import
4+
(async () => {
5+
const { FrenglishSDK } = await import('@frenglish/sdk');
6+
7+
async function fetchConfigAndSetOutputs() {
8+
const apiKey = process.env.FRENGLISH_API_KEY;
9+
10+
if (!apiKey) {
11+
console.error('::error::FRENGLISH_API_KEY secret is not set.');
12+
process.exit(1);
13+
}
14+
15+
// Check if GITHUB_OUTPUT path is available
16+
if (!process.env.GITHUB_OUTPUT) {
17+
console.error('::error::GITHUB_OUTPUT environment variable is not set. Are you running this script in GitHub Actions?');
18+
process.exit(1);
19+
}
20+
21+
try {
22+
console.log('Initializing Frenglish SDK...');
23+
const frenglish = FrenglishSDK(apiKey); // Assuming this is correct based on your usage
24+
25+
console.log('Fetching default configuration from Frenglish SDK instance...');
26+
const config = await frenglish.getDefaultConfiguration();
27+
28+
if (!config || !config.originLanguage || !config.languages || !Array.isArray(config.languages)) {
29+
console.error(`::error::Failed to retrieve a valid configuration object from SDK. Received: ${JSON.stringify(config)}`);
30+
process.exit(1);
31+
}
32+
33+
const originLanguage = config.originLanguage;
34+
const targetLanguages = config.languages; // Assuming 'languages' contains ALL languages including origin
35+
36+
// It's safer to check if originLanguage is actually in the languages array before filtering
37+
const actualTargetLanguages = targetLanguages.filter(lang => lang !== originLanguage);
38+
39+
if (actualTargetLanguages.length === 0) {
40+
console.warn('::warning::No target languages found in the configuration after filtering out the origin language.');
41+
}
42+
43+
const targetLangsString = actualTargetLanguages.join(' '); // Create space-separated string
44+
45+
console.log(`Source Language Determined: ${originLanguage}`);
46+
console.log(`Target Languages Determined: ${targetLangsString}`);
47+
48+
// --- Use GITHUB_OUTPUT to set outputs ---
49+
// Write outputs to the file specified by GITHUB_OUTPUT
50+
fs.appendFileSync(process.env.GITHUB_OUTPUT, `source_lang=${originLanguage}\n`);
51+
fs.appendFileSync(process.env.GITHUB_OUTPUT, `target_langs=${targetLangsString}\n`);
52+
// --- End of GITHUB_OUTPUT usage ---
53+
54+
console.log('Outputs set successfully.');
55+
56+
} catch (error) {
57+
console.error(`::error::Error during Frenglish configuration fetch: ${error.message}`);
58+
process.exit(1);
59+
}
60+
}
61+
62+
await fetchConfigAndSetOutputs();
63+
})().catch(error => {
64+
console.error('::error::Fatal error executing script:', error);
65+
process.exit(1);
66+
});

.github/scripts/format-locales.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const { execSync } = require("child_process");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const LOCALES_DIR = "."; // Root directory to start search
6+
const EXCLUDE_DIR = path.resolve(LOCALES_DIR, "node_modules"); // Absolute path to node_modules
7+
8+
function formatJsonFile(filePath) {
9+
try {
10+
console.log(`Formatting ${filePath}...`);
11+
12+
// Read the file
13+
const content = fs.readFileSync(filePath, "utf-8");
14+
15+
// Parse and reformat the JSON
16+
// Ensure content is not empty before parsing
17+
if (content.trim() === "") {
18+
console.warn(`✓ Skipping empty file: ${filePath}`);
19+
return true; // Treat empty files as success (nothing to format)
20+
}
21+
22+
const parsedJson = JSON.parse(content);
23+
24+
// Sort keys - handle non-object JSON (e.g., arrays, primitives)
25+
let formattedContent;
26+
if (typeof parsedJson === 'object' && parsedJson !== null && !Array.isArray(parsedJson)) {
27+
// Sort keys only for non-null, non-array objects
28+
const sortedKeys = Object.keys(parsedJson).sort();
29+
const sortedJson = {};
30+
sortedKeys.forEach(key => {
31+
sortedJson[key] = parsedJson[key];
32+
});
33+
formattedContent = JSON.stringify(sortedJson, null, 2);
34+
} else {
35+
// For arrays or primitives, just stringify with indentation
36+
formattedContent = JSON.stringify(parsedJson, null, 2);
37+
}
38+
39+
40+
// Write the formatted content back to the file
41+
// Add newline at the end for POSIX compatibility
42+
fs.writeFileSync(filePath, formattedContent + "\n", "utf-8");
43+
44+
console.log(`✓ Formatted ${filePath}`);
45+
return true;
46+
} catch (error) {
47+
// Provide more context on parsing errors
48+
console.error(`✗ Failed to format ${filePath}: ${error.name} - ${error.message}`);
49+
return false;
50+
}
51+
}
52+
53+
function formatAllLocalesFiles() {
54+
try {
55+
// Find all JSON files recursively starting from LOCALES_DIR
56+
const command = `find ${LOCALES_DIR} -type f -name "*.json"`;
57+
console.log(`Running command: ${command}`);
58+
const output = execSync(command).toString().trim();
59+
const allFiles = output.split("\n").filter(Boolean);
60+
61+
// Filter out files within the node_modules directory
62+
const filesToFormat = allFiles.filter((filePath) => {
63+
const absoluteFilePath = path.resolve(filePath);
64+
// Check if the file path starts with the node_modules path
65+
return !absoluteFilePath.startsWith(EXCLUDE_DIR + path.sep) && // Exclude files *inside* node_modules
66+
absoluteFilePath !== EXCLUDE_DIR; // Exclude node_modules itself if it were a file somehow
67+
});
68+
69+
console.log(`Found ${allFiles.length} total JSON files.`);
70+
console.log(`Excluded ${allFiles.length - filesToFormat.length} files within node_modules.`);
71+
console.log("Files to format:", filesToFormat);
72+
73+
if (filesToFormat.length === 0) {
74+
return []; // Return empty array if no files left after filtering
75+
}
76+
77+
return filesToFormat.map(formatJsonFile);
78+
} catch (error) {
79+
// Handle errors from the 'find' command itself
80+
console.error("Error finding JSON files:", error.message);
81+
if (error.stderr) {
82+
console.error("Find command stderr:", error.stderr.toString());
83+
}
84+
return [];
85+
}
86+
}
87+
88+
function main() {
89+
const results = formatAllLocalesFiles(); // results is now an array of booleans (true for success, false for failure)
90+
91+
if (results.length === 0) {
92+
console.log("No relevant JSON files found or processed. Nothing to format.");
93+
// Decide if this should be an error or not; exiting 0 is fine if it's expected
94+
process.exit(0);
95+
}
96+
97+
// Count failures
98+
const failedCount = results.filter((success) => !success).length;
99+
100+
if (failedCount > 0) {
101+
console.error(`\nFormatting complete, but failed for ${failedCount} file(s). See logs above.`);
102+
process.exit(1);
103+
}
104+
105+
console.log("\nAll relevant JSON files formatted successfully!");
106+
}
107+
108+
main();

0 commit comments

Comments
 (0)