|
1 | | -import * as fs from "node:fs"; |
2 | | -import * as path from "node:path"; |
3 | | -import * as clack from "@clack/prompts"; |
4 | | -import { CYAN, RESET } from "./colors"; |
| 1 | +import * as fs from 'node:fs' |
| 2 | +import * as path from 'node:path' |
| 3 | +import { getDotEnvTemplate, getEnvModuleTemplate, getServerEntryTemplate, getWebhookIndexTemplate } from './templates' |
5 | 4 |
|
6 | | -/** Returns the target handler file path for a framework/platform pair. */ |
7 | 5 | export function getFilePath(framework: string, platform: string): string { |
8 | | - const cwd = process.cwd(); |
9 | | - const hasSrc = fs.existsSync(path.join(cwd, "src")); |
10 | | - |
11 | 6 | switch (framework) { |
12 | | - case "nextjs": |
13 | | - if (fs.existsSync(path.join(cwd, "src/app"))) { |
14 | | - return `src/app/api/webhooks/${platform}/route.ts`; |
15 | | - } |
16 | | - if (fs.existsSync(path.join(cwd, "app"))) { |
17 | | - return `app/api/webhooks/${platform}/route.ts`; |
18 | | - } |
19 | | - return `app/api/webhooks/${platform}/route.ts`; |
| 7 | + case 'nextjs': { |
| 8 | + const cwd = process.cwd() |
| 9 | + if (fs.existsSync(path.join(cwd, 'src/app'))) return `src/app/api/webhooks/${platform}/route.ts` |
| 10 | + return `app/api/webhooks/${platform}/route.ts` |
| 11 | + } |
| 12 | + case 'express': |
| 13 | + case 'hono': |
| 14 | + return `src/routes/webhooks/${platform}.ts` |
| 15 | + case 'cloudflare': |
| 16 | + return 'src/index.ts' |
| 17 | + default: |
| 18 | + return `webhooks/${platform}.ts` |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export function getWebhookPath(framework: string, platform: string): string { |
| 23 | + return framework === 'nextjs' ? `/api/webhooks/${platform}` : `/webhooks/${platform}` |
| 24 | +} |
20 | 25 |
|
21 | | - case "express": |
22 | | - return hasSrc |
23 | | - ? `src/routes/webhooks/${platform}.ts` |
24 | | - : `routes/webhooks/${platform}.ts`; |
| 26 | +export function createHandlerFile(filePath: string, content: string): void { |
| 27 | + const fullPath = path.join(process.cwd(), filePath) |
| 28 | + fs.mkdirSync(path.dirname(fullPath), { recursive: true }) |
| 29 | + if (!fs.existsSync(fullPath)) { |
| 30 | + fs.writeFileSync(fullPath, content, 'utf8') |
| 31 | + return |
| 32 | + } |
| 33 | + const current = fs.readFileSync(fullPath, 'utf8') |
| 34 | + if (current !== content) fs.writeFileSync(fullPath, content, 'utf8') |
| 35 | +} |
25 | 36 |
|
26 | | - case "hono": |
27 | | - return hasSrc |
28 | | - ? `src/routes/webhooks/${platform}.ts` |
29 | | - : `src/routes/webhooks/${platform}.ts`; |
| 37 | +export function createSupportFiles(framework: string, platform: string, envVar: string): void { |
| 38 | + if (framework === 'hono' || framework === 'express') { |
| 39 | + const routerIndex = getWebhookIndexTemplate(framework, platform) |
| 40 | + const entry = getServerEntryTemplate(framework) |
| 41 | + if (routerIndex) createHandlerFile('src/routes/webhooks/index.ts', routerIndex) |
| 42 | + if (entry) createHandlerFile('src/index.ts', entry) |
| 43 | + createHandlerFile('src/env.ts', getEnvModuleTemplate(envVar)) |
| 44 | + ensureNodeTsConfig() |
| 45 | + } |
30 | 46 |
|
31 | | - case "cloudflare": |
32 | | - return hasSrc ? `src/webhooks/${platform}.ts` : `webhooks/${platform}.ts`; |
| 47 | + ensureDotEnv(envVar) |
| 48 | + ensureGitIgnoreDotEnv() |
| 49 | + ensurePackageScripts(framework) |
| 50 | +} |
33 | 51 |
|
34 | | - default: |
35 | | - return `webhooks/${platform}.ts`; |
| 52 | +function ensureDotEnv(envVar: string): void { |
| 53 | + const envPath = path.join(process.cwd(), '.env') |
| 54 | + if (!fs.existsSync(envPath)) { |
| 55 | + fs.writeFileSync(envPath, getDotEnvTemplate(envVar), 'utf8') |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const existing = fs.readFileSync(envPath, 'utf8') |
| 60 | + if (!existing.includes(`${envVar}=`)) { |
| 61 | + fs.writeFileSync(envPath, `${getDotEnvTemplate(envVar)}\n${existing}`, 'utf8') |
36 | 62 | } |
37 | 63 | } |
38 | 64 |
|
39 | | -/** Returns webhook route path used by tern config and forwarding. */ |
40 | | -export function getWebhookPath(platform: string): string { |
41 | | - return `/api/webhooks/${platform}`; |
| 65 | +function ensureGitIgnoreDotEnv(): void { |
| 66 | + const ignorePath = path.join(process.cwd(), '.gitignore') |
| 67 | + if (!fs.existsSync(ignorePath)) { |
| 68 | + fs.writeFileSync(ignorePath, '.env\n', 'utf8') |
| 69 | + return |
| 70 | + } |
| 71 | + const existing = fs.readFileSync(ignorePath, 'utf8') |
| 72 | + if (!existing.split('\n').includes('.env')) { |
| 73 | + fs.writeFileSync(ignorePath, `${existing.trimEnd()}\n.env\n`, 'utf8') |
| 74 | + } |
42 | 75 | } |
43 | 76 |
|
44 | | -/** Creates a handler file, confirming before overwrite. */ |
45 | | -export async function createHandlerFile( |
46 | | - filePath: string, |
47 | | - content: string, |
48 | | -): Promise<void> { |
49 | | - const fullPath = path.join(process.cwd(), filePath); |
| 77 | +function ensurePackageScripts(framework: string): void { |
| 78 | + const packagePath = path.join(process.cwd(), 'package.json') |
| 79 | + if (!fs.existsSync(packagePath)) return |
50 | 80 |
|
51 | | - if (fs.existsSync(fullPath)) { |
52 | | - const overwrite = await clack.confirm({ |
53 | | - message: `${path.basename(fullPath)} already exists. overwrite?`, |
54 | | - }); |
55 | | - if (clack.isCancel(overwrite) || !overwrite) { |
56 | | - clack.log.warn(`skipped ${filePath}`); |
57 | | - return; |
58 | | - } |
| 81 | + const raw = fs.readFileSync(packagePath, 'utf8') |
| 82 | + const pkg = JSON.parse(raw) as Record<string, any> |
| 83 | + pkg.scripts = pkg.scripts ?? {} |
| 84 | + |
| 85 | + if (framework === 'hono' || framework === 'express') { |
| 86 | + pkg.scripts.dev = 'tsx --env-file=.env watch src/index.ts' |
| 87 | + pkg.scripts.build = 'tsc' |
| 88 | + pkg.scripts.start = 'node --env-file=.env dist/index.js' |
| 89 | + } else if (framework === 'nextjs') { |
| 90 | + pkg.scripts.dev = 'next dev' |
| 91 | + pkg.scripts.build = 'next build' |
| 92 | + pkg.scripts.start = 'next start' |
| 93 | + } else if (framework === 'cloudflare') { |
| 94 | + pkg.scripts.dev = 'wrangler dev' |
| 95 | + pkg.scripts.deploy = 'wrangler deploy' |
59 | 96 | } |
60 | 97 |
|
61 | | - fs.mkdirSync(path.dirname(fullPath), { recursive: true }); |
62 | | - fs.writeFileSync(fullPath, content, "utf8"); |
63 | | - clack.log.success(`created ${CYAN}${filePath}${RESET}`); |
| 98 | + fs.writeFileSync(packagePath, `${JSON.stringify(pkg, null, 2)}\n`, 'utf8') |
| 99 | +} |
| 100 | + |
| 101 | +function ensureNodeTsConfig(): void { |
| 102 | + const tsconfigPath = path.join(process.cwd(), 'tsconfig.json') |
| 103 | + const config = { |
| 104 | + compilerOptions: { |
| 105 | + target: 'ES2022', |
| 106 | + module: 'NodeNext', |
| 107 | + moduleResolution: 'NodeNext', |
| 108 | + lib: ['ES2022'], |
| 109 | + strict: true, |
| 110 | + skipLibCheck: true, |
| 111 | + outDir: 'dist', |
| 112 | + rootDir: 'src', |
| 113 | + }, |
| 114 | + include: ['src'], |
| 115 | + } |
| 116 | + |
| 117 | + if (!fs.existsSync(tsconfigPath)) { |
| 118 | + fs.writeFileSync(tsconfigPath, `${JSON.stringify(config, null, 2)}\n`, 'utf8') |
| 119 | + } |
64 | 120 | } |
0 commit comments