Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/trueberryless/the-thinking-hub. The central knowledge hub for 8 key Computer Science thinking modes. the-thinking-hub.felixs.dev
Something went wrong. Try again.
3.0 kB · 84 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485// 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')}