forked from
quillmatiq.com/augment
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 // 16: import netlify..., 19: adapter: netlify() (0-based)
30 const importIdx = 16
31 const adapterIdx = 19
32 if (comment) {
33 if (!astroConfig[importIdx].trim().startsWith('//')) {
34 astroConfig[importIdx] = '// ' + astroConfig[importIdx]
35 }
36 if (!astroConfig[adapterIdx].trim().startsWith('//')) {
37 astroConfig[adapterIdx] = '// ' + astroConfig[adapterIdx]
38 }
39 } else {
40 if (astroConfig[importIdx].trim().startsWith('//')) {
41 astroConfig[importIdx] = astroConfig[importIdx].replace(/^\/\/\s?/, '')
42 }
43 if (astroConfig[adapterIdx].trim().startsWith('//')) {
44 astroConfig[adapterIdx] = astroConfig[adapterIdx].replace(/^\/\/\s?/, '')
45 }
46 }
47 fs.writeFileSync(astroConfigPath, astroConfig.join('\n'), 'utf-8')
48}
49
50if (!linkCardEnabled) {
51 // If linkCard is disabled, rename proxy.ts and comment adapter
52 if (fs.existsSync(proxyPath)) {
53 fs.renameSync(proxyPath, backupPath)
54 console.log('🟡 proxy.ts disabled')
55 }
56 toggleAstroAdapter(true)
57 console.log('🟡 adapter config commented')
58} else {
59 // If linkCard is enabled, restore proxy.ts and uncomment adapter
60 if (fs.existsSync(backupPath)) {
61 fs.renameSync(backupPath, proxyPath)
62 console.log('🔵 proxy.ts enabled')
63 }
64 toggleAstroAdapter(false)
65 console.log('🔵 adapter config uncommented')
66}