Skip to content

Commit

Permalink
⚡ New updates to generating metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ShafSpecs committed Jan 26, 2024
1 parent ea30de7 commit 6897ec8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 6 deletions.
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@epic-web/client-hints": "^1.2.2",
"@epic-web/invariant": "^1.0.0",
"@headlessui/react": "^1.7.18",
"@octokit/request": "^8.1.6",
"@remix-run/node": "^2.5.0",
"@remix-run/react": "^2.5.0",
"@remix-run/serve": "^2.5.0",
Expand Down
64 changes: 58 additions & 6 deletions scripts/generate-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import { readFile, readdir, writeFile } from 'fs/promises'
import { existsSync, statSync, readdirSync, readFileSync } from 'fs'
import matter from 'gray-matter'
import { normalizePath } from 'vite'
import { request } from '@octokit/request'
import 'dotenv/config'

console.log('Generating metadata...')

const __dirname = resolve()
const __dirname =
process.env.NODE_ENV === 'production'
? resolve()
: process.env.GITHUB_WORKSPACE ?? ''

// const octokit = request.defaults({
// headers: {
// authorization: `token ${process.env.GITHUB_TOKEN}`,
// },
// })

/**
* The reserved name for the index file of every doc section
*
Expand All @@ -20,6 +32,7 @@ const versions = await readFile(
'utf-8'
)
const tags = JSON.parse(versions).map((v: any) => v.tag) as Array<string>
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

tags.forEach(async tag => {
const currentDirectory = resolve(postDirectory, tag)
Expand Down Expand Up @@ -124,9 +137,48 @@ tags.forEach(async tag => {
metadataObject.sections = sortedSections.map(s => s[0])
metadataObject.meta = _f

await writeFile(
resolve(currentDirectory, 'metadata.json'),
JSON.stringify(metadataObject, null, 2),
'utf-8'
)
if (process.env.NODE_ENV === 'development') {
await writeFile(
resolve(currentDirectory, 'metadata.json'),
JSON.stringify(metadataObject, null, 2),
'utf-8'
)
} else {
const { data } = await request(
'GET /repos/{owner}/{repo}/contents/{path}',
{
owner: process.env.GITHUB_OWNER ?? '',
repo: process.env.GITHUB_REPO ?? '',
path: `posts/${tag}/metadata.json`,
}
)

// @ts-ignore
const content = Buffer.from(data.content, 'base64').toString('utf-8')

if (content === JSON.stringify(metadataObject, null, 2)) {
console.log(`No changes observed in ${tag} version. Skipping...`)
await delay(1_000)
return
}

// @ts-ignore
const sha = data.sha

await request('PUT /repos/{owner}/{repo}/contents/{path}', {
owner: process.env.GITHUB_OWNER ?? '',
repo: process.env.GITHUB_REPO ?? '',
path: `posts/${tag}/metadata.json`,
message: '📃 Update metadata.json',
content: Buffer.from(JSON.stringify(metadataObject, null, 2)).toString(
'base64'
),
sha,
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
})

await delay(3_000)
}
})

0 comments on commit 6897ec8

Please sign in to comment.