Repository navigation
feat: optional provider scenarios, shared webhook path, and demo deploy #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
729408c
feat(paykit): share webhook route
maxktz 9bd983f
feat(demo): support optional provider scenarios
maxktz d3c1ed2
feat(paykit): derive client api path
maxktz f411043
feat(paykit): rename plans config to products
maxktz 8dee078
Merge branch 'feat/webhook-same-path' into feat/ci-tests
maxktz 03219e5
Merge branch 'feat/webhook-same-path' into feat/ci-tests
maxktz 427b0c7
fix(demo): address review feedback
maxktz 2b67f05
fix(paykit): tighten init and env sync handling
maxktz f9af4fb
Merge branch 'main' into feat/ci-tests
maxktz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { requirePaykitPolar } from "./src/lib/paykit/polar"; | ||
|
|
||
| export const paykit = requirePaykitPolar(); | ||
| export default paykit; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { requirePaykitStripe } from "./src/lib/paykit/stripe"; | ||
|
|
||
| export const paykit = requirePaykitStripe(); | ||
| export default paykit; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import { spawnSync } from "node:child_process"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import dotenv from "dotenv"; | ||
|
|
||
| export type DeployTarget = "canary" | "prod"; | ||
|
|
||
| export const DEPLOY_TARGETS = { | ||
| canary: { | ||
| appUrl: "https://canary.paykit.sh", | ||
| envFile: ".env.canary.local", | ||
| projectId: "prj_yE0iPxCyx5cKn9mXTE93tbTHbf0O", | ||
| projectName: "pk-demo-canary", | ||
| }, | ||
| prod: { | ||
| appUrl: "https://demo.paykit.sh", | ||
| envFile: ".env.production.local", | ||
| projectId: "TODO_VERCEL_PROD_PROJECT_ID", | ||
| projectName: "demo-prod", | ||
| }, | ||
| }; | ||
|
|
||
| export const VERCEL_ORG_ID = "team_aTDm0BA9FeXnYnflix3JVLeS"; | ||
| const PAYKIT_PACKAGES = ["paykitjs", "@paykitjs/polar", "@paykitjs/stripe"]; | ||
|
|
||
| const REPO_ROOT = path.resolve(import.meta.dirname, "../../.."); | ||
| const DEMO_DIR = path.join(REPO_ROOT, "apps/demo"); | ||
|
|
||
| export function getDeployTarget(value: string | undefined): DeployTarget { | ||
| if (value === "canary" || value === "prod") return value; | ||
| throw new Error("Expected target to be 'canary' or 'prod'"); | ||
| } | ||
|
|
||
| export function assertTargetConfigured(target: DeployTarget): void { | ||
| const config = DEPLOY_TARGETS[target]; | ||
| if (VERCEL_ORG_ID.startsWith("TODO_")) { | ||
| throw new Error("Set VERCEL_ORG_ID in apps/demo/scripts/deploy-demo.ts"); | ||
| } | ||
| if (config.projectId.startsWith("TODO_")) { | ||
| throw new Error(`Set ${target} projectId in apps/demo/scripts/deploy-demo.ts`); | ||
| } | ||
| } | ||
|
|
||
| function run( | ||
| command: string, | ||
| args: string[], | ||
| options?: { cwd?: string; env?: Record<string, string> }, | ||
| ): string { | ||
| const result = spawnSync(command, args, { | ||
| cwd: options?.cwd ?? REPO_ROOT, | ||
| encoding: "utf8", | ||
| env: { ...process.env, ...options?.env }, | ||
| stdio: ["inherit", "pipe", "inherit"], | ||
| }); | ||
|
|
||
| if (result.status !== 0) { | ||
| throw new Error(`${command} ${args.join(" ")} failed`); | ||
| } | ||
|
|
||
| return result.stdout.trim(); | ||
| } | ||
|
|
||
| function runInherit( | ||
| command: string, | ||
| args: string[], | ||
| options?: { cwd?: string; env?: Record<string, string> }, | ||
| ): void { | ||
| const result = spawnSync(command, args, { | ||
| cwd: options?.cwd ?? REPO_ROOT, | ||
| env: { ...process.env, ...options?.env }, | ||
| stdio: "inherit", | ||
| }); | ||
|
|
||
| if (result.status !== 0) { | ||
| throw new Error(`${command} ${args.join(" ")} failed`); | ||
| } | ||
| } | ||
|
|
||
| function assertPackageVersionExists(packageName: string, version: string): void { | ||
| const resolved = run("npm", ["view", `${packageName}@${version}`, "version"]); | ||
| if (resolved !== version) { | ||
| throw new Error(`Expected ${packageName}@${version}, got ${resolved}`); | ||
| } | ||
| } | ||
|
|
||
| function resolvePackageVersion(selector: string): string { | ||
| return run("npm", ["view", `paykitjs@${selector}`, "version"]); | ||
| } | ||
|
|
||
| function copyRepo(tempRoot: string): void { | ||
| fs.cpSync(REPO_ROOT, tempRoot, { | ||
| recursive: true, | ||
| filter: (source) => { | ||
| const relative = path.relative(REPO_ROOT, source); | ||
| if (!relative) return true; | ||
| const parts = relative.split(path.sep); | ||
| return !parts.some((part) => | ||
| [".git", "node_modules", ".next", ".turbo", "dist", "coverage"].includes(part), | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function patchDemoPackage(tempRoot: string, version: string): void { | ||
| const packagePath = path.join(tempRoot, "apps/demo/package.json"); | ||
| const manifest = JSON.parse(fs.readFileSync(packagePath, "utf8")) as { | ||
| dependencies?: Record<string, string>; | ||
| }; | ||
|
|
||
| manifest.dependencies ??= {}; | ||
| delete manifest.dependencies["@paykitjs/dash"]; | ||
| for (const packageName of PAYKIT_PACKAGES) { | ||
| manifest.dependencies[packageName] = version; | ||
| } | ||
|
|
||
| fs.writeFileSync(packagePath, `${JSON.stringify(manifest, null, 2)}\n`); | ||
| } | ||
|
|
||
| function patchVercelConfig(tempRoot: string): void { | ||
| const vercelPath = path.join(tempRoot, "apps/demo/vercel.json"); | ||
| fs.writeFileSync( | ||
| vercelPath, | ||
| `${JSON.stringify( | ||
| { | ||
| buildCommand: "bun run build", | ||
| framework: "nextjs", | ||
| installCommand: "bun install", | ||
| }, | ||
| null, | ||
| 2, | ||
| )}\n`, | ||
| ); | ||
| } | ||
|
|
||
| function patchTsconfig(tempRoot: string): void { | ||
| const tsconfigPath = path.join(tempRoot, "apps/demo/tsconfig.json"); | ||
| const tsconfig = JSON.parse(fs.readFileSync(tsconfigPath, "utf8")) as { | ||
| compilerOptions?: Record<string, unknown>; | ||
| extends?: string; | ||
| }; | ||
|
|
||
| delete tsconfig.extends; | ||
| tsconfig.compilerOptions = { | ||
| strict: true, | ||
| target: "esnext", | ||
| module: "esnext", | ||
| moduleResolution: "bundler", | ||
| esModuleInterop: true, | ||
| verbatimModuleSyntax: true, | ||
| noUncheckedIndexedAccess: true, | ||
| exactOptionalPropertyTypes: false, | ||
| noErrorTruncation: true, | ||
| ...tsconfig.compilerOptions, | ||
| }; | ||
|
|
||
| fs.writeFileSync(tsconfigPath, `${JSON.stringify(tsconfig, null, 2)}\n`); | ||
| } | ||
|
|
||
| function deployDemo(): void { | ||
| const target = getDeployTarget(process.argv[2]); | ||
| const keepTemp = process.argv.includes("--keep-temp"); | ||
| const versionFlagIndex = process.argv.indexOf("--version"); | ||
| const versionArg = versionFlagIndex === -1 ? undefined : process.argv[versionFlagIndex + 1]; | ||
|
|
||
| assertTargetConfigured(target); | ||
|
|
||
| if (versionFlagIndex !== -1 && !versionArg) { | ||
| throw new Error(`Usage: bun deploy:${target} --version <paykit-version>`); | ||
| } | ||
|
|
||
| const npmSelector = versionArg ?? (target === "canary" ? "canary" : "latest"); | ||
| const config = DEPLOY_TARGETS[target]; | ||
| const envPath = path.join(DEMO_DIR, config.envFile); | ||
|
|
||
| if (!fs.existsSync(envPath)) { | ||
| throw new Error(`Missing ${config.envFile}`); | ||
| } | ||
|
|
||
| const demoEnv = dotenv.parse(fs.readFileSync(envPath)); | ||
| const version = resolvePackageVersion(npmSelector); | ||
|
|
||
| if (npmSelector !== version) { | ||
| console.log(`Resolved paykitjs@${npmSelector} -> ${version}`); | ||
| } | ||
|
|
||
| console.log(`Deploying ${config.projectName} with PayKit ${version}`); | ||
|
|
||
| for (const packageName of PAYKIT_PACKAGES) { | ||
| assertPackageVersionExists(packageName, version); | ||
| } | ||
|
|
||
| const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), `paykit-demo-${target}-`)); | ||
|
|
||
| try { | ||
| copyRepo(tempRoot); | ||
| patchDemoPackage(tempRoot, version); | ||
| patchVercelConfig(tempRoot); | ||
| patchTsconfig(tempRoot); | ||
|
|
||
| const tempDemoDir = path.join(tempRoot, "apps/demo"); | ||
| fs.copyFileSync(envPath, path.join(tempDemoDir, ".env.local")); | ||
|
|
||
| const vercelEnv = { | ||
| ...demoEnv, | ||
| VERCEL_ORG_ID, | ||
| VERCEL_PROJECT_ID: config.projectId, | ||
| }; | ||
|
|
||
| runInherit("bun", ["install"], { cwd: tempRoot, env: demoEnv }); | ||
| runInherit("bun", ["push"], { cwd: tempDemoDir, env: demoEnv }); | ||
| runInherit("bunx", ["vercel", "pull", "--yes", "--environment=production"], { | ||
| cwd: tempDemoDir, | ||
| env: vercelEnv, | ||
| }); | ||
| runInherit("bunx", ["vercel", "deploy", "--prod"], { cwd: tempDemoDir, env: vercelEnv }); | ||
| } finally { | ||
| if (keepTemp) { | ||
| console.log(`Kept temp deploy repo: ${tempRoot}`); | ||
| } else { | ||
| fs.rmSync(tempRoot, { recursive: true, force: true }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { | ||
| deployDemo(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.