Skip to content

Commit c2e9795

Browse files
authored
chore: add prisma generate errors and plugin options to telemetry (#491)
1 parent 270258b commit c2e9795

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

packages/schema/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"lint": "eslint src tests --ext ts",
7676
"test": "jest",
7777
"prepublishOnly": "pnpm build",
78-
"publish-dev": "pnpm publish --tag dev",
78+
"publish-dev": "pnpm publish --registry http://localhost:4873",
7979
"postinstall": "node bin/post-install.js"
8080
},
8181
"peerDependencies": {
@@ -101,6 +101,7 @@
101101
"promisify": "^0.0.3",
102102
"semver": "^7.3.8",
103103
"sleep-promise": "^9.1.0",
104+
"strip-color": "^0.1.0",
104105
"ts-morph": "^16.0.0",
105106
"upper-case-first": "^2.0.2",
106107
"uuid": "^9.0.0",
@@ -119,6 +120,7 @@
119120
"@types/node": "^14.18.32",
120121
"@types/pluralize": "^0.0.29",
121122
"@types/semver": "^7.3.13",
123+
"@types/strip-color": "^0.1.0",
122124
"@types/tmp": "^0.2.3",
123125
"@types/upper-case-first": "^1.1.2",
124126
"@types/uuid": "^8.3.4",

packages/schema/src/cli/plugin-runner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export class PluginRunner {
136136
'cli:plugin:error',
137137
{
138138
plugin: name,
139+
options,
139140
},
140141
async () => {
141142
let result = run(context.schema, options, dmmf, config);

packages/schema/src/plugins/prisma/schema-generator.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getDMMF } from '@prisma/internals';
12
import {
23
ArrayExpr,
34
AstNode,
@@ -35,8 +36,10 @@ import {
3536
import fs from 'fs';
3637
import { writeFile } from 'fs/promises';
3738
import path from 'path';
39+
import stripColor from 'strip-color';
3840
import { name } from '.';
3941
import { getStringLiteral } from '../../language-server/validator/utils';
42+
import telemetry from '../../telemetry';
4043
import { execSync } from '../../utils/exec-utils';
4144
import {
4245
ModelFieldType,
@@ -76,6 +79,7 @@ export default class PrismaSchemaGenerator {
7679

7780
async generate(model: Model, options: PluginOptions, config?: Record<string, string>) {
7881
const prisma = new PrismaModel();
82+
const warnings: string[] = [];
7983

8084
for (const decl of model.declarations) {
8185
switch (decl.$type) {
@@ -106,15 +110,37 @@ export default class PrismaSchemaGenerator {
106110
await writeFile(outFile, this.PRELUDE + prisma.toString());
107111

108112
if (options.format === true) {
109-
// run 'prisma format'
110-
await execSync(`npx prisma format --schema ${outFile}`);
113+
try {
114+
// run 'prisma format'
115+
await execSync(`npx prisma format --schema ${outFile}`);
116+
} catch {
117+
warnings.push(`Failed to format Prisma schema file`);
118+
}
111119
}
112120

113121
const generateClient = options.generateClient !== false;
114122

115123
if (generateClient) {
116-
// run 'prisma generate'
117-
await execSync(`npx prisma generate --schema ${outFile}`);
124+
try {
125+
// run 'prisma generate'
126+
await execSync(`npx prisma generate --schema ${outFile}`);
127+
} catch {
128+
await this.trackPrismaSchemaError(outFile);
129+
throw new PluginError(name, `Failed to run "prisma generate"`);
130+
}
131+
}
132+
133+
return warnings;
134+
}
135+
136+
private async trackPrismaSchemaError(schema: string) {
137+
try {
138+
await getDMMF({ datamodel: fs.readFileSync(schema, 'utf-8') });
139+
} catch (err) {
140+
if (err instanceof Error) {
141+
// eslint-disable-next-line @typescript-eslint/no-var-requires
142+
telemetry.track('prisma:error', { command: 'generate', message: stripColor(err.message) });
143+
}
118144
}
119145
}
120146

packages/schema/src/telemetry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export type TelemetryEvents =
2222
| 'cli:command:error'
2323
| 'cli:plugin:start'
2424
| 'cli:plugin:complete'
25-
| 'cli:plugin:error';
25+
| 'cli:plugin:error'
26+
| 'prisma:error';
2627

2728
/**
2829
* Utility class for sending telemetry

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)