Skip to content

Commit bef4630

Browse files
committed
chore: wip
1 parent c74c276 commit bef4630

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

.stacks/core/actions/src/domains/add.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import process from 'node:process'
2-
import { createHostedZone } from '@stacksjs/dns'
2+
import { createHostedZone, getNameservers, updateNameservers } from '@stacksjs/dns'
33
import { app } from '@stacksjs/config'
44
import { handleError } from '@stacksjs/error-handling'
55
import { italic, parseOptions, runCommand } from '@stacksjs/cli'
@@ -28,14 +28,23 @@ if (!options.domain) {
2828
}
2929
}
3030

31-
// check if hostedZone exists, if it does, delete
3231
const result = await createHostedZone(options.domain)
3332

3433
if (result.isErr()) {
3534
handleError(result.error)
3635
process.exit(1)
3736
}
3837

38+
// Update the nameservers
39+
const nameServers = await getNameservers(options.domain)
40+
41+
if (!nameServers) {
42+
handleError(`No nameservers found for domain: ${options.domain}`)
43+
process.exit(1)
44+
}
45+
46+
await updateNameservers(nameServers)
47+
3948
const nameservers = result.value
4049
const registrar: string = (await whois(options.domain, true)).parsedData.Registrar
4150

.stacks/core/buddy/src/commands/deploy.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { intro, italic, log, outro } from '@stacksjs/cli'
55
import { Action, ExitCode } from '@stacksjs/types'
66
import { Route53 } from '@aws-sdk/client-route-53'
77
import { app } from '@stacksjs/config'
8+
import { getNameservers, updateNameservers } from '@stacksjs/dns'
89

910
export function deploy(buddy: CLI) {
1011
const descriptions = {
@@ -79,6 +80,7 @@ export function deploy(buddy: CLI) {
7980
})
8081
}
8182

83+
// please note, this function also updates the user's nameservers if they are out of date
8284
async function hasUserDomainBeenAddedToCloud(domainName?: string) {
8385
const route53 = new Route53()
8486

@@ -88,9 +90,14 @@ async function hasUserDomainBeenAddedToCloud(domainName?: string) {
8890
return false
8991

9092
const existingHostedZone = existingHostedZones.HostedZones.find(zone => zone.Name === `${domainName}.`)
91-
if (existingHostedZone)
92-
return true
93+
if (existingHostedZone) {
94+
// need to ensure the user has updated their nameservers if they aren't up to date already
95+
const domainNameservers = await getNameservers(domainName)
96+
const hostedZoneDetail = await route53.getHostedZone({ Id: existingHostedZone.Id })
97+
const hostedZoneNameservers = hostedZoneDetail.DelegationSet?.NameServers || []
9398

99+
await updateNameservers(domainNameservers, hostedZoneNameservers, domainName)
100+
}
94101
return false
95102
}
96103

.stacks/core/dns/src/drivers/aws.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Route53 } from '@aws-sdk/client-route-53'
22
import { err, ok } from '@stacksjs/error-handling'
33
import { fs } from '@stacksjs/storage'
4+
import { config } from '@stacksjs/config'
45
import { path as p } from '@stacksjs/path'
6+
import { Route53Domains } from '@aws-sdk/client-route-53-domains'
57

68
export async function deleteHostedZone(domainName: string) {
79
const route53 = new Route53()
@@ -86,38 +88,31 @@ export async function createHostedZone(domainName: string) {
8688

8789
// Check if the hosted zone already exists
8890
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}.`)
9192

92-
const existingHostedZone = existingHostedZones.HostedZones.find(zone => zone.Name === `${domainName}.`)
93+
// if the hosted zone already exists, then we want to
9394
if (existingHostedZone)
94-
return err((`Hosted Zone already exists for domain: ${domainName}`))
95+
return ok(existingHostedZone)
9596

9697
// Create the hosted zone
97-
const hostedZone = await route53.createHostedZone({
98+
const createHostedZoneOutput = await route53.createHostedZone({
9899
Name: domainName,
99100
CallerReference: `${Date.now()}`,
100101
})
101102

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')
108105

109-
// await storage.writeFile(p.projectConfigPath('dns.ts'), JSON.stringify(dns))
110-
111-
return ok(nameServers)
106+
return ok(createHostedZoneOutput)
112107
}
113108

114-
function updateNameservers(nameservers: string[]) {
109+
export function writeNameserversToConfig(nameservers: string[]) {
115110
try {
116111
const path = p.projectConfigPath('dns.ts')
117112
const fileContent = fs.readFileSync(path, 'utf-8')
118113
const modifiedContent = fileContent.replace(
119114
/nameservers: \[.*?\]/s,
120-
`nameservers: [${nameservers.map(ns => `'${ns}'`).join(', ')}]`,
115+
`nameservers: [${nameservers.map(ns => `'${ns}'`).join(', ')}]`,
121116
)
122117
fs.writeFileSync(path, modifiedContent, 'utf-8')
123118
// eslint-disable-next-line no-console
@@ -147,9 +142,41 @@ export async function findHostedZone(domain: string) {
147142
}
148143
catch (error) {
149144
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)
151146
}
152147
}
153148

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

Comments
 (0)