Skip to content

Commit ed0474a

Browse files
authored
Merge branch 'main' into tofik/v2-security-questionnare-UI-UX-and-some-logic
2 parents 251f82a + b68d2dd commit ed0474a

File tree

4 files changed

+195
-4
lines changed

4 files changed

+195
-4
lines changed

apps/app/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"playwright-core": "^1.52.0",
100100
"posthog-js": "^1.236.6",
101101
"posthog-node": "^5.8.2",
102+
"prisma": "^6.13.0",
102103
"puppeteer-core": "^24.7.2",
103104
"react": "^19.1.1",
104105
"react-day-picker": "^8.10.1",
@@ -155,7 +156,6 @@
155156
"glob": "^11.0.3",
156157
"jsdom": "^26.1.0",
157158
"postcss": "^8.5.4",
158-
"prisma": "^6.13.0",
159159
"raw-loader": "^4.0.2",
160160
"tailwindcss": "^4.1.8",
161161
"typescript": "^5.8.3",
@@ -186,8 +186,6 @@
186186
"dev": "bun i && bunx concurrently --kill-others --names \"next,trigger\" --prefix-colors \"yellow,blue\" \"next dev --turbo -p 3000\" \"bunx trigger.dev@4.0.6 dev\"",
187187
"lint": "next lint && prettier --check .",
188188
"prebuild": "bun run db:generate",
189-
"postinstall": "node ./scripts/trigger-generate-prisma-client.js",
190-
"trigger:prisma:generate": "node ./scripts/trigger-generate-prisma-client.js",
191189
"start": "next start",
192190
"test": "vitest",
193191
"test:all": "./scripts/test-all.sh",

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/db/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"typescript": "^5.9.2"
1515
},
1616
"exports": {
17-
".": "./dist/index.js"
17+
".": "./dist/index.js",
18+
"./postinstall": "./dist/postinstall.js"
19+
},
20+
"bin": {
21+
"comp-prisma-postinstall": "./dist/postinstall.js"
1822
},
1923
"files": [
2024
"dist",

packages/db/src/postinstall.ts

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
 (0)