-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: cli plugin support #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { default as prisma } from './prisma'; | ||
| export { default as typescript } from './typescript'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { PrismaSchemaGenerator, type CliPlugin } from '@zenstackhq/sdk'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| const plugin: CliPlugin = { | ||
| name: 'Prisma Schema Generator', | ||
| statusText: 'Generating Prisma schema', | ||
| async generate({ model, defaultOutputPath, pluginOptions }) { | ||
| let outDir = defaultOutputPath; | ||
| if (typeof pluginOptions['output'] === 'string') { | ||
| outDir = path.resolve(defaultOutputPath, pluginOptions['output']); | ||
| if (!fs.existsSync(outDir)) { | ||
| fs.mkdirSync(outDir, { recursive: true }); | ||
| } | ||
| } | ||
| const prismaSchema = await new PrismaSchemaGenerator(model).generate(); | ||
| fs.writeFileSync(path.join(outDir, 'schema.prisma'), prismaSchema); | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { CliPlugin } from '@zenstackhq/sdk'; | ||
| import { TsSchemaGenerator } from '@zenstackhq/sdk'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| const plugin: CliPlugin = { | ||
| name: 'TypeScript Schema Generator', | ||
| statusText: 'Generating TypeScript schema', | ||
| async generate({ model, defaultOutputPath, pluginOptions }) { | ||
| let outDir = defaultOutputPath; | ||
| if (typeof pluginOptions['output'] === 'string') { | ||
| outDir = path.resolve(defaultOutputPath, pluginOptions['output']); | ||
| if (!fs.existsSync(outDir)) { | ||
| fs.mkdirSync(outDir, { recursive: true }); | ||
| } | ||
| } | ||
| await new TsSchemaGenerator().generate(model, outDir); | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createProject, runCli } from '../utils'; | ||
| import { execSync } from 'node:child_process'; | ||
|
|
||
| describe('Custom plugins tests', () => { | ||
| it('runs custom plugin generator', () => { | ||
| const workDir = createProject(` | ||
| plugin custom { | ||
| provider = '../my-plugin.js' | ||
| output = '../custom-output' | ||
| } | ||
|
|
||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `); | ||
|
|
||
| fs.writeFileSync( | ||
| path.join(workDir, 'my-plugin.ts'), | ||
| ` | ||
| import type { CliPlugin } from '@zenstackhq/sdk'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| const plugin: CliPlugin = { | ||
| name: 'Custom Generator', | ||
| statusText: 'Generating foo.txt', | ||
| async generate({ model, defaultOutputPath, pluginOptions }) { | ||
| let outDir = defaultOutputPath; | ||
| if (typeof pluginOptions['output'] === 'string') { | ||
| outDir = path.resolve(defaultOutputPath, pluginOptions['output']); | ||
| if (!fs.existsSync(outDir)) { | ||
| fs.mkdirSync(outDir, { recursive: true }); | ||
| } | ||
| } | ||
| fs.writeFileSync(path.join(outDir, 'foo.txt'), 'from my plugin'); | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; | ||
| `, | ||
| ); | ||
|
|
||
| execSync('npx tsc', { cwd: workDir }); | ||
| runCli('generate', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'custom-output/foo.txt'))).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createProject, runCli } from '../utils'; | ||
|
|
||
| describe('Core plugins tests', () => { | ||
| it('can automatically generate a TypeScript schema with default output', () => { | ||
| const workDir = createProject(` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `); | ||
| runCli('generate', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true); | ||
| }); | ||
|
|
||
| it('can automatically generate a TypeScript schema with custom output', () => { | ||
| const workDir = createProject(` | ||
| plugin typescript { | ||
| provider = '@core/typescript' | ||
| output = '../generated-schema' | ||
| } | ||
|
|
||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `); | ||
| runCli('generate', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'generated-schema/schema.ts'))).toBe(true); | ||
| }); | ||
|
|
||
| it('can generate a Prisma schema with default output', () => { | ||
| const workDir = createProject(` | ||
| plugin prisma { | ||
| provider = '@core/prisma' | ||
| } | ||
|
|
||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `); | ||
| runCli('generate', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(true); | ||
| }); | ||
|
|
||
| it('can generate a Prisma schema with custom output', () => { | ||
| const workDir = createProject(` | ||
| plugin prisma { | ||
| provider = '@core/prisma' | ||
| output = './prisma' | ||
| } | ||
|
|
||
| model User { | ||
| id String @id @default(cuid()) | ||
| } | ||
| `); | ||
| runCli('generate', workDir); | ||
| expect(fs.existsSync(path.join(workDir, 'zenstack/prisma/schema.prisma'))).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,4 @@ | ||
| { | ||
| "extends": "@zenstackhq/typescript-config/base.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "." | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,4 @@ | ||
| { | ||
| "extends": "@zenstackhq/typescript-config/base.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "." | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,4 @@ | ||
| { | ||
| "extends": "@zenstackhq/typescript-config/base.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "." | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,4 @@ | ||
| { | ||
| "extends": "@zenstackhq/typescript-config/base.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "." | ||
| }, | ||
| "include": ["src/**/*"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,4 @@ | ||
| { | ||
| "extends": "@zenstackhq/typescript-config/base.json", | ||
| "compilerOptions": { | ||
| "baseUrl": "." | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.