Skip to content

Commit

Permalink
refactor: migrate scripts from .mjs to .ts
Browse files Browse the repository at this point in the history
Converts Node.js script files from .mjs to TypeScript:
- Updates package.json script commands to use .ts extension
- Removes legacy .mjs files (cf-dns, colo, monitor)
- Scripts functionality remains unchanged
- Prepares codebase for improved type safety and maintainability

Files will be recreated with TypeScript equivalents in a follow-up commit.
  • Loading branch information
ccbikai committed Dec 29, 2024
1 parent a558438 commit d78582e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"local": "vercel dev",
"worker": "wrangler dev",
"deploy": "wrangler deploy",
"postinstall": "node scripts/colo.mjs && simple-git-hooks || true",
"cf-dns": "tsx --env-file=.env scripts/cf-dns.mjs",
"monitor": "tsx scripts/monitor.mjs",
"dev:monitor": "tsx --env-file=.env scripts/monitor.mjs"
"postinstall": "tsx scripts/colo.ts && simple-git-hooks || true",
"cf-dns": "tsx --env-file=.env scripts/cf-dns.ts",
"monitor": "tsx scripts/monitor.ts",
"dev:monitor": "tsx --env-file=.env scripts/monitor.ts"
},
"dependencies": {
"@mdx-js/loader": "^3.0.1",
Expand Down
23 changes: 18 additions & 5 deletions scripts/cf-dns.mjs → scripts/cf-dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ import { CLOUDFLARE_REGIONS } from '../config/cloudflare'

const WORKER_HOST = process.env.WORKER_HOST

interface DNSRecord {
id: string
name: string
type: string
content: string
proxied: boolean
zone_id: string
}

const cloudflare = new Cloudflare({
apiToken: process.env.CLOUDFLARE_API_TOKEN,
})

async function updateDNS(existingRecords, dnsName, ip) {
async function updateDNS(
existingRecords: DNSRecord[],
dnsName: string,
ip: string,
): Promise<void> {
const existingRecord = existingRecords.find(record => record.name === dnsName)
if (existingRecord) {
if (existingRecord.content === ip) {
Expand Down Expand Up @@ -35,13 +48,13 @@ async function updateDNS(existingRecords, dnsName, ip) {
}
}

async function main() {
const { result: existingRecords = [] } = await cloudflare.dns.records.list({
zone_id: process.env.CLOUDFLARE_ZONE_ID,
async function main(): Promise<void> {
const { result: existingRecords = [] } = (await cloudflare.dns.records.list({
zone_id: process.env.CLOUDFLARE_ZONE_ID as string,
per_page: 5000000,
type: 'A',
search: WORKER_HOST,
})
})) as unknown as { result: DNSRecord[] }

for (const region in CLOUDFLARE_REGIONS) {
const dnsName = `${region}.${WORKER_HOST}`
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions scripts/monitor.mjs → scripts/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ if (process.env.http_proxy) {
setGlobalDispatcher(new ProxyAgent(process.env.http_proxy))
}

const { values: args = {} } = parseArgs({
const { values: args } = parseArgs({
options: {
regions: {
type: 'string',
},
},
})
}) as { values: { regions?: string } }

const WORKER_HOST = process.env.WORKER_HOST

Expand Down

0 comments on commit d78582e

Please sign in to comment.