Skip to content

Commit

Permalink
feat(nx-prisma): add support for prisma format
Browse files Browse the repository at this point in the history
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
kdawgwilk committed Nov 8, 2023
1 parent b6ad465 commit d43cc65
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/nx-prisma/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"schema": "./src/executors/deploy/schema.json",
"description": "The migrate deploy command applies all pending migrations, and creates the database if it does not exist. Primarily used in non-development environments."
},
"format": {
"implementation": "./src/executors/format/executor",
"schema": "./src/executors/format/schema.json",
"description": "Formats the Prisma Schema Language of the Prisma schema file."
},
"generate": {
"implementation": "./src/executors/generate/executor",
"schema": "./src/executors/generate/schema.json",
Expand Down
51 changes: 51 additions & 0 deletions packages/nx-prisma/src/executors/format/executor.spec.ts
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();
});
});
21 changes: 21 additions & 0 deletions packages/nx-prisma/src/executors/format/executor.ts
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;
};
12 changes: 12 additions & 0 deletions packages/nx-prisma/src/executors/format/schema.d.ts
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;
14 changes: 14 additions & 0 deletions packages/nx-prisma/src/executors/format/schema.json
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": []
}
12 changes: 12 additions & 0 deletions packages/nx-prisma/src/generators/configuration/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ describe('configuration generator', () => {
'prisma-deploy': {
executor: '@nx-tools/nx-prisma:deploy',
},
'prisma-format': {
executor: '@nx-tools/nx-prisma:format',
},
'prisma-generate': {
executor: '@nx-tools/nx-prisma:generate',
},
Expand Down Expand Up @@ -79,6 +82,12 @@ describe('configuration generator', () => {
schema: 'apps/mypkg/custom-dir/schema.prisma',
},
},
'prisma-format': {
executor: '@nx-tools/nx-prisma:format',
options: {
schema: 'apps/mypkg/custom-dir/schema.prisma',
},
},
'prisma-generate': {
executor: '@nx-tools/nx-prisma:generate',
options: {
Expand Down Expand Up @@ -159,6 +168,9 @@ describe('configuration generator', () => {
'prisma-deploy': {
executor: '@nx-tools/nx-prisma:deploy',
},
'prisma-format': {
executor: '@nx-tools/nx-prisma:format',
},
'prisma-generate': {
executor: '@nx-tools/nx-prisma:generate',
},
Expand Down
4 changes: 4 additions & 0 deletions packages/nx-prisma/src/generators/configuration/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export async function configurationGenerator(tree: Tree, options: ConfigurationG
executor: '@nx-tools/nx-prisma:deploy',
...executorOpts,
},
'prisma-format': {
executor: '@nx-tools/nx-prisma:format',
...executorOpts,
},
'prisma-generate': {
executor: '@nx-tools/nx-prisma:generate',
...executorOpts,
Expand Down

0 comments on commit d43cc65

Please sign in to comment.