Something went wrong. Try again.
Fork of Chiri for Astro for my blog forked from quillmatiq.com/augment
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081// toggle-proxy.tsimport fs from 'fs'import path from 'path'import { fileURLToPath } from 'url'
// Compatible with ES module __dirnameconst __filename = fileURLToPath(import.meta.url)const __dirname = path.dirname(__filename)
const configPath = path.resolve(__dirname, '../src/config.ts')const proxyPath = path.resolve(__dirname, '../src/pages/api/proxy.ts')const backupPath = path.resolve(__dirname, '../src/pages/api/proxy.ts.bak')const astroConfigPath = path.resolve(__dirname, '../astro.config.ts')
// Read config.ts contentconst configContent = fs.readFileSync(configPath, 'utf-8')
// Use regex to extract linkCard config (assuming the format does not change)const match = configContent.match(/linkCard:\s*(true|false)/)if (!match) { console.error('linkCard config not found') process.exit(1)}const linkCardEnabled: boolean = match[1] === 'true'
// Helper to comment/uncomment adapter lines in astro.config.tsfunction toggleAstroAdapter(comment: boolean) { const astroConfig = fs.readFileSync(astroConfigPath, 'utf-8').split('\n')
// Find the import line for netlify adapter (including commented lines) const importIndex = astroConfig.findIndex((line) => line.trim().includes('import') && line.includes('netlify'))
// Find the adapter line (including commented lines) const adapterIndex = astroConfig.findIndex((line) => line.trim().includes('adapter:') && line.includes('netlify'))
if (importIndex === -1 || adapterIndex === -1) { console.error('Could not find netlify adapter import or configuration') return }
if (comment) { // Comment out the import line if not already commented if (!astroConfig[importIndex].trim().startsWith('//')) { astroConfig[importIndex] = '// ' + astroConfig[importIndex] } // Comment out the adapter line if not already commented if (!astroConfig[adapterIndex].trim().startsWith('//')) { astroConfig[adapterIndex] = '// ' + astroConfig[adapterIndex] } } else { // Uncomment the import line if commented if (astroConfig[importIndex].trim().startsWith('//')) { astroConfig[importIndex] = astroConfig[importIndex].replace(/^\/\/\s?/, '') } // Uncomment the adapter line if commented if (astroConfig[adapterIndex].trim().startsWith('//')) { astroConfig[adapterIndex] = astroConfig[adapterIndex].replace(/^\/\/\s?/, '') } }
fs.writeFileSync(astroConfigPath, astroConfig.join('\n'), 'utf-8')}
if (!linkCardEnabled) { // If linkCard is disabled, rename proxy.ts and comment adapter if (fs.existsSync(proxyPath)) { fs.renameSync(proxyPath, backupPath) console.log('🟡 proxy.ts disabled') } toggleAstroAdapter(true) console.log('🟡 adapter config disabled')} else { // If linkCard is enabled, restore proxy.ts and uncomment adapter if (fs.existsSync(backupPath)) { fs.renameSync(backupPath, proxyPath) console.log('🟢 proxy.ts enabled') } toggleAstroAdapter(false) console.log('🟢 adapter config enabled')}