download.ts•1.94 kB
import { writeFileSync, mkdirSync, existsSync } from 'fs'
import AdmZip from 'adm-zip'
import { join } from 'path'
const DOC_CACHE_URL = 'https://web-cdn.agora.io/doc-prod/docs-cache.zip'
const DOCS_DIR = join(__dirname, '../../.docs')
const DOCS_ZIP_DIR = join(__dirname, '../../.tmp')
const ZIP_PATH = join(DOCS_ZIP_DIR, 'docs-cache.zip')
async function downloadAndUnzip() {
try {
// Clean .docs directory if it exists, then create a new empty one
if (existsSync(DOCS_DIR)) {
require('fs').rmSync(DOCS_DIR, { recursive: true, force: true })
}
mkdirSync(DOCS_DIR, { recursive: true })
// Create tmp directory if it doesn't exist
if (!existsSync(DOCS_ZIP_DIR)) {
mkdirSync(DOCS_ZIP_DIR, { recursive: true })
}
// Download the zip file if it doesn't exist
if (!existsSync(ZIP_PATH)) {
console.log('Downloading docs cache...')
const response = await fetch(DOC_CACHE_URL)
const buffer = await response.arrayBuffer()
writeFileSync(ZIP_PATH, Buffer.from(buffer))
} else {
console.log('Using existing docs cache...')
}
// Unzip only markdown files
console.log('Unzipping markdown files...')
const zip = new AdmZip(ZIP_PATH)
const entries = zip.getEntries()
entries.forEach((entry) => {
if (entry.entryName.match(/\.(md|mdx)$/i)) {
console.log(`Extracting: ${entry.entryName}`)
zip.extractEntryTo(entry, DOCS_DIR, true, true) // Changed to true to maintain folder structure
}
})
// Copy sitemap.xml if it exists
const sitemapEntry = entries.find(
(entry) => entry.entryName === 'sitemap.xml'
)
if (sitemapEntry) {
console.log('Extracting: sitemap.xml')
zip.extractEntryTo(sitemapEntry, DOCS_DIR, true, true)
}
console.log('Download and extraction complete!')
} catch (error) {
console.error('Error:', error)
process.exit(1)
}
}
downloadAndUnzip()