@trycompai/db 1.3.16 → 1.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ type GenerateOptions = {
3
+ projectRoot?: string;
4
+ force?: boolean;
5
+ log?: (message: string) => void;
6
+ };
7
+ export declare function generatePrismaClient(options?: GenerateOptions): {
8
+ schema: string;
9
+ };
10
+ export {};
11
+ //# sourceMappingURL=postinstall.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postinstall.d.ts","sourceRoot":"","sources":["../src/postinstall.ts"],"names":[],"mappings":";AAMA,KAAK,eAAe,GAAG;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC,CAAC;AASF,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,eAAoB,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAyDtF"}
@@ -0,0 +1,138 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.generatePrismaClient = generatePrismaClient;
5
+ const node_child_process_1 = require("node:child_process");
6
+ const node_fs_1 = require("node:fs");
7
+ const node_path_1 = require("node:path");
8
+ const executableName = process.platform === 'win32' ? 'prisma.cmd' : 'prisma';
9
+ function generatePrismaClient(options = {}) {
10
+ const projectRoot = options.projectRoot ?? process.cwd();
11
+ const log = options.log ?? ((message) => console.log(`[prisma-postinstall] ${message}`));
12
+ const resolution = resolveSchemaPath(projectRoot);
13
+ if (!resolution.path) {
14
+ throw new Error([
15
+ 'Unable to locate schema.prisma from @trycompai/db.',
16
+ 'Looked in the following locations:',
17
+ ...resolution.searched.map((candidate) => ` - ${candidate}`),
18
+ ].join('\n'));
19
+ }
20
+ const schemaDir = (0, node_path_1.resolve)(projectRoot, 'prisma');
21
+ const schemaDestination = (0, node_path_1.resolve)(schemaDir, 'schema.prisma');
22
+ (0, node_fs_1.mkdirSync)(schemaDir, { recursive: true });
23
+ (0, node_fs_1.copyFileSync)(resolution.path, schemaDestination);
24
+ log(`Copied schema from ${resolution.path} to ${schemaDestination}`);
25
+ const clientEntryPoint = (0, node_path_1.resolve)(projectRoot, 'node_modules/.prisma/client/default.js');
26
+ if (!options.force && (0, node_fs_1.existsSync)(clientEntryPoint)) {
27
+ log('Prisma client already exists. Skipping generation.');
28
+ return { schema: schemaDestination };
29
+ }
30
+ const prismaBinary = resolvePrismaBinary(projectRoot);
31
+ if (!prismaBinary) {
32
+ throw new Error([
33
+ 'Prisma CLI not found in this workspace. Ensure "prisma" is installed.',
34
+ `Checked paths:`,
35
+ ...buildBinaryCandidates(projectRoot).map((candidate) => ` - ${candidate}`),
36
+ ].join('\n'));
37
+ }
38
+ log('Generating Prisma client for Trigger deploy...');
39
+ const result = (0, node_child_process_1.spawnSync)(prismaBinary, ['generate', `--schema=${schemaDestination}`], {
40
+ cwd: projectRoot,
41
+ stdio: 'inherit',
42
+ env: {
43
+ ...process.env,
44
+ PRISMA_HIDE_UPDATE_MESSAGE: '1',
45
+ },
46
+ });
47
+ if (result.status !== 0) {
48
+ throw new Error(`Prisma generate exited with code ${result.status ?? -1}`);
49
+ }
50
+ log('Prisma client generation complete.');
51
+ return { schema: schemaDestination };
52
+ }
53
+ function resolveSchemaPath(projectRoot) {
54
+ const candidates = buildSchemaCandidates(projectRoot);
55
+ const path = candidates.find((candidate) => (0, node_fs_1.existsSync)(candidate));
56
+ return { path, searched: candidates };
57
+ }
58
+ function buildSchemaCandidates(projectRoot) {
59
+ const candidates = new Set();
60
+ const addCandidates = (start) => {
61
+ if (!start) {
62
+ return;
63
+ }
64
+ let current = start;
65
+ while (true) {
66
+ candidates.add((0, node_path_1.resolve)(current, 'node_modules/@trycompai/db/dist/schema.prisma'));
67
+ const parent = (0, node_path_1.dirname)(current);
68
+ if (parent === current) {
69
+ break;
70
+ }
71
+ current = parent;
72
+ }
73
+ };
74
+ addCandidates(projectRoot);
75
+ const initCwd = process.env.INIT_CWD;
76
+ if (initCwd && initCwd !== projectRoot) {
77
+ addCandidates(initCwd);
78
+ }
79
+ candidates.add((0, node_path_1.resolve)(projectRoot, '../../packages/db/dist/schema.prisma'));
80
+ candidates.add((0, node_path_1.resolve)(projectRoot, '../packages/db/dist/schema.prisma'));
81
+ return Array.from(candidates);
82
+ }
83
+ function resolvePrismaBinary(projectRoot) {
84
+ const candidates = buildBinaryCandidates(projectRoot);
85
+ return candidates.find((candidate) => (0, node_fs_1.existsSync)(candidate));
86
+ }
87
+ function buildBinaryCandidates(projectRoot) {
88
+ const candidates = new Set();
89
+ const addCandidates = (start) => {
90
+ if (!start) {
91
+ return;
92
+ }
93
+ let current = start;
94
+ while (true) {
95
+ candidates.add((0, node_path_1.resolve)(current, 'node_modules', '.bin', executableName));
96
+ const parent = (0, node_path_1.dirname)(current);
97
+ if (parent === current) {
98
+ break;
99
+ }
100
+ current = parent;
101
+ }
102
+ };
103
+ addCandidates(projectRoot);
104
+ const initCwd = process.env.INIT_CWD;
105
+ if (initCwd && initCwd !== projectRoot) {
106
+ addCandidates(initCwd);
107
+ }
108
+ return Array.from(candidates);
109
+ }
110
+ function shouldRunCli(force) {
111
+ if (force) {
112
+ return true;
113
+ }
114
+ if (process.env.TRIGGER_PRISMA_FORCE_GENERATE === '1') {
115
+ return true;
116
+ }
117
+ return Boolean(process.env.TRIGGER_SECRET_KEY ||
118
+ process.env.TRIGGER_DEPLOYMENT ||
119
+ process.env.CI === 'true' ||
120
+ process.env.PRISMA_GENERATE_ON_INSTALL === '1');
121
+ }
122
+ function runCli() {
123
+ const force = process.argv.includes('--force');
124
+ if (!shouldRunCli(force)) {
125
+ process.exit(0);
126
+ }
127
+ try {
128
+ generatePrismaClient({ projectRoot: process.cwd(), force });
129
+ }
130
+ catch (error) {
131
+ console.error('[prisma-postinstall] Failed to generate Prisma client:', error);
132
+ process.exit(1);
133
+ }
134
+ }
135
+ const executedAsScript = typeof require !== 'undefined' && typeof module !== 'undefined' && require.main === module;
136
+ if (executedAsScript) {
137
+ runCli();
138
+ }