Combine JSON Files #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Combine JSON Files | |
on: | |
push: | |
paths: | |
- 'domains/*.json' | |
branches: | |
- main | |
workflow_dispatch: | |
jobs: | |
combine_json: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
- name: Install dependencies | |
run: npm --version | |
- name: Combine JSON files | |
run: | | |
const fs = require('fs'); | |
const path = require('path'); | |
const dirPath = path.join(__dirname, 'domains'); | |
const outputPath = path.join(__dirname, 'index.json'); | |
const combinedData = []; | |
fs.readdirSync(dirPath).forEach(file => { | |
if (file.endsWith('.json')) { | |
const data = JSON.parse(fs.readFileSync(path.join(dirPath, file), 'utf8')); | |
const domain = file.replace('.json', '.is-truly-a.pro'); | |
const entry = { | |
domain, | |
owner: { | |
username: data.owner.username || null, | |
email: data.owner.email || null, | |
discord: data.owner.discord || null | |
} | |
}; | |
// Remove empty fields | |
entry.owner = Object.fromEntries( | |
Object.entries(entry.owner).filter(([_, v]) => v != null) | |
); | |
combinedData.push(entry); | |
} | |
}); | |
fs.writeFileSync(outputPath, JSON.stringify(combinedData, null, 2)); | |
- name: Push to API repository | |
uses: cpina/github-action-push-to-another-repository@main | |
with: | |
source-directory: './' | |
destination-github-username: 'is-truly-a-pro' | |
destination-repository-name: 'api' | |
target-branch: 'main' | |
commit-message: 'Update index.json with combined domains' | |
env: | |
API_TOKEN_GITHUB: ${{ secrets.GH_API_TOKEN }} |