Skip to content

Commit

Permalink
feat(nx-prisma): allow to execute seeding script with tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehoczky committed Apr 10, 2024
1 parent 25e6edf commit 36e55c4
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 27 deletions.
453 changes: 450 additions & 3 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"prettier": "2.8.8",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"tsx": "^4.7.2",
"typescript": "5.4.3",
"validate-branch-name": "1.3.0",
"verdaccio": "^5.0.4"
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-prisma/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@nx/dependency-checks": [
"error",
{
"ignoredDependencies": ["prisma", "ts-node"]
"ignoredDependencies": ["prisma", "ts-node", "tsx"]
}
]
}
Expand Down
8 changes: 7 additions & 1 deletion packages/nx-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"@nx/devkit": "^16.0.0 || ^17.0.0 || ^18.0.0",
"prisma": "^5.0.0",
"ts-node": "^10.0.0",
"tslib": "^2.5.3"
"tslib": "^2.5.3",
"tsx": "^4.0.0"
},
"peerDependenciesMeta": {
"tsx": {
"optional": true
}
}
}
70 changes: 50 additions & 20 deletions packages/nx-prisma/src/executors/seed/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,57 @@ describe('Seed Executor', () => {
);
});

it('script option', async () => {
const options: SeedExecutorSchema = {
script: 'custom-seed-file.ts',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(
expectCommandToHaveBeenCalled('npx ts-node', [
'--project=workspace-folder/apps/foo/tsconfig.json',
'custom-seed-file.ts',
])
);
expect(output.success).toBeTruthy();
describe('with ts-node', () => {
it('script option', async () => {
const options: SeedExecutorSchema = {
script: 'custom-seed-file.ts',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(
expectCommandToHaveBeenCalled('npx ts-node', [
'--project=workspace-folder/apps/foo/tsconfig.json',
'custom-seed-file.ts',
])
);
expect(output.success).toBeTruthy();
});

it('with all options', async () => {
const options: SeedExecutorSchema = {
script: 'seed.ts',
tsConfig: 'tsconfig.base.ts',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(expectCommandToHaveBeenCalled('npx ts-node', ['--project=tsconfig.base.ts', 'seed.ts']));
expect(output.success).toBeTruthy();
});
});

it('with all options', async () => {
const options: SeedExecutorSchema = {
script: 'seed.ts',
tsConfig: 'tsconfig.base.ts',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(expectCommandToHaveBeenCalled('npx ts-node', ['--project=tsconfig.base.ts', 'seed.ts']));
expect(output.success).toBeTruthy();
describe('with tsx', () => {
it('script option', async () => {
const options: SeedExecutorSchema = {
script: 'custom-seed-file.ts',
executeWith: 'tsx',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(
expectCommandToHaveBeenCalled('npx tsx', [
'--tsconfig=workspace-folder/apps/foo/tsconfig.json',
'custom-seed-file.ts',
])
);
expect(output.success).toBeTruthy();
});

it('with all options', async () => {
const options: SeedExecutorSchema = {
script: 'seed.ts',
tsConfig: 'tsconfig.base.ts',
executeWith: 'tsx',
};
const output = await executor(options, mockContext as ExecutorContext);
expect(expectCommandToHaveBeenCalled('npx tsx', ['--tsconfig=tsconfig.base.ts', 'seed.ts']));
expect(output.success).toBeTruthy();
});
});
});
6 changes: 4 additions & 2 deletions packages/nx-prisma/src/executors/seed/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export default async function run(options: SeedExecutorSchema, ctx: ExecutorCont
if (!options.script) {
throw new Error('You must specify a seed script file.');
}
options.executeWith = options.executeWith ?? 'ts-node';

const command = `${getPackageManagerCommand().exec} ts-node`;
const command = `${getPackageManagerCommand().exec} ${options.executeWith}`;
const args = getArgs(options, ctx);

await logger.group('Seeding Database', async () => {
Expand All @@ -23,9 +24,10 @@ export default async function run(options: SeedExecutorSchema, ctx: ExecutorCont

const getArgs = (options: SeedExecutorSchema, ctx: ExecutorContext): string[] => {
const args = [];
const tsConfigArgName = options.executeWith === 'ts-node' ? 'project' : 'tsconfig';
const tsConfig = options?.tsConfig ?? joinPathFragments(getProjectRoot(ctx), 'tsconfig.json');

args.push(`--project=${tsConfig}`);
args.push(`--${tsConfigArgName}=${tsConfig}`);

args.push(options.script);

Expand Down
4 changes: 4 additions & 0 deletions packages/nx-prisma/src/executors/seed/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ export interface SeedExecutorSchema {
* TypeScript config to use for seeding
*/
tsConfig?: string;
/**
* Tool to use for executing the seeding script. Currently `ts-node` and `tsx` are supported
*/
executeWith?: 'ts-node' | 'tsx';
}
6 changes: 6 additions & 0 deletions packages/nx-prisma/src/executors/seed/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"tsConfig": {
"type": "string",
"description": "TypeScript config to use for seeding"
},
"executeWith": {
"type": "string",
"enum": ["ts-node", "tsx"],
"description": "Tool to use for executing the seeding script. Currently `ts-node` and `tsx` are supported",
"default": "ts-node"
}
},
"required": ["script"]
Expand Down

0 comments on commit 36e55c4

Please sign in to comment.