-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-prisma): add support for prisma format
adds support to the generator to include adding a new executor that will run the prisma format command the formats the prisma schema closes #767
- Loading branch information
Showing
7 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,51 @@ | ||
import { ExecutorContext } from '@nx/devkit'; | ||
import { expectCommandToHaveBeenCalled } from '../generate/executor.spec'; | ||
import executor from './executor'; | ||
import { FormatExecutorSchema } from './schema'; | ||
|
||
jest.mock('@nx-tools/core', () => { | ||
const originalModule = jest.requireActual('@nx-tools/core'); | ||
return { | ||
__esModule: true, | ||
...originalModule, | ||
getExecOutput: jest.fn(async () => Promise.resolve({ stderr: '', exitCode: 0 })), | ||
}; | ||
}); | ||
|
||
const mockContext: Partial<ExecutorContext> = { | ||
root: 'workspace-root', | ||
workspace: { version: 2, projects: { foo: { root: 'apps/foo' } } }, | ||
projectName: 'foo', | ||
}; | ||
|
||
describe('Format Executor', () => { | ||
it('empty options', async () => { | ||
const options: FormatExecutorSchema = {}; | ||
const output = await executor(options, mockContext as ExecutorContext); | ||
expect( | ||
expectCommandToHaveBeenCalled('npx prisma format', ['--schema=workspace-root/apps/foo/prisma/schema.prisma']) | ||
); | ||
expect(output.success).toBeTruthy(); | ||
}); | ||
|
||
test.each([['schema', 'my-prisma-file.schema']])( | ||
'given %p option with %p value, should be handled has arg', | ||
async (option: keyof FormatExecutorSchema, value: string) => { | ||
const options: FormatExecutorSchema = { | ||
[option]: value, | ||
}; | ||
const output = await executor(options, mockContext as ExecutorContext); | ||
expect(expectCommandToHaveBeenCalled('npx prisma format', [`--${option}=${value}`])); | ||
expect(output.success).toBeTruthy(); | ||
} | ||
); | ||
|
||
it('with all options', async () => { | ||
const options: FormatExecutorSchema = { | ||
schema: 'my-schema.schema', | ||
}; | ||
const output = await executor(options, mockContext as ExecutorContext); | ||
expect(expectCommandToHaveBeenCalled('npx prisma format', ['--schema=my-schema.schema'])); | ||
expect(output.success).toBeTruthy(); | ||
}); | ||
}); |
This file contains 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 { ExecutorContext } from '@nx/devkit'; | ||
import { runCommand } from '../../run-commands'; | ||
import { getDefaultScheme } from '../../utils'; | ||
import { FormatExecutorSchema } from './schema'; | ||
|
||
export default async function run(options: FormatExecutorSchema, ctx: ExecutorContext): Promise<{ success: true }> { | ||
return runCommand(options, ctx, { | ||
description: 'Format schema', | ||
command: 'prisma format', | ||
getArgs, | ||
}); | ||
} | ||
|
||
const getArgs = (options: FormatExecutorSchema, ctx: ExecutorContext): string[] => { | ||
const args = []; | ||
const schema = options?.schema ?? getDefaultScheme(ctx); | ||
|
||
args.push(`--schema=${schema}`); | ||
|
||
return args; | ||
}; |
This file contains 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,12 @@ | ||
/** | ||
* This file was automatically generated by json-schema-to-typescript. | ||
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, | ||
* and run json-schema-to-typescript to regenerate this file. | ||
*/ | ||
|
||
import { PrismaBase } from '../../interfaces'; | ||
|
||
/** | ||
* Formats the Prisma Schema Language of the Prisma schema file. | ||
*/ | ||
export type FormatExecutorSchema = PrismaBase; |
This file contains 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,14 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"version": 2, | ||
"title": "Prisma format executor", | ||
"description": "Formats the Prisma Schema Language of the Prisma schema file.", | ||
"type": "object", | ||
"properties": { | ||
"schema": { | ||
"type": "string", | ||
"description": "Specifies the path to the desired schema.prisma file to be processed instead of the default path. Both absolute and relative paths are supported." | ||
} | ||
}, | ||
"required": [] | ||
} |
This file contains 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 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