|
1 | 1 | import { exec } from "node:child_process"; |
2 | | -import { promisify } from "node:util"; |
3 | | - |
4 | | -const execAsync = promisify(exec); |
5 | | - |
6 | 2 | import fs from "node:fs"; |
| 3 | +import { createRequire } from "node:module"; |
7 | 4 | import path from "node:path"; |
| 5 | +import { promisify } from "node:util"; |
8 | 6 |
|
9 | 7 | import * as p from "@clack/prompts"; |
10 | 8 | import { Command } from "commander"; |
@@ -32,6 +30,9 @@ import { |
32 | 30 | } from "../utils/env"; |
33 | 31 | import { capture } from "../utils/telemetry"; |
34 | 32 |
|
| 33 | +const execAsync = promisify(exec); |
| 34 | +const require = createRequire(import.meta.url); |
| 35 | + |
35 | 36 | function ensureDir(filePath: string): void { |
36 | 37 | const dir = path.dirname(filePath); |
37 | 38 | if (!fs.existsSync(dir)) { |
@@ -64,6 +65,22 @@ function stripeConfig(): string { |
64 | 65 | }`; |
65 | 66 | } |
66 | 67 |
|
| 68 | +export function detectPaykitCli(): boolean { |
| 69 | + try { |
| 70 | + require.resolve("paykitjs/package.json"); |
| 71 | + return true; |
| 72 | + } catch { |
| 73 | + return false; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export function getWebhookListenCommand(port: number, hasPaykitCli = detectPaykitCli()): string { |
| 78 | + const path = `localhost:${String(port)}/paykit/webhook`; |
| 79 | + return hasPaykitCli |
| 80 | + ? `paykitjs listen --forward-to ${path}` |
| 81 | + : `stripe listen --forward-to ${path}`; |
| 82 | +} |
| 83 | + |
67 | 84 | function generateConfigFile(templateId: string, includeIdentify: boolean): string { |
68 | 85 | const productImports = |
69 | 86 | templateId === "saas-starter" |
@@ -548,8 +565,7 @@ async function initAction(options: { cwd: string; defaults: boolean }): Promise< |
548 | 565 | const exec = getExecPrefix(pm); |
549 | 566 | const c = picocolors.cyan; |
550 | 567 | const b = picocolors.bold; |
551 | | - const webhookCommand = "stripe listen --forward-to localhost:3000/paykit/webhook"; |
552 | | - // TODO: replace with paykitjs listen |
| 568 | + const webhookCommand = getWebhookListenCommand(3000); |
553 | 569 |
|
554 | 570 | const isRerun = files.length === 0; |
555 | 571 | const heading = isRerun |
|
0 commit comments