|
1 | 1 | import { Route53 } from '@aws-sdk/client-route-53'
|
2 | 2 | import { err, ok } from '@stacksjs/error-handling'
|
3 | 3 | import { fs } from '@stacksjs/storage'
|
| 4 | +import { config } from '@stacksjs/config' |
4 | 5 | import { path as p } from '@stacksjs/path'
|
| 6 | +import { Route53Domains } from '@aws-sdk/client-route-53-domains' |
5 | 7 |
|
6 | 8 | export async function deleteHostedZone(domainName: string) {
|
7 | 9 | const route53 = new Route53()
|
@@ -86,38 +88,31 @@ export async function createHostedZone(domainName: string) {
|
86 | 88 |
|
87 | 89 | // Check if the hosted zone already exists
|
88 | 90 | const existingHostedZones = await route53.listHostedZonesByName({ DNSName: domainName })
|
89 |
| - if (!existingHostedZones || !existingHostedZones.HostedZones) |
90 |
| - return err((`No hosted zones found for domain: ${domainName}`)) |
| 91 | + const existingHostedZone = existingHostedZones.HostedZones?.find(zone => zone.Name === `${domainName}.`) |
91 | 92 |
|
92 |
| - const existingHostedZone = existingHostedZones.HostedZones.find(zone => zone.Name === `${domainName}.`) |
| 93 | + // if the hosted zone already exists, then we want to |
93 | 94 | if (existingHostedZone)
|
94 |
| - return err((`Hosted Zone already exists for domain: ${domainName}`)) |
| 95 | + return ok(existingHostedZone) |
95 | 96 |
|
96 | 97 | // Create the hosted zone
|
97 |
| - const hostedZone = await route53.createHostedZone({ |
| 98 | + const createHostedZoneOutput = await route53.createHostedZone({ |
98 | 99 | Name: domainName,
|
99 | 100 | CallerReference: `${Date.now()}`,
|
100 | 101 | })
|
101 | 102 |
|
102 |
| - // Update the nameservers |
103 |
| - const nameServers = hostedZone.DelegationSet?.NameServers |
104 |
| - if (!nameServers) |
105 |
| - return err((`No nameservers found for domain: ${domainName}`)) |
106 |
| - |
107 |
| - updateNameservers(nameServers) |
| 103 | + if (!createHostedZoneOutput.HostedZone) |
| 104 | + return err('Failed to create hosted zone') |
108 | 105 |
|
109 |
| - // await storage.writeFile(p.projectConfigPath('dns.ts'), JSON.stringify(dns)) |
110 |
| - |
111 |
| - return ok(nameServers) |
| 106 | + return ok(createHostedZoneOutput) |
112 | 107 | }
|
113 | 108 |
|
114 |
| -function updateNameservers(nameservers: string[]) { |
| 109 | +export function writeNameserversToConfig(nameservers: string[]) { |
115 | 110 | try {
|
116 | 111 | const path = p.projectConfigPath('dns.ts')
|
117 | 112 | const fileContent = fs.readFileSync(path, 'utf-8')
|
118 | 113 | const modifiedContent = fileContent.replace(
|
119 | 114 | /nameservers: \[.*?\]/s,
|
120 |
| - `nameservers: [${nameservers.map(ns => `'${ns}'`).join(', ')}]`, |
| 115 | + `nameservers: [${nameservers.map(ns => `'${ns}'`).join(', ')}]`, |
121 | 116 | )
|
122 | 117 | fs.writeFileSync(path, modifiedContent, 'utf-8')
|
123 | 118 | // eslint-disable-next-line no-console
|
@@ -147,9 +142,41 @@ export async function findHostedZone(domain: string) {
|
147 | 142 | }
|
148 | 143 | catch (error) {
|
149 | 144 | console.error(`Failed to find hosted zone for domain ${domain}:`, error)
|
150 |
| - return handleError(`Failed to find hosted zone for domain ${domain}:`, error) |
| 145 | + return handleError(`Failed to find hosted zone for domain ${domain}:`, error as Error) |
151 | 146 | }
|
152 | 147 | }
|
153 | 148 |
|
154 |
| -// deleteHostedZone('stacksjs.org').catch(console.error) |
155 |
| -// createHostedZone('stacksjs.org').catch(console.error) |
| 149 | +export async function getNameservers(domainName?: string) { |
| 150 | + if (!domainName) |
| 151 | + return [] |
| 152 | + |
| 153 | + const route53Domains = new Route53Domains({}) |
| 154 | + const domainDetail = await route53Domains.getDomainDetail({ DomainName: domainName }) |
| 155 | + return domainDetail?.Nameservers?.map(ns => ns.Name as string) || [] |
| 156 | +} |
| 157 | + |
| 158 | +export async function updateNameservers(hostedZoneNameservers: string[], domainNameservers?: string[], domainName?: string) { |
| 159 | + if (!domainName) |
| 160 | + domainName = config.app.url |
| 161 | + |
| 162 | + if (!domainNameservers) |
| 163 | + domainNameservers = await getNameservers(domainName) |
| 164 | + |
| 165 | + if (JSON.stringify(domainNameservers.sort()) !== JSON.stringify(hostedZoneNameservers.sort())) { |
| 166 | + log.info('Updating your domain nameservers to match the ones in your hosted zone...') |
| 167 | + |
| 168 | + const route53Domains = new Route53Domains({}) |
| 169 | + |
| 170 | + await route53Domains.updateDomainNameservers({ |
| 171 | + DomainName: domainName, |
| 172 | + Nameservers: hostedZoneNameservers.map(ns => ({ Name: ns })), |
| 173 | + }) |
| 174 | + |
| 175 | + writeNameserversToConfig(hostedZoneNameservers) |
| 176 | + |
| 177 | + log.info('Nameservers updated.') |
| 178 | + return |
| 179 | + } |
| 180 | + |
| 181 | + log.info('Your nameservers are up to date.') |
| 182 | +} |
0 commit comments