Skip to content

Commit

Permalink
Merge pull request #2 from skinbookmc/servers
Browse files Browse the repository at this point in the history
Added R2 Bucket
  • Loading branch information
ethan-davies authored Mar 30, 2024
2 parents 273dde8 + 480e8a4 commit ee7ea3d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/cdn.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Upload to R2 Bucket

on:
push:
branches:
- master

workflow_dispatch:

jobs:
upload-cdn:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install dependencies
run: npm install

- name: Run npm run package
run: npm run package

- name: Configure AWS credentials
run: |
aws s3 cp ./cdn s3://$BUCKET_NAME --recursive --region $REGION --access-key-id $ACCESS_KEY_ID --secret-access-key $SECRET_ACCESS_KEY
env:
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
SECRET_ACCESS_KEY: ${{ secrets.SECRET_ACCESS_KEY }}
BUCKET_NAME: skinbook-cdn
REGION: WEUR
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**/node_modules
pnpm-lock.yaml
yarn.lock
yarn.lock
.env
**/cdn
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "./scripts/validator.js",
"scripts": {
"validate": "node ./scripts/validator.js"
"validate": "node ./scripts/validator.js",
"package": "node ./scripts/prepare.js"
},
"repository": {
"type": "git",
Expand All @@ -17,6 +18,8 @@
},
"homepage": "https://github.com/skinbookmc/ServerMappings#readme",
"dependencies": {
"ajv": "^8.12.0"
"ajv": "^8.12.0",
"cloudflare": "3.0.0-beta.14",
"dotenv": "^16.4.5"
}
}
78 changes: 78 additions & 0 deletions scripts/prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const fs = require('fs')
const path = require('path')

const serversDir = path.join(__dirname, '../servers')
const cdnDir = path.join(__dirname, '../cdn')

// Function to read directories recursively
function readDirectoriesRecursively(dir) {
let files = []

const items = fs.readdirSync(dir)

items.forEach((item) => {
const fullPath = path.join(dir, item)

if (fs.statSync(fullPath).isDirectory()) {
files = files.concat(readDirectoriesRecursively(fullPath))
} else {
files.push(fullPath)
}
})

return files
}

// Function to combine metadata files into a single servers.json file
function combineMetadataToServersJSON() {
const metadataFiles = readDirectoriesRecursively(serversDir).filter(
(file) => path.basename(file) === 'metadata.json'
)

const serversData = metadataFiles.map((file) => {
return JSON.parse(fs.readFileSync(file, 'utf-8'))
})

const combinedData = serversData.reduce((acc, curr) => {
return acc.concat(curr)
}, [])

fs.writeFileSync(
path.join(cdnDir, 'servers.json'),
JSON.stringify(combinedData, null, 2)
)
}

// Function to copy and rename files to the CDN folders
function copyAndRenameFiles(folderName, fileExtension) {
const files = readDirectoriesRecursively(serversDir).filter(
(file) => path.basename(file) === fileExtension
)

files.forEach((file) => {
const folderId = path.basename(path.dirname(file))
const destination = path.join(cdnDir, folderName, `${folderId}.png`)

// Create destination folder if it doesn't exist
const destFolder = path.dirname(destination)
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder, { recursive: true })
}

fs.copyFileSync(file, destination)
})
}

// Main function to execute all tasks
function prepareCDNUpload() {
if (!fs.existsSync(cdnDir)) {
fs.mkdirSync(cdnDir)
}

combineMetadataToServersJSON()
copyAndRenameFiles('logos', 'logo.png')
copyAndRenameFiles('banners', 'background.png')
}

// Usage
prepareCDNUpload()

0 comments on commit ee7ea3d

Please sign in to comment.