|
| 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 | +}); |
0 commit comments