Skip to content

Commit

Permalink
feat(nx-prisma): compiled using swc
Browse files Browse the repository at this point in the history
  • Loading branch information
gperdomor committed Aug 5, 2024
1 parent d8ff729 commit bd67998
Show file tree
Hide file tree
Showing 32 changed files with 229 additions and 159 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/lib/logging.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as chalk from 'chalk';
import * as os from 'node:os';
import { logger } from './logging';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const ci = require('ci-info');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const chalk = require('chalk');

describe('Logging', () => {
beforeEach(() => {
Expand Down
29 changes: 29 additions & 0 deletions plugins/nx-prisma/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"jsc": {
"target": "es2021",
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true
},
"keepClassNames": true,
"externalHelpers": true,
"loose": true
},
"module": {
"type": "commonjs"
},
"sourceMaps": true,
"exclude": [
"jest.config.ts",
".*\\.spec.tsx?$",
".*\\.test.tsx?$",
"./src/jest-setup.ts$",
"./**/jest-setup.ts$",
".*.js$"
]
}
20 changes: 19 additions & 1 deletion plugins/nx-prisma/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
/* eslint-disable */
import { readFileSync } from 'fs';

// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
const { exclude: _, ...swcJestConfig } = JSON.parse(readFileSync(`${__dirname}/.swcrc`, 'utf-8'));

// disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves.
// If we do not disable this, SWC Core will read .swcrc and won't transform our test files due to "exclude"
if (swcJestConfig.swcrc === undefined) {
swcJestConfig.swcrc = false;
}

// Uncomment if using global setup/teardown files being transformed via swc
// https://nx.dev/nx-api/jest/documents/overview#global-setupteardown-with-nx-libraries
// jest needs EsModule Interop to find the default exported setup/teardown functions
// swcJestConfig.module.noInterop = false;

export default {
displayName: 'nx-prisma',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
moduleFileExtensions: ['ts', 'js', 'html'],
testEnvironment: '',
coverageDirectory: '../../coverage/plugins/nx-prisma',
};
2 changes: 1 addition & 1 deletion plugins/nx-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
},
"peerDependencies": {
"@nx/devkit": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
"@swc/helpers": "~0.5.11",
"prisma": "^5.0.0",
"ts-node": "^10.0.0",
"tslib": "^2.5.0",
"tsx": "^4.0.0"
},
"peerDependenciesMeta": {
Expand Down
2 changes: 1 addition & 1 deletion plugins/nx-prisma/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"tags": ["type:plugin", "scope:nx-prisma"],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"executor": "@nx/js:swc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/plugins/nx-prisma",
Expand Down
10 changes: 6 additions & 4 deletions plugins/nx-prisma/src/executors/deploy/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ jest.mock('@nx-tools/core', () => {
};
});

const mockContext: Partial<ExecutorContext> = {
const context: ExecutorContext = {
root: 'workspace-root',
workspace: { version: 2, projects: { foo: { root: 'apps/foo' } } },
projectName: 'foo',
cwd: process.cwd(),
isVerbose: false,
};

describe('Deploy Executor', () => {
it('empty options', async () => {
it('can run with empty options', async () => {
const options: DeployExecutorSchema = {};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(
expectCommandToHaveBeenCalled('npx prisma migrate deploy', [
'--schema=workspace-root/apps/foo/prisma/schema.prisma',
Expand All @@ -36,7 +38,7 @@ describe('Deploy Executor', () => {
const options: DeployExecutorSchema = {
[option]: value,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(expectCommandToHaveBeenCalled('npx prisma migrate deploy', [`--${option}=${value}`]));
expect(output.success).toBeTruthy();
}
Expand Down
11 changes: 5 additions & 6 deletions plugins/nx-prisma/src/executors/deploy/executor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { ExecutorContext } from '@nx/devkit';
import { ExecutorContext, PromiseExecutor } from '@nx/devkit';
import { runCommand } from '../../run-commands';
import { getDefaultScheme } from '../../utils';
import { DeployExecutorSchema } from './schema';

export default async function runExecutor(
options: DeployExecutorSchema,
ctx: ExecutorContext
): Promise<{ success: true }> {
const runExecutor: PromiseExecutor<DeployExecutorSchema> = async (options, ctx) => {
return runCommand(options, ctx, {
description: 'Deploying Database',
command: 'prisma migrate deploy',
getArgs,
});
}
};

const getArgs = (options: DeployExecutorSchema, ctx: ExecutorContext): string[] => {
const args = [];
Expand All @@ -22,3 +19,5 @@ const getArgs = (options: DeployExecutorSchema, ctx: ExecutorContext): string[]

return args;
};

export default runExecutor;
12 changes: 7 additions & 5 deletions plugins/nx-prisma/src/executors/format/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ jest.mock('@nx-tools/core', () => {
};
});

const mockContext: Partial<ExecutorContext> = {
const context: ExecutorContext = {
root: 'workspace-root',
workspace: { version: 2, projects: { foo: { root: 'apps/foo' } } },
projectName: 'foo',
cwd: process.cwd(),
isVerbose: false,
};

describe('Format Executor', () => {
it('empty options', async () => {
it('can run with empty options', async () => {
const options: FormatExecutorSchema = {};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(
expectCommandToHaveBeenCalled('npx prisma format', ['--schema=workspace-root/apps/foo/prisma/schema.prisma'])
);
Expand All @@ -34,7 +36,7 @@ describe('Format Executor', () => {
const options: FormatExecutorSchema = {
[option]: value,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(expectCommandToHaveBeenCalled('npx prisma format', [`--${option}=${value}`]));
expect(output.success).toBeTruthy();
}
Expand All @@ -44,7 +46,7 @@ describe('Format Executor', () => {
const options: FormatExecutorSchema = {
schema: 'my-schema.schema',
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(expectCommandToHaveBeenCalled('npx prisma format', ['--schema=my-schema.schema']));
expect(output.success).toBeTruthy();
});
Expand Down
8 changes: 5 additions & 3 deletions plugins/nx-prisma/src/executors/format/executor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ExecutorContext } from '@nx/devkit';
import { ExecutorContext, PromiseExecutor } 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 }> {
const runExecutor: PromiseExecutor<FormatExecutorSchema> = async (options, ctx) => {
return runCommand(options, ctx, {
description: 'Format schema',
command: 'prisma format',
getArgs,
});
}
};

const getArgs = (options: FormatExecutorSchema, ctx: ExecutorContext): string[] => {
const args = [];
Expand All @@ -19,3 +19,5 @@ const getArgs = (options: FormatExecutorSchema, ctx: ExecutorContext): string[]

return args;
};

export default runExecutor;
2 changes: 1 addition & 1 deletion plugins/nx-prisma/src/executors/format/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/schema",
"$schema": "https://json-schema.org/schema",
"version": 2,
"title": "Prisma format executor",
"description": "Formats the Prisma Schema Language of the Prisma schema file.",
Expand Down
14 changes: 8 additions & 6 deletions plugins/nx-prisma/src/executors/generate/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ jest.mock('@nx-tools/core', () => {
};
});

const mockContext: Partial<ExecutorContext> = {
const context: ExecutorContext = {
root: 'workspace-root',
workspace: { version: 2, projects: { foo: { root: 'apps/foo' } } },
projectName: 'foo',
cwd: process.cwd(),
isVerbose: false,
};

export const expectCommandToHaveBeenCalled = (cmd: string, args: string[]) => {
Expand All @@ -27,9 +29,9 @@ describe('Generate Executor', () => {
jest.spyOn(console, 'info').mockImplementation(() => true);
});

it('empty options', async () => {
it('can run with empty options', async () => {
const options: GenerateExecutorSchema = {};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(
expectCommandToHaveBeenCalled('npx prisma generate', ['--schema=workspace-root/apps/foo/prisma/schema.prisma'])
);
Expand All @@ -42,7 +44,7 @@ describe('Generate Executor', () => {
const options: GenerateExecutorSchema = {
[option]: value,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(expectCommandToHaveBeenCalled('npx prisma generate', [`--${option}=${value}`]));
expect(output.success).toBeTruthy();
}
Expand All @@ -54,7 +56,7 @@ describe('Generate Executor', () => {
const options: GenerateExecutorSchema = {
[flag]: true,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(
expectCommandToHaveBeenCalled('npx prisma generate', [
'--schema=workspace-root/apps/foo/prisma/schema.prisma',
Expand All @@ -72,7 +74,7 @@ describe('Generate Executor', () => {
generator: 'sample-generator',
watch: true,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(
expectCommandToHaveBeenCalled('npx prisma generate', [
'--schema=my-schema.schema',
Expand Down
11 changes: 5 additions & 6 deletions plugins/nx-prisma/src/executors/generate/executor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { ExecutorContext } from '@nx/devkit';
import { ExecutorContext, PromiseExecutor } from '@nx/devkit';
import { runCommand } from '../../run-commands';
import { getDefaultScheme } from '../../utils';
import { GenerateExecutorSchema } from './schema';

export default async function runExecutor(
options: GenerateExecutorSchema,
ctx: ExecutorContext
): Promise<{ success: true }> {
const runExecutor: PromiseExecutor<GenerateExecutorSchema> = async (options, ctx) => {
return runCommand(options, ctx, {
description: 'Generating Client',
command: 'prisma generate',
getArgs,
});
}
};

const getArgs = (options: GenerateExecutorSchema, ctx: ExecutorContext): string[] => {
const args = [];
Expand All @@ -35,3 +32,5 @@ const getArgs = (options: GenerateExecutorSchema, ctx: ExecutorContext): string[

return args;
};

export default runExecutor;
18 changes: 10 additions & 8 deletions plugins/nx-prisma/src/executors/migrate/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ jest.mock('node:child_process', () => {
};
});

const mockContext: Partial<ExecutorContext> = {
const context: ExecutorContext = {
root: 'workspace-root',
workspace: { version: 2, projects: { foo: { root: 'apps/foo' } } },
projectName: 'foo',
cwd: process.cwd(),
isVerbose: false,
};

describe('Migrate Executor', () => {
beforeEach(() => {
jest.spyOn(console, 'info').mockImplementation(() => true);
});

it('empty options', async () => {
it('can run with empty options', async () => {
const options: MigrateExecutorSchema = { name: 'mig-name' };
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(execSync).toHaveBeenCalledWith(
`npx prisma migrate dev --schema=workspace-root/apps/foo/prisma/schema.prisma --name=mig-name`,
`npx prisma migrate dev --schema=workspace-root/apps/foo/prisma/schema.prisma --name="mig-name"`,
{
stdio: 'inherit',
}
Expand All @@ -42,9 +44,9 @@ describe('Migrate Executor', () => {
name: 'users',
[flag]: true,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(execSync).toHaveBeenCalledWith(
`npx prisma migrate dev --schema=workspace-root/apps/foo/prisma/schema.prisma --name=users --${flag}`,
`npx prisma migrate dev --schema=workspace-root/apps/foo/prisma/schema.prisma --name="users" --${flag}`,
{
stdio: 'inherit',
}
Expand All @@ -61,9 +63,9 @@ describe('Migrate Executor', () => {
'skip-generate': true,
'skip-seed': true,
};
const output = await executor(options, mockContext as ExecutorContext);
const output = await executor(options, context);
expect(execSync).toHaveBeenCalledWith(
'npx prisma migrate dev --schema=my-schema.schema --name=migration-name --create-only --skip-generate --skip-seed',
'npx prisma migrate dev --schema=my-schema.schema --name="migration-name" --create-only --skip-generate --skip-seed',
{ stdio: 'inherit' }
);
expect(output.success).toBeTruthy();
Expand Down
13 changes: 6 additions & 7 deletions plugins/nx-prisma/src/executors/migrate/executor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { logger } from '@nx-tools/core';
import { ExecutorContext, getPackageManagerCommand } from '@nx/devkit';
import { ExecutorContext, getPackageManagerCommand, PromiseExecutor } from '@nx/devkit';
import { execSync } from 'node:child_process';
import { getDefaultScheme } from '../../utils';
import { MigrateExecutorSchema } from './schema';

export default async function runExecutor(
options: MigrateExecutorSchema,
ctx: ExecutorContext
): Promise<{ success: true }> {
const runExecutor: PromiseExecutor<MigrateExecutorSchema> = async (options, ctx) => {
const command = `${getPackageManagerCommand().exec} prisma migrate dev`;
const args = getArgs(options, ctx);

Expand All @@ -18,14 +15,14 @@ export default async function runExecutor(
});

return { success: true };
}
};

const getArgs = (options: MigrateExecutorSchema, ctx: ExecutorContext): string[] => {
const args = [];
const schema = options?.schema ?? getDefaultScheme(ctx);

args.push(`--schema=${schema}`);
args.push(`--name=${options.name}`);
args.push(`--name="${options.name}"`);

if (options?.['create-only']) {
args.push('--create-only');
Expand All @@ -41,3 +38,5 @@ const getArgs = (options: MigrateExecutorSchema, ctx: ExecutorContext): string[]

return args;
};

export default runExecutor;
Loading

0 comments on commit bd67998

Please sign in to comment.