Skip to content

Commit

Permalink
ci: check all files and update readme accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximus7474 committed Dec 12, 2024
1 parent dd9bb1d commit bff28a4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 15 deletions.
77 changes: 65 additions & 12 deletions .github/scripts/validate-locale-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]*?<!-- Recap End -->/g,
`## Locales Status:\n${summary}\n${updatedLocales.join('\n')}\n<!-- Recap End -->`
);

if (newFileContent === fileContent) newFileContent += (
`\n\n## Locales Status:\n` +
`${summary}\n${updatedLocales.join('\n')}\n` +
`<!-- Recap End -->`
);

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) {

Expand All @@ -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;
}
});
}
39 changes: 36 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: Validate All Locale Files
name: Update Readme Locale Recap

on:
push:
branches:
- main
paths:
- '*.json'
workflow_dispatch:

jobs:
Expand All @@ -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

0 comments on commit bff28a4

Please sign in to comment.