Skip to content

Commit 27558db

Browse files
feat: use vercel blob for storing generated data
1 parent b14aef5 commit 27558db

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

.github/workflows/cron.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ jobs:
88
deploy:
99
runs-on: ubuntu-latest
1010
steps:
11+
- name: Checkout repo
12+
uses: actions/checkout@v4
13+
14+
- name: Set up Python 3.12
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: '3.12'
18+
19+
- name: Set up bun
20+
uses: oven-sh/setup-bun@v1
21+
22+
- name: Install dependencies
23+
run: |
24+
make prebuild
25+
bun install
26+
27+
- name: Populate the latest data
28+
run: make generate
29+
30+
- name: Sync data to the blob store
31+
run: bun sync up
32+
1133
- name: Trigger deployment
1234
uses: joelwmale/webhook-action@master
1335
with:

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ generate:
1111
poetry run python gfi/populate.py
1212

1313
generate-prod:
14-
make pre-build
15-
make generate
14+
bun sync down
1615
make build
1716

1817
test:

bun.lockb

-76.7 KB
Binary file not shown.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"dev": "nuxt dev",
88
"generate": "nuxt generate",
99
"preview": "nuxt preview",
10-
"postinstall": "nuxt prepare"
10+
"postinstall": "nuxt prepare",
11+
"sync": "bun sync.js"
1112
},
1213
"devDependencies": {
1314
"@nuxt/devtools": "latest",
@@ -19,6 +20,7 @@
1920
},
2021
"dependencies": {
2122
"@heroicons/vue": "^2.0.18",
23+
"@vercel/blob": "^0.16.1",
2224
"dayjs": "^1.11.10"
2325
}
2426
}

sync.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import https from 'https'
4+
5+
import { put, list } from '@vercel/blob'
6+
7+
const dirToSync = './data'
8+
const filesToSync = ['generated.json', 'tags.json']
9+
10+
/**
11+
* Downloads a file from a given url to a specified destination.
12+
*
13+
* @param {string} url - The URL of the file to download.
14+
* @param {string} dest - The destination where the file should be saved.
15+
* @return {Promise} A Promise that resolves when the file has been downloaded.
16+
*/
17+
function downloadFile(url, dest) {
18+
return new Promise((resolve, reject) => {
19+
// Check if the file already exists
20+
const fileExists = fs.existsSync(dest)
21+
22+
const file = fs.createWriteStream(dest, { flags: 'w' }) // 'w' flag for write mode
23+
24+
https
25+
.get(url, (response) => {
26+
if (response.statusCode !== 200) {
27+
reject(new Error(`failed to download file: status code ${response.statusCode}`))
28+
return
29+
}
30+
31+
response.pipe(file)
32+
33+
if (fileExists) {
34+
console.log(`file ${dest} already exists. overwriting.`)
35+
}
36+
})
37+
.on('error', (err) => {
38+
reject(err)
39+
})
40+
41+
file.on('finish', () => {
42+
resolve()
43+
})
44+
45+
file.on('error', (err) => {
46+
fs.unlink(dest) // delete the file on error
47+
reject(err)
48+
})
49+
})
50+
}
51+
52+
/**
53+
* Sync files from local to Vercel Blob
54+
*/
55+
async function syncFilesUp() {
56+
for (const fileName of filesToSync) {
57+
const filePath = path.resolve(path.join(dirToSync, fileName))
58+
const stat = await fs.statSync(filePath)
59+
60+
if (stat.isFile()) {
61+
const fileContent = await fs.readFileSync(filePath)
62+
await put(fileName, fileContent, { access: 'public', addRandomSuffix: false })
63+
}
64+
}
65+
}
66+
67+
/**
68+
* Sync files from Vercel Blob to local
69+
*/
70+
async function syncFilesDown() {
71+
const response = await list()
72+
for (const blob of response.blobs) {
73+
await downloadFile(blob.url, path.resolve(path.join(dirToSync, blob.pathname)))
74+
}
75+
}
76+
77+
if (!("BLOB_READ_WRITE_TOKEN" in process.env)) {
78+
console.error('`BLOB_READ_WRITE_TOKEN` not set in env. exiting.')
79+
process.exit(1) // skicq: JS-0263
80+
}
81+
82+
switch (process.argv[2]) {
83+
case 'up':
84+
await syncFilesUp()
85+
console.info('syncing up successful.')
86+
break
87+
case 'down':
88+
await syncFilesDown()
89+
console.info('syncing down successful.')
90+
break
91+
default:
92+
console.error('must provide a valid sync direction. exiting.')
93+
}

0 commit comments

Comments
 (0)