diff --git a/.github/scripts/validate-locale-files.js b/.github/scripts/validate-locale-files.js index a4fd1f5..851a1d0 100644 --- a/.github/scripts/validate-locale-files.js +++ b/.github/scripts/validate-locale-files.js @@ -65,16 +65,64 @@ function validateJsonFile(fileName, skipRecap) { } console.log(`\x1b[32mFile '${fileName}' passed all checks.\x1b[0m`); + return true; } catch (error) { console.error(`File '${fileName}' did not pass the checks`); process.exitCode = 1; + return false; + } +} + +function updateReadMeFile(localesStatuses) { + let fileContent; + + const filePath = path.resolve(parentDir, './README.md'); + try { + fileContent = fs.readFileSync(filePath, 'utf8'); + } catch (err) { + console.error('Unable to open README.md:', err.message); + process.exit(1); + } + + const regex = /- (✅|❌) \*\*(.*?)\*\* \(Base Locale - v[\d.]+\)/; + const enEntry = fileContent.match(regex); + + let updatedLocales = [enEntry ? enEntry[0] : '- ✅ **en.json** (Base Locale - v?.?.?)']; + + localesStatuses.forEach((isUpToDate, fileName) => { + const icon = isUpToDate ? '✅' : '❌'; + const displayName = `**${fileName}**`; + updatedLocales.push(`- ${icon} ${displayName}`); + }); + + const totalLocales = [...localesStatuses.values()].length + 1; + const upToDateLocales = [...localesStatuses.values()].filter(Boolean).length + 1; + const summary = `*${upToDateLocales}/${totalLocales} locales up to date*`; + + let newFileContent = fileContent.replace( + /## Locales Status:[\s\S]*?/g, + `## Locales Status:\n${summary}\n${updatedLocales.join('\n')}\n` + ); + + if (newFileContent === fileContent) newFileContent += ( + `\n\n## Locales Status:\n` + + `${summary}\n${updatedLocales.join('\n')}\n` + + `` + ); + + try { + fs.writeFileSync(filePath, newFileContent); + console.log('README.md has been successfully updated!'); + } catch (err) { + console.error('Error writing to README.md:', err); } } const args = process.argv.slice(2); const skipVerification = args.includes('--skipdetails'); -const files = args.filter(arg => arg !== '--skipdetails'); +const updateReadMe = args.includes('--updatereadme'); +const files = args.filter(arg => arg.endsWith('.json')); if (files.length === 0) { @@ -86,29 +134,34 @@ if (files.length === 0) { const jsonFiles = allFiles.filter(file => file.endsWith('.json')); + const recap = new Map(); + jsonFiles.forEach((file) => { switch (file) { case 'en.json': validateEnJsonFile(file); break default: - validateJsonFile(file, skipVerification); + const status = validateJsonFile(file, skipVerification); + recap.set(file, status); break; } }); + + if (updateReadMe) updateReadMeFile(recap); + }); } else { + if (updateReadMe) console.warn('cannot use --updatereadme and check individual files'); files.forEach((file) => { - if (file.endsWith('.json')) { - console.log(`\x1b[34mChecking ${file}\x1b[0m`); - switch (file) { - case 'en.json': - validateEnJsonFile(path.resolve(parentDir, file)); - break - default: - validateJsonFile(file, path.resolve(parentDir, file), skipVerification); - break; - } + console.log(`\x1b[34mChecking ${file}\x1b[0m`); + switch (file) { + case 'en.json': + validateEnJsonFile(path.resolve(parentDir, file)); + break + default: + validateJsonFile(file, path.resolve(parentDir, file), skipVerification); + break; } }); } \ No newline at end of file diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 99915c4..766c4ed 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,6 +1,11 @@ -name: Validate All Locale Files +name: Update Readme Locale Recap on: + push: + branches: + - main + paths: + - '*.json' workflow_dispatch: jobs: @@ -21,6 +26,34 @@ jobs: cd .github/scripts npm install - - name: Validate all JSON locale Files + - name: Bump package version run: | - node ./.github/scripts/validate-locale-files.js --skipdetails + git config --global user.name "GitHub Actions" + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" + + - name: Update ReadMe + run: | + node ./.github/scripts/validate-locale-files.js --skipdetails --updatereadme + continue-on-error: true + + - name: Check Readme has changed + id: check-readme + run: | + git status --porcelain README.md | grep '^[ AM]' > /dev/null + if [[ $? -eq 0 ]]; then + echo "Changes detected in README.md" + echo "README_CHANGED=true" >> $GITHUB_ENV + else + echo "No changes in README.md" + echo "README_CHANGED=false" >> $GITHUB_ENV + fi + continue-on-error: true + + - name: Commit Changes + if: env.README_CHANGED == 'true' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + git add README.md + git commit -m "Update README.md with locale validation changes" + git push \ No newline at end of file