Fork of Chiri for Astro for my blog
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((line) => line.trim().includes('import') && line.includes('netlify'))
32
33 // Find the adapter line (including commented lines)
34 const adapterIndex = astroConfig.findIndex((line) => line.trim().includes('adapter:') && line.includes('netlify'))
35
36 if (importIndex === -1 || adapterIndex === -1) {
37 console.error('Could not find netlify adapter import or configuration')
38 return
39 }
40
41 if (comment) {
42 // Comment out the import line if not already commented
43 if (!astroConfig[importIndex].trim().startsWith('//')) {
44 astroConfig[importIndex] = '// ' + astroConfig[importIndex]
45 }
46 // Comment out the adapter line if not already commented
47 if (!astroConfig[adapterIndex].trim().startsWith('//')) {
48 astroConfig[adapterIndex] = '// ' + astroConfig[adapterIndex]
49 }
50 } else {
51 // Uncomment the import line if commented
52 if (astroConfig[importIndex].trim().startsWith('//')) {
53 astroConfig[importIndex] = astroConfig[importIndex].replace(/^\/\/\s?/, '')
54 }
55 // Uncomment the adapter line if commented
56 if (astroConfig[adapterIndex].trim().startsWith('//')) {
57 astroConfig[adapterIndex] = astroConfig[adapterIndex].replace(/^\/\/\s?/, '')
58 }
59 }
60
61 fs.writeFileSync(astroConfigPath, astroConfig.join('\n'), 'utf-8')
62}
63
64if (!linkCardEnabled) {
65 // If linkCard is disabled, rename proxy.ts and comment adapter
66 if (fs.existsSync(proxyPath)) {
67 fs.renameSync(proxyPath, backupPath)
68 console.log('🟡 proxy.ts disabled')
69 }
70 toggleAstroAdapter(true)
71 console.log('🟡 adapter config disabled')
72} else {
73 // If linkCard is enabled, restore proxy.ts and uncomment adapter
74 if (fs.existsSync(backupPath)) {
75 fs.renameSync(backupPath, proxyPath)
76 console.log('🟢 proxy.ts enabled')
77 }
78 toggleAstroAdapter(false)
79 console.log('🟢 adapter config enabled')
80}