Skip to content

Commit

Permalink
feat(plugins): add plugin registry fetch script
Browse files Browse the repository at this point in the history
- Introduce `fetch-registry.mjs` to download `registry.json` from `ArtalkJS/Community` repo for the latest release
- Update `build:docs` script to include registry fetch
  • Loading branch information
qwqcode committed Oct 22, 2024
1 parent 258dfde commit c2b9afa
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
__debug*
vite.config.ts.timestamp-*

# frontend
node_modules
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pnpm-lock.yaml
**/auto-imports.d.ts
**/components.d.ts
**/typed-router.d.ts
**/vite.config.ts.timestamp-*
ui/artalk/src/api/v2.ts
docs/swagger/swagger.json
docs/swagger/swagger.yaml
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"build:plugin-kit": "pnpm -F @artalk/plugin-kit build",
"build:eslint-plugin": "pnpm -F eslint-plugin-artalk build",
"build:typedoc": "pnpm typedoc",
"build:docs": "pnpm build && pnpm -F=docs-landing build && pnpm -F=docs-swagger swagger:build && pnpm -F=docs build:docs && pnpm patch:docs && pnpm build:typedoc",
"patch:docs": "cp -rf docs/landing/dist/* docs/swagger/dist/* docs/docs/.vitepress/dist",
"build:docs": "pnpm build && pnpm -F=docs-landing build && pnpm -F=docs-swagger swagger:build && pnpm -F=docs build:docs && pnpm after:build-docs",
"after:build-docs": "cp -rf docs/landing/dist/* docs/swagger/dist/* docs/docs/.vitepress/dist && pnpm build:typedoc && pnpm fetch:registry",
"fetch:registry": "node scripts/fetch-registry.mjs -- --docs-build",
"lint:eslint": "eslint .",
"lint:prettier": "prettier --check .",
"lint": "pnpm lint:eslint && pnpm lint:prettier",
Expand Down
91 changes: 91 additions & 0 deletions scripts/fetch-registry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node

import https from 'node:https'
import fs from 'node:fs'
import process from 'node:process'
import path from 'node:path'

const __dirname = path.dirname(new URL(import.meta.url).pathname)

const REGISTRY_URL = 'https://github.com/ArtalkJS/Community/releases/latest/download/registry.json'
const DOCS_DIST_PATH = path.join(__dirname, '../docs/docs/.vitepress/dist/plugins')

const download = (url) => {
return new Promise((resolve, reject) => {
const request = https.get(url, (response) => {
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
const redirectUrl = response.headers.location
response.resume()
return resolve(download(redirectUrl))
}

if (response.statusCode !== 200) {
response.resume()
reject({
error: true,
message: `Failed to get '${url}'`,
statusCode: response.statusCode,
})
return
}

let data = ''
response.on('data', (chunk) => {
data += chunk
})

response.on('end', () => {
try {
resolve(JSON.parse(data)) // Try to parse the JSON
} catch (err) {
reject({
error: true,
message: 'Failed to parse JSON response',
detail: err.message,
})
}
})
})

// Handle request errors
request.on('error', (err) => {
reject({
error: true,
message: 'Network error occurred',
detail: err.message,
})
})

// Ensure the request ends
request.end()
})
}

download(REGISTRY_URL)
.then((data) => {
const registryJSON = JSON.stringify(data, null, 2)

if (process.argv.includes('--docs-build')) {
fs.mkdir(DOCS_DIST_PATH, { recursive: true }, (err) => {
if (err) {
console.error('❌ Failed to create directory:', err)
process.exit(1)
}

const outputFile = path.join(DOCS_DIST_PATH, 'registry.json')
fs.writeFile(outputFile, registryJSON, (err) => {
if (err) {
console.error('❌ Failed to write registry.json:', err)
process.exit(1)
}
console.log(`✅ The registry.json has been updated! Saved to: "${outputFile}"`)
})
})
} else {
console.log(registryJSON)
}
})
.catch((err) => {
console.error(JSON.stringify(err, null, 2))
process.exit(1)
})

0 comments on commit c2b9afa

Please sign in to comment.