Skip to content

Commit

Permalink
fix(cli): Fix translatable entity imports
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 2, 2024
1 parent 9379d73 commit e6c9ba8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
27 changes: 17 additions & 10 deletions packages/cli/src/commands/add/entity/add-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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! };
}
Expand Down
15 changes: 9 additions & 6 deletions packages/cli/src/commands/add/service/add-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e6c9ba8

Please sign in to comment.