Преглед изворни кода

feat(cli): Include custom CRUD permissions with plugin scaffold

Michael Bromley пре 2 година
родитељ
комит
0c62b6f927

+ 1 - 3
packages/cli/src/commands/new/plugin/new-plugin.ts

@@ -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';
 
@@ -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) {
@@ -177,7 +176,6 @@ export async function generatePlugin(options: GeneratePluginOptions) {
         fs.writeFileSync(filePath, rendered);
     });
 
-    const { outro } = await import('@clack/prompts');
     outro('✅ Plugin scaffolding complete!');
 }
 

+ 3 - 2
packages/cli/src/commands/new/plugin/scaffold/api/admin.resolver.ts

@@ -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}';
 
@@ -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 },

+ 13 - 1
packages/cli/src/commands/new/plugin/scaffold/constants.ts

@@ -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 - 0
packages/cli/src/commands/new/plugin/types.ts

@@ -2,6 +2,7 @@ export interface GeneratePluginOptions {
     name: string;
     withCustomEntity: boolean;
     withApiExtensions: boolean;
+    withAdminUi: boolean;
     customEntityName: string;
 }
 

+ 1 - 1
packages/cli/tsconfig.json

@@ -1,7 +1,7 @@
 {
   "extends": "../../tsconfig.json",
   "compilerOptions": {
-    "moduleResolution": "NodeNext",
+    "moduleResolution": "node",
     "declaration": true,
     "removeComments": true,
     "strictPropertyInitialization": false,