Fork of Chiri for Astro for my blog
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at e67947f5890b314dfbccb66501cfde2507d7f2d8 84 lines 3.0 kB view raw
1// toggle-proxy.ts 2import fs from 'fs' 3import path from 'path' 4import { fileURLToPath } from 'url' 5 6// Compatible with ES module __dirname 7const __filename = fileURLToPath(import.meta.url) 8const __dirname = path.dirname(__filename) 9 10const configPath = path.resolve(__dirname, '../src/config.ts') 11const proxyPath = path.resolve(__dirname, '../src/pages/api/proxy.ts') 12const backupPath = path.resolve(__dirname, '../src/pages/api/proxy.ts.bak') 13const astroConfigPath = path.resolve(__dirname, '../astro.config.ts') 14 15// Read config.ts content 16const configContent = fs.readFileSync(configPath, 'utf-8') 17 18// Use regex to extract linkCard config (assuming the format does not change) 19const match = configContent.match(/linkCard:\s*(true|false)/) 20if (!match) { 21 console.error('linkCard config not found') 22 process.exit(1) 23} 24const linkCardEnabled: boolean = match[1] === 'true' 25 26// Helper to comment/uncomment adapter lines in astro.config.ts 27function toggleAstroAdapter(comment: boolean) { 28 const astroConfig = fs.readFileSync(astroConfigPath, 'utf-8').split('\n') 29 30 // Find the import line for netlify adapter (including commented lines) 31 const importIndex = astroConfig.findIndex( 32 (line) => line.trim().includes('import') && line.includes('netlify') 33 ) 34 35 // Find the adapter line (including commented lines) 36 const adapterIndex = astroConfig.findIndex( 37 (line) => line.trim().includes('adapter:') && line.includes('netlify') 38 ) 39 40 if (importIndex === -1 || adapterIndex === -1) { 41 console.error('Could not find netlify adapter import or configuration') 42 return 43 } 44 45 if (comment) { 46 // Comment out the import line if not already commented 47 if (!astroConfig[importIndex].trim().startsWith('//')) { 48 astroConfig[importIndex] = '// ' + astroConfig[importIndex] 49 } 50 // Comment out the adapter line if not already commented 51 if (!astroConfig[adapterIndex].trim().startsWith('//')) { 52 astroConfig[adapterIndex] = '// ' + astroConfig[adapterIndex] 53 } 54 } else { 55 // Uncomment the import line if commented 56 if (astroConfig[importIndex].trim().startsWith('//')) { 57 astroConfig[importIndex] = astroConfig[importIndex].replace(/^\/\/\s?/, '') 58 } 59 // Uncomment the adapter line if commented 60 if (astroConfig[adapterIndex].trim().startsWith('//')) { 61 astroConfig[adapterIndex] = astroConfig[adapterIndex].replace(/^\/\/\s?/, '') 62 } 63 } 64 65 fs.writeFileSync(astroConfigPath, astroConfig.join('\n'), 'utf-8') 66} 67 68if (!linkCardEnabled) { 69 // If linkCard is disabled, rename proxy.ts and comment adapter 70 if (fs.existsSync(proxyPath)) { 71 fs.renameSync(proxyPath, backupPath) 72 console.log('🟡 proxy.ts disabled') 73 } 74 toggleAstroAdapter(true) 75 console.log('🟡 adapter config disabled') 76} else { 77 // If linkCard is enabled, restore proxy.ts and uncomment adapter 78 if (fs.existsSync(backupPath)) { 79 fs.renameSync(backupPath, proxyPath) 80 console.log('🟢 proxy.ts enabled') 81 } 82 toggleAstroAdapter(false) 83 console.log('🟢 adapter config enabled') 84}