Browse Source

fix(cli): Fix translatable entity imports

Michael Bromley 1 year ago
parent
commit
e6c9ba8e8d

+ 17 - 10
packages/cli/src/commands/add/entity/add-entity.ts

@@ -117,16 +117,10 @@ function createEntity(plugin: VendurePluginRef, options: AddEntityOptions) {
         path.join(entitiesDir, `${options.translationFileName}.ts`),
     );
 
-    const entityClass = entityFile.getClass('ScaffoldEntity')?.rename(options.className);
-    const customFieldsClass = entityFile
-        .getClass('ScaffoldEntityCustomFields')
-        ?.rename(`${options.className}CustomFields`);
-    const translationClass = translationFile
-        .getClass('ScaffoldTranslation')
-        ?.rename(`${options.className}Translation`);
-    const translationCustomFieldsClass = translationFile
-        .getClass('ScaffoldEntityCustomFieldsTranslation')
-        ?.rename(`${options.className}CustomFieldsTranslation`);
+    const entityClass = entityFile.getClass('ScaffoldEntity');
+    const customFieldsClass = entityFile.getClass('ScaffoldEntityCustomFields');
+    const translationClass = translationFile.getClass('ScaffoldTranslation');
+    const translationCustomFieldsClass = translationFile.getClass('ScaffoldEntityCustomFieldsTranslation');
 
     if (!options.features.customFields) {
         // Remove custom fields from entity
@@ -142,8 +136,21 @@ function createEntity(plugin: VendurePluginRef, options: AddEntityOptions) {
         entityClass?.getProperty('translations')?.remove();
         removeImplementsFromClass('Translatable', entityClass);
         translationFile.delete();
+    } else {
+        entityFile
+            .getImportDeclaration('./entity-translation.template')
+            ?.setModuleSpecifier(`./${options.translationFileName}`);
+        translationFile
+            .getImportDeclaration('./entity.template')
+            ?.setModuleSpecifier(`./${options.fileName}`);
     }
 
+    // Rename the entity classes
+    entityClass?.rename(options.className);
+    customFieldsClass?.rename(`${options.className}CustomFields`);
+    translationClass?.rename(`${options.className}Translation`);
+    translationCustomFieldsClass?.rename(`${options.className}CustomFieldsTranslation`);
+
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     return { entityClass: entityClass!, translationClass: translationClass! };
 }

+ 9 - 6
packages/cli/src/commands/add/service/add-service.ts

@@ -174,12 +174,15 @@ async function addService(
             moduleSpecifier: '@nestjs/common',
             namedImports: ['Inject'],
         });
-        serviceClassDeclaration.getConstructors()[0]?.addParameter({
-            scope: Scope.Private,
-            name: 'options',
-            type: pluginOptions.typeDeclaration.getName(),
-            decorators: [{ name: 'Inject', arguments: [pluginOptions.constantDeclaration.getName()] }],
-        });
+        serviceClassDeclaration
+            .getConstructors()[0]
+            ?.addParameter({
+                scope: Scope.Private,
+                name: 'options',
+                type: pluginOptions.typeDeclaration.getName(),
+                decorators: [{ name: 'Inject', arguments: [pluginOptions.constantDeclaration.getName()] }],
+            })
+            .formatText();
     }
     modifiedSourceFiles.push(serviceSourceFile);