name: Sync Versions to index.json on: push: branches: - main paths: - '*/*.json' - '!index.json' workflow_dispatch: jobs: sync-versions: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Node.js uses: actions/setup-node@v4 with: node-version: '18' - name: Sync versions from individual JSON files run: | node << 'EOF' const fs = require('fs'); const path = require('path'); const { readdirSync } = require('fs'); // Read index.json const indexPath = './index.json'; const indexData = JSON.parse(fs.readFileSync(indexPath, 'utf8')); // Get all directories in current folder const entries = readdirSync('.', { withFileTypes: true }); const directoriesWithJsonFiles = entries .filter(entry => entry.isDirectory() && !entry.name.startsWith('.')) .map(entry => entry.name) .filter(dirName => { const jsonPath = path.join(dirName, `${dirName}.json`); return fs.existsSync(jsonPath); }); console.log(`Found ${directoriesWithJsonFiles.length} directories with matching JSON files\n`); directoriesWithJsonFiles.forEach(dir => { const filePath = path.join(dir, `${dir}.json`); try { const sourceData = JSON.parse(fs.readFileSync(filePath, 'utf8')); const indexKey = `${dir}/${dir}.json`; // Check if this entry exists in index.json if (indexData[indexKey]) { // Update version in index.json if (sourceData.version) { const oldVersion = indexData[indexKey].version; indexData[indexKey].version = sourceData.version; if (oldVersion !== sourceData.version) { console.log(`✓ ${indexKey}: ${oldVersion} → ${sourceData.version}`); } else { console.log(`= ${indexKey}: ${sourceData.version} (no change)`); } } } else { console.log(`⚠ ${indexKey}: Not found in index.json`); } } catch (error) { console.error(`✗ ${filePath}: ${error.message}`); } }); // Write updated index.json with pretty formatting fs.writeFileSync(indexPath, JSON.stringify(indexData, null, 2) + '\n'); console.log('\n✓ index.json updated successfully'); EOF - name: Check for changes id: verify run: | if git diff --quiet; then echo "changed=false" >> $GITHUB_OUTPUT else echo "changed=true" >> $GITHUB_OUTPUT git diff fi - name: Configure Git if: steps.verify.outputs.changed == 'true' run: | git config user.name "Gitea Actions Bot" git config user.email "actions@gitea.local" - name: Commit and push changes if: steps.verify.outputs.changed == 'true' run: | git add index.json git commit -m "chore: sync version strings from individual JSON files" -m "Automatically updated version fields in index.json to match their respective source files" -m "Generated by sync-versions workflow" git push origin main env: GIT_AUTHOR_NAME: Gitea Actions Bot GIT_AUTHOR_EMAIL: actions@gitea.local GIT_COMMITTER_NAME: Gitea Actions Bot GIT_COMMITTER_EMAIL: actions@gitea.local