Skip to content

Commit a4d6ac7

Browse files
committed
more fixes
1 parent 4858033 commit a4d6ac7

File tree

7 files changed

+69
-7
lines changed

7 files changed

+69
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"license": "MIT",
2222
"devDependencies": {
2323
"@eslint/js": "^9.29.0",
24-
"@types/node": "^20.17.24",
24+
"@types/node": "catalog:",
2525
"eslint": "~9.29.0",
2626
"glob": "^11.0.2",
2727
"prettier": "^3.5.3",

packages/cli/src/actions/generate.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string)
6969
throw new CliError(`Unknown core plugin: ${provider}`);
7070
}
7171
} else {
72+
let moduleSpec = provider;
73+
if (moduleSpec.startsWith('.')) {
74+
// relative to schema's path
75+
moduleSpec = path.resolve(path.dirname(schemaFile), moduleSpec);
76+
}
7277
try {
73-
cliPlugin = (await import(provider)).default as CliPlugin;
78+
cliPlugin = (await import(moduleSpec)).default as CliPlugin;
7479
} catch (error) {
7580
throw new CliError(`Failed to load plugin ${provider}: ${error}`);
7681
}

packages/cli/src/plugins/prisma.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ const plugin: CliPlugin = {
66
name: 'Prisma Schema Generator',
77
statusText: 'Generating Prisma schema',
88
async generate({ model, defaultOutputPath, pluginOptions }) {
9-
let outFile = path.join(defaultOutputPath, 'schema.prisma');
9+
let outDir = defaultOutputPath;
1010
if (typeof pluginOptions['output'] === 'string') {
11-
const outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
11+
outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
1212
if (!fs.existsSync(outDir)) {
1313
fs.mkdirSync(outDir, { recursive: true });
1414
}
15-
outFile = path.join(outDir, 'schema.prisma');
1615
}
1716
const prismaSchema = await new PrismaSchemaGenerator(model).generate();
18-
fs.writeFileSync(outFile, prismaSchema);
17+
fs.writeFileSync(path.join(outDir, 'schema.prisma'), prismaSchema);
1918
},
2019
};
2120

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { describe, expect, it } from 'vitest';
4+
import { createProject, runCli } from '../utils';
5+
import { execSync } from 'node:child_process';
6+
7+
describe('Custom plugins tests', () => {
8+
it('runs custom plugin generator', () => {
9+
const workDir = createProject(`
10+
plugin custom {
11+
provider = '../my-plugin.js'
12+
output = '../custom-output'
13+
}
14+
15+
model User {
16+
id String @id @default(cuid())
17+
}
18+
`);
19+
20+
fs.writeFileSync(
21+
path.join(workDir, 'my-plugin.ts'),
22+
`
23+
import type { CliPlugin } from '@zenstackhq/sdk';
24+
import fs from 'node:fs';
25+
import path from 'node:path';
26+
27+
const plugin: CliPlugin = {
28+
name: 'Custom Generator',
29+
statusText: 'Generating foo.txt',
30+
async generate({ model, defaultOutputPath, pluginOptions }) {
31+
let outDir = defaultOutputPath;
32+
if (typeof pluginOptions['output'] === 'string') {
33+
outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
34+
if (!fs.existsSync(outDir)) {
35+
fs.mkdirSync(outDir, { recursive: true });
36+
}
37+
}
38+
fs.writeFileSync(path.join(outDir, 'foo.txt'), 'from my plugin');
39+
},
40+
};
41+
42+
export default plugin;
43+
`,
44+
);
45+
46+
execSync('npx tsc', { cwd: workDir });
47+
runCli('generate', workDir);
48+
expect(fs.existsSync(path.join(workDir, 'custom-output/foo.txt'))).toBe(true);
49+
});
50+
});

packages/testtools/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"pg": "^8.13.1"
4242
},
4343
"devDependencies": {
44+
"@types/node": "catalog:",
4445
"@types/tmp": "catalog:",
4546
"@zenstackhq/eslint-config": "workspace:*",
4647
"@zenstackhq/typescript-config": "workspace:*"

pnpm-lock.yaml

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ catalog:
1010
langium-cli: 3.5.0
1111
ts-pattern: ^5.7.1
1212
typescript: ^5.0.0
13+
'@types/node': ^20.17.24
1314
tmp: ^0.2.3
1415
'@types/tmp': ^0.2.6

0 commit comments

Comments
 (0)