|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawnSync } from 'node:child_process'; |
| 4 | +import { copyFileSync, existsSync, mkdirSync } from 'node:fs'; |
| 5 | +import { dirname, resolve } from 'node:path'; |
| 6 | + |
| 7 | +type GenerateOptions = { |
| 8 | + projectRoot?: string; |
| 9 | + force?: boolean; |
| 10 | + log?: (message: string) => void; |
| 11 | +}; |
| 12 | + |
| 13 | +type SchemaResolution = { |
| 14 | + path?: string; |
| 15 | + searched: string[]; |
| 16 | +}; |
| 17 | + |
| 18 | +const executableName = process.platform === 'win32' ? 'prisma.cmd' : 'prisma'; |
| 19 | + |
| 20 | +export function generatePrismaClient(options: GenerateOptions = {}): { schema: string } { |
| 21 | + const projectRoot = options.projectRoot ?? process.cwd(); |
| 22 | + const log = options.log ?? ((message: string) => console.log(`[prisma-postinstall] ${message}`)); |
| 23 | + |
| 24 | + const resolution = resolveSchemaPath(projectRoot); |
| 25 | + |
| 26 | + if (!resolution.path) { |
| 27 | + throw new Error( |
| 28 | + [ |
| 29 | + 'Unable to locate schema.prisma from @trycompai/db.', |
| 30 | + 'Looked in the following locations:', |
| 31 | + ...resolution.searched.map((candidate) => ` - ${candidate}`), |
| 32 | + ].join('\n'), |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const schemaDir = resolve(projectRoot, 'prisma'); |
| 37 | + const schemaDestination = resolve(schemaDir, 'schema.prisma'); |
| 38 | + |
| 39 | + mkdirSync(schemaDir, { recursive: true }); |
| 40 | + copyFileSync(resolution.path, schemaDestination); |
| 41 | + log(`Copied schema from ${resolution.path} to ${schemaDestination}`); |
| 42 | + |
| 43 | + const clientEntryPoint = resolve(projectRoot, 'node_modules/.prisma/client/default.js'); |
| 44 | + if (!options.force && existsSync(clientEntryPoint)) { |
| 45 | + log('Prisma client already exists. Skipping generation.'); |
| 46 | + return { schema: schemaDestination }; |
| 47 | + } |
| 48 | + |
| 49 | + const prismaBinary = resolvePrismaBinary(projectRoot); |
| 50 | + |
| 51 | + if (!prismaBinary) { |
| 52 | + throw new Error( |
| 53 | + [ |
| 54 | + 'Prisma CLI not found in this workspace. Ensure "prisma" is installed.', |
| 55 | + `Checked paths:`, |
| 56 | + ...buildBinaryCandidates(projectRoot).map((candidate) => ` - ${candidate}`), |
| 57 | + ].join('\n'), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + log('Generating Prisma client for Trigger deploy...'); |
| 62 | + const result = spawnSync(prismaBinary, ['generate', `--schema=${schemaDestination}`], { |
| 63 | + cwd: projectRoot, |
| 64 | + stdio: 'inherit', |
| 65 | + env: { |
| 66 | + ...process.env, |
| 67 | + PRISMA_HIDE_UPDATE_MESSAGE: '1', |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + if (result.status !== 0) { |
| 72 | + throw new Error(`Prisma generate exited with code ${result.status ?? -1}`); |
| 73 | + } |
| 74 | + |
| 75 | + log('Prisma client generation complete.'); |
| 76 | + return { schema: schemaDestination }; |
| 77 | +} |
| 78 | + |
| 79 | +function resolveSchemaPath(projectRoot: string): SchemaResolution { |
| 80 | + const candidates = buildSchemaCandidates(projectRoot); |
| 81 | + const path = candidates.find((candidate) => existsSync(candidate)); |
| 82 | + return { path, searched: candidates }; |
| 83 | +} |
| 84 | + |
| 85 | +function buildSchemaCandidates(projectRoot: string): string[] { |
| 86 | + const candidates = new Set<string>(); |
| 87 | + |
| 88 | + const addCandidates = (start: string | undefined) => { |
| 89 | + if (!start) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + let current = start; |
| 94 | + while (true) { |
| 95 | + candidates.add(resolve(current, 'node_modules/@trycompai/db/dist/schema.prisma')); |
| 96 | + const parent = dirname(current); |
| 97 | + if (parent === current) { |
| 98 | + break; |
| 99 | + } |
| 100 | + current = parent; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + addCandidates(projectRoot); |
| 105 | + const initCwd = process.env.INIT_CWD; |
| 106 | + if (initCwd && initCwd !== projectRoot) { |
| 107 | + addCandidates(initCwd); |
| 108 | + } |
| 109 | + |
| 110 | + candidates.add(resolve(projectRoot, '../../packages/db/dist/schema.prisma')); |
| 111 | + candidates.add(resolve(projectRoot, '../packages/db/dist/schema.prisma')); |
| 112 | + |
| 113 | + return Array.from(candidates); |
| 114 | +} |
| 115 | + |
| 116 | +function resolvePrismaBinary(projectRoot: string): string | undefined { |
| 117 | + const candidates = buildBinaryCandidates(projectRoot); |
| 118 | + return candidates.find((candidate) => existsSync(candidate)); |
| 119 | +} |
| 120 | + |
| 121 | +function buildBinaryCandidates(projectRoot: string): string[] { |
| 122 | + const candidates = new Set<string>(); |
| 123 | + |
| 124 | + const addCandidates = (start: string | undefined) => { |
| 125 | + if (!start) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + let current = start; |
| 130 | + while (true) { |
| 131 | + candidates.add(resolve(current, 'node_modules', '.bin', executableName)); |
| 132 | + const parent = dirname(current); |
| 133 | + if (parent === current) { |
| 134 | + break; |
| 135 | + } |
| 136 | + current = parent; |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + addCandidates(projectRoot); |
| 141 | + const initCwd = process.env.INIT_CWD; |
| 142 | + if (initCwd && initCwd !== projectRoot) { |
| 143 | + addCandidates(initCwd); |
| 144 | + } |
| 145 | + |
| 146 | + return Array.from(candidates); |
| 147 | +} |
| 148 | + |
| 149 | +function shouldRunCli(force: boolean): boolean { |
| 150 | + if (force) { |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + if (process.env.TRIGGER_PRISMA_FORCE_GENERATE === '1') { |
| 155 | + return true; |
| 156 | + } |
| 157 | + |
| 158 | + return Boolean( |
| 159 | + process.env.TRIGGER_SECRET_KEY || |
| 160 | + process.env.TRIGGER_DEPLOYMENT || |
| 161 | + process.env.CI === 'true' || |
| 162 | + process.env.PRISMA_GENERATE_ON_INSTALL === '1', |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +function runCli() { |
| 167 | + const force = process.argv.includes('--force'); |
| 168 | + |
| 169 | + if (!shouldRunCli(force)) { |
| 170 | + process.exit(0); |
| 171 | + } |
| 172 | + |
| 173 | + try { |
| 174 | + generatePrismaClient({ projectRoot: process.cwd(), force }); |
| 175 | + } catch (error) { |
| 176 | + console.error('[prisma-postinstall] Failed to generate Prisma client:', error); |
| 177 | + process.exit(1); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +const executedAsScript = |
| 182 | + typeof require !== 'undefined' && typeof module !== 'undefined' && require.main === module; |
| 183 | + |
| 184 | +if (executedAsScript) { |
| 185 | + runCli(); |
| 186 | +} |
0 commit comments