Skip to content

Commit

Permalink
feat(cli): Include custom CRUD permissions with plugin scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Sep 12, 2023
1 parent a6df4c1 commit 0c62b6f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
4 changes: 1 addition & 3 deletions packages/cli/src/commands/new/plugin/new-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cancel, confirm, intro, isCancel, multiselect, outro, text } from '@clack/prompts';
import { camelCase, constantCase, paramCase, pascalCase } from 'change-case';
import { Command } from 'commander';
import * as fs from 'fs-extra';
import path from 'path';

Expand All @@ -16,7 +16,6 @@ import { GeneratePluginOptions, TemplateContext } from './types';
const cancelledMessage = 'Plugin setup cancelled.';

export async function newPlugin() {
const { cancel, confirm, intro, isCancel, multiselect, text } = await import('@clack/prompts');
const options: GeneratePluginOptions = { name: '', customEntityName: '' } as any;
intro('Scaffolding a new Vendure plugin!');
if (!options.name) {
Expand Down Expand Up @@ -177,7 +176,6 @@ export async function generatePlugin(options: GeneratePluginOptions) {
fs.writeFileSync(filePath, rendered);
});

const { outro } = await import('@clack/prompts');
outro('✅ Plugin scaffolding complete!');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function renderAdminResolverWithEntity(context: TemplateContext) {
import { Args, Resolver, Mutation } from '@nestjs/graphql';
import { Allow, Ctx, RequestContext, Transaction, Permission } from '@vendure/core';
import { ${context.entity.instanceName}Permission } from '../constants';
import { ${context.service.className} } from '../services/${context.service.fileName}';
import { ${context.entity.className} } from '../entities/${context.entity.fileName}';
Expand All @@ -19,14 +20,14 @@ export class AdminResolver {
@Transaction()
@Mutation()
@Allow(Permission.SuperAdmin)
@Allow(${context.entity.instanceName}Permission.Create)
create${context.entity.className}(@Ctx() ctx: RequestContext, @Args() args: { input: Create${context.customEntityName}Input }): Promise<${context.entity.className}> {
return this.${context.service.instanceName}.create(ctx, args.input);
}
@Transaction()
@Mutation()
@Allow(Permission.SuperAdmin)
@Allow(${context.entity.instanceName}Permission.Update)
update${context.entity.className}(
@Ctx() ctx: RequestContext,
@Args() args: { input: Update${context.customEntityName}Input },
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/commands/new/plugin/scaffold/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ import { constantCase, pascalCase } from 'change-case';

import { TemplateContext } from '../types';

export function renderConstants({ pluginName, pluginInitOptionsName }: TemplateContext): string {
export function renderConstants(context: TemplateContext): string {
const { pluginName, pluginInitOptionsName } = context;
const optionalImports: string[] = [];
const optionalStatements: string[] = [];
if (context.withCustomEntity) {
optionalImports.push(`import { CrudPermissionDefinition } from '@vendure/core';`);
optionalStatements.push(
`export const ${context.entity.instanceName}Permission = new CrudPermissionDefinition('${context.entity.className}');`,
);
}
return /* language=TypeScript */ `
${optionalImports.join('\n')}
export const ${pluginInitOptionsName} = Symbol('${pluginInitOptionsName}');
export const loggerCtx = '${pluginName}';
${optionalStatements.join('\n')}
`;
}
1 change: 1 addition & 0 deletions packages/cli/src/commands/new/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface GeneratePluginOptions {
name: string;
withCustomEntity: boolean;
withApiExtensions: boolean;
withAdminUi: boolean;
customEntityName: string;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"moduleResolution": "NodeNext",
"moduleResolution": "node",
"declaration": true,
"removeComments": true,
"strictPropertyInitialization": false,
Expand Down

0 comments on commit 0c62b6f

Please sign in to comment.