|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +import MailPace from '@mailpace/mailpace.js' |
| 5 | +import { createEnv } from '@t3-oss/env-core' |
| 6 | +import minimist from 'minimist' |
| 7 | +import { z } from 'zod' |
| 8 | + |
| 9 | +const gaRegexp = /^\d+\.\d+\.\d+$/ |
| 10 | +const canaryRegexp = /^(\d+)\.(\d+)\.(\d+)-canary\.(\d+)$/ |
| 11 | + |
| 12 | +const env = createEnv({ |
| 13 | + server: { |
| 14 | + CI: z |
| 15 | + .string() |
| 16 | + .optional() |
| 17 | + .transform(v => v === 'true'), |
| 18 | + MAILPACE_API_TOKEN: z.string(), |
| 19 | + EMAIL_ADDRESS_FROM: z.string().email(), |
| 20 | + EMAIL_ADDRESS_TO: z.string().email() |
| 21 | + }, |
| 22 | + isServer: true, |
| 23 | + runtimeEnv: process.env |
| 24 | +}) |
| 25 | + |
| 26 | +main() |
| 27 | + |
| 28 | +const fileSchema = z.object({ |
| 29 | + filename: z.string(), |
| 30 | + patch: z.string().optional() |
| 31 | +}) |
| 32 | + |
| 33 | +async function main() { |
| 34 | + const argv = minimist(process.argv.slice(2)) |
| 35 | + const thisVersion = argv.version |
| 36 | + if (gaRegexp.test(thisVersion)) { |
| 37 | + await sendGAEmail(thisVersion) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + const previousVersion = getPreviousVersion(argv.version) |
| 42 | + |
| 43 | + if (!previousVersion) { |
| 44 | + console.log('No previous version to compare with') |
| 45 | + process.exit(0) |
| 46 | + } |
| 47 | + const compareURL = `https://api.github.com/repos/vercel/next.js/compare/v${previousVersion}...v${thisVersion}` |
| 48 | + const compare = await fetch(compareURL).then(res => res.json()) |
| 49 | + const files = z.array(fileSchema).parse(compare.files) |
| 50 | + const appRouterFile = files.find( |
| 51 | + file => |
| 52 | + file.filename === 'packages/next/src/client/components/app-router.tsx' |
| 53 | + ) |
| 54 | + if (!appRouterFile) { |
| 55 | + console.log('No changes in app-router.tsx') |
| 56 | + process.exit(0) |
| 57 | + } |
| 58 | + await sendNotificationEmail(thisVersion, appRouterFile) |
| 59 | +} |
| 60 | + |
| 61 | +// -- |
| 62 | + |
| 63 | +/** |
| 64 | + * @param {string} version |
| 65 | + */ |
| 66 | +function getPreviousVersion(version) { |
| 67 | + const match = canaryRegexp.exec(version) |
| 68 | + if (!match || !match[4]) { |
| 69 | + return null |
| 70 | + } |
| 71 | + const canary = parseInt(match[4]) |
| 72 | + if (canary === 0) { |
| 73 | + return null |
| 74 | + } |
| 75 | + return `${match[1]}.${match[2]}.${match[3]}-canary.${canary - 1}` |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * |
| 80 | + * @param {string} thisVersion |
| 81 | + * @param {z.infer<typeof fileSchema>} appRouterFile |
| 82 | + * @returns |
| 83 | + */ |
| 84 | +async function sendNotificationEmail(thisVersion, appRouterFile) { |
| 85 | + const client = new MailPace.DomainClient(env.MAILPACE_API_TOKEN) |
| 86 | + const release = await fetch( |
| 87 | + `https://api.github.com/repos/vercel/next.js/releases/tags/v${thisVersion}` |
| 88 | + ).then(res => res.json()) |
| 89 | + const subject = `[nuqs] Next.js ${thisVersion} has app router changes` |
| 90 | + const body = `Release: ${release.html_url} |
| 91 | +
|
| 92 | +${release.body} |
| 93 | +--- |
| 94 | +
|
| 95 | +${ |
| 96 | + appRouterFile.patch |
| 97 | + ? `App router changes: |
| 98 | + \`\`\`diff |
| 99 | + ${appRouterFile.patch} |
| 100 | + \`\`\`` |
| 101 | + : 'No patch available' |
| 102 | +} |
| 103 | +` |
| 104 | + console.info('Sending email:', subject) |
| 105 | + console.info(body) |
| 106 | + if (!env.CI) { |
| 107 | + return |
| 108 | + } |
| 109 | + return client.sendEmail({ |
| 110 | + from: env.EMAIL_ADDRESS_FROM, |
| 111 | + to: env.EMAIL_ADDRESS_TO, |
| 112 | + subject, |
| 113 | + textbody: body, |
| 114 | + tags: ['nuqs'] |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @param {string} thisVersion |
| 120 | + */ |
| 121 | +function sendGAEmail(thisVersion) { |
| 122 | + const client = new MailPace.DomainClient(env.MAILPACE_API_TOKEN) |
| 123 | + const subject = `[nuqs] Next.js ${thisVersion} was published to GA` |
| 124 | + const body = `https://github.com/vercel/next.js/releases/tag/v${thisVersion}` |
| 125 | + console.info('Sending email:', subject) |
| 126 | + console.info(body) |
| 127 | + if (!env.CI) { |
| 128 | + return |
| 129 | + } |
| 130 | + return client.sendEmail({ |
| 131 | + from: env.EMAIL_ADDRESS_FROM, |
| 132 | + to: env.EMAIL_ADDRESS_TO, |
| 133 | + subject, |
| 134 | + textbody: body, |
| 135 | + tags: ['nuqs'] |
| 136 | + }) |
| 137 | +} |
0 commit comments