Skip to content

Commit

Permalink
fix: resolve some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-rodriguez committed Mar 6, 2024
1 parent d508f79 commit 52f047a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ samples/todo


.nx/cache
sonar-project.properties
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const nxPreset = require('@nx/jest/preset').default;

module.exports = { ...nxPreset };
module.exports = { ...nxPreset, coverageReporters: [...nxPreset.coverageReporters, 'lcov', 'cobertura'] };
20 changes: 10 additions & 10 deletions packages/mailer/src/lib/mailer.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Inject, Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { Transporter, SentMessageInfo, SendMailOptions } from 'nodemailer';
import { MAILER_OPTIONS_PROVIDER_NAME, MAILER_TRANSPORT_PROVIDER_NAME } from './mailer.constants';
import { MailerModuleOptions, IHandlebarsOptions } from './mailer.types';
import * as fs from 'fs-extra';
import { SendMailOptions, SentMessageInfo, Transporter } from 'nodemailer';
import { join } from 'path';
import { MAILER_OPTIONS_PROVIDER_NAME, MAILER_TRANSPORT_PROVIDER_NAME } from './mailer.constants';
import { IHandlebarsOptions, MailerModuleOptions } from './mailer.types';

@Injectable()
export class MailerService implements OnModuleInit, OnModuleDestroy {
Expand Down Expand Up @@ -31,7 +31,7 @@ export class MailerService implements OnModuleInit, OnModuleDestroy {

Object.assign(this.hbsOptions, this.options.hbsOptions);

if (this.hbsOptions.extension![0] !== '.') {
if (!this.hbsOptions.extension!.startsWith('.')) {
this.hbsOptions.extension = '.' + this.hbsOptions.extension;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ export class MailerService implements OnModuleInit, OnModuleDestroy {
}

if (thirdParam && typeof firstParam === 'string') {
mailOptions.html = this.templates[thirdParam!](fourthParam, fifthParam);
mailOptions.html = this.templates[thirdParam](fourthParam, fifthParam);
} else {
mailOptions.html = this.templates[secondParam!](thirdParam, fourthParam);
}
Expand All @@ -128,7 +128,7 @@ export class MailerService implements OnModuleInit, OnModuleDestroy {
}

private addHelpers(): void {
if (this.hbsOptions.helpers && this.hbsOptions.helpers.length) {
if (this.hbsOptions.helpers?.length) {
this.hbsOptions.helpers.forEach(helper => {
this.hbs.registerHelper(helper.name, helper.func);
});
Expand All @@ -137,11 +137,11 @@ export class MailerService implements OnModuleInit, OnModuleDestroy {

private addTemplates(): void {
if (fs.existsSync(this.hbsOptions.templatesDir)) {
const templates = fs.readdirSync(this.hbsOptions!.templatesDir, {
const templates = fs.readdirSync(this.hbsOptions.templatesDir, {
withFileTypes: true,
});
templates
.filter(value => value.name.endsWith(this.hbsOptions!.extension!) && value.isFile())
.filter(value => value.name.endsWith(this.hbsOptions.extension!) && value.isFile())
.forEach(element => {
this.templates[element.name.substring(0, element.name.indexOf('.'))] = this.hbs.compile(
fs.readFileSync(join(this.hbsOptions.templatesDir, element.name)).toString(),
Expand All @@ -152,11 +152,11 @@ export class MailerService implements OnModuleInit, OnModuleDestroy {

private addPartials(): void {
if (this.hbsOptions.partialsDir && fs.existsSync(this.hbsOptions.partialsDir)) {
const partials = fs.readdirSync(this.hbsOptions!.partialsDir, {
const partials = fs.readdirSync(this.hbsOptions.partialsDir, {
withFileTypes: true,
});
partials
.filter(value => value.name.endsWith(this.hbsOptions!.extension!) && value.isFile())
.filter(value => value.name.endsWith(this.hbsOptions.extension!) && value.isFile())
.forEach(element => {
this.hbs.registerPartial(
element.name.substring(0, element.name.indexOf('.')),
Expand Down
12 changes: 6 additions & 6 deletions packages/mailer/src/test/mailer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('MailerService', () => {
const expected = { ...input, from: '[email protected]' };

service.sendPlainMail(input);
expect(transporter.sendMail).toBeCalledWith(expected);
expect(transporter.sendMail).toHaveBeenCalledWith(expected);
});

it('should send a plain text email using the provided transporter providing all params', () => {
Expand All @@ -68,7 +68,7 @@ describe('MailerService', () => {
const expected = { ...input, from: '[email protected]' };

service.sendPlainMail(input.to, input.subject, input.html);
expect(transporter.sendMail).toBeCalledWith(expected);
expect(transporter.sendMail).toHaveBeenCalledWith(expected);
});

it('should send email from a handlebars template by using the provided transporter providing emailOptions object', () => {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('MailerService', () => {
};

service.sendTemplateMail(input, 'test', { title: 'my title' });
expect(transporter.sendMail).toBeCalledWith(expected);
expect(transporter.sendMail).toHaveBeenCalledWith(expected);
});

it('should register a template and should be ready to use', () => {
Expand All @@ -116,7 +116,7 @@ describe('MailerService', () => {

service.addTemplate('my-view', `My view`);
service.sendTemplateMail(input, 'my-view', {});
expect(transporter.sendMail).toBeCalledWith(expected);
expect(transporter.sendMail).toHaveBeenCalledWith(expected);
});

it('should register a partial and should be ready to use', () => {
Expand All @@ -134,7 +134,7 @@ describe('MailerService', () => {
service.addTemplate('my-view', `{{#> my-partial }}My view{{/my-partial}}`);
service.registerPartial('my-partial', `this is my partial: {{> @partial-block }}`);
service.sendTemplateMail(input, 'my-view', {});
expect(transporter.sendMail).toBeCalledWith(expected);
expect(transporter.sendMail).toHaveBeenCalledWith(expected);
});

it('should register a helper and should be ready to use', () => {
Expand All @@ -154,6 +154,6 @@ describe('MailerService', () => {
});
service.addTemplate('my-view', `{{#bold}}My view{{/bold}}`);
service.sendTemplateMail(input, 'my-view', {});
expect(transporter.sendMail).toBeCalledWith(expected);
expect(transporter.sendMail).toHaveBeenCalledWith(expected);
});
});
18 changes: 0 additions & 18 deletions packages/nx-nest/src/generators/auth-jwt/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ describe('auth-jwt generator', () => {
it('should add JWT configuration if convict is present', async () => {
if (tree.exists(`./packages/nx-nest/apps/${options.name}/src/config.ts`)) {
const fileContent = tree.read(`./packages/nx-nest/apps/${options.name}/src/config.ts`)?.toString();
// expect(fileContent).toContain({
// jwt: {
// secret: {
// doc: 'JWT secret',
// format: String,
// default: 'SECRET',
// env: 'JWT_SECRET',
// arg: 'jwtSecret',
// secret: true,
// },
// expiration: {
// doc: 'Token expiration time',
// default: '24h',
// format: String,
// env: 'JWT_EXPIRATION',
// },
// },
// });
expect(fileContent).toContain('jwt: {');
expect(fileContent).toContain('secret: {');
expect(fileContent).toContain('expiration: {');
Expand Down
58 changes: 24 additions & 34 deletions packages/nx-nest/src/utils/ast-file-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AsExpression,
ClassDeclaration,
IndentationText,
ObjectLiteralElementLike,
ObjectLiteralExpression,
Project,
PropertyAssignment,
Expand Down Expand Up @@ -369,6 +370,25 @@ export class ASTFileBuilder {
return this;
}

private updatePropertyAssignment(property: ObjectLiteralElementLike, propertyInitializer: string | string[]): void {
const initializer = (property as PropertyAssignment).getInitializer();
if (initializer?.getKind() === SyntaxKind.ArrayLiteralExpression) {
if (Array.isArray(propertyInitializer)) {
propertyInitializer.forEach(elem => {
(initializer as ArrayLiteralExpression).addElement(elem);
});
} else {
(initializer as ArrayLiteralExpression).addElement(propertyInitializer);
}
} else {
property.set({
initializer: Array.isArray(propertyInitializer)
? JSON.stringify(propertyInitializer).replace(/(['"])/g, '')
: propertyInitializer,
});
}
}

addPropertyToObjectLiteralParam(
varName: string,
paramIndex: number,
Expand All @@ -388,27 +408,12 @@ export class ASTFileBuilder {
if (arg && arg.getKind() === SyntaxKind.ObjectLiteralExpression) {
const property = arg.getProperty(propertyName);
if (property) {
const initializer = (property as PropertyAssignment).getInitializer();
if (initializer?.getKind() === SyntaxKind.ArrayLiteralExpression) {
if (Array.isArray(propertyInitializer)) {
propertyInitializer.forEach(elem => {
(initializer as ArrayLiteralExpression).addElement(elem);
});
} else {
(initializer as ArrayLiteralExpression).addElement(propertyInitializer);
}
} else {
property.set({
initializer: Array.isArray(propertyInitializer)
? JSON.stringify(propertyInitializer).replace(/('|")/g, '')
: propertyInitializer,
});
}
this.updatePropertyAssignment(property, propertyInitializer);
} else {
arg.addPropertyAssignment({
name: propertyName,
initializer: Array.isArray(propertyInitializer)
? JSON.stringify(propertyInitializer).replace(/('|")/g, '')
? JSON.stringify(propertyInitializer).replace(/(['"])/g, '')
: propertyInitializer,
});
}
Expand Down Expand Up @@ -440,27 +445,12 @@ export class ASTFileBuilder {
if (arg && arg.getKind() === SyntaxKind.ObjectLiteralExpression) {
const property = (arg as ObjectLiteralExpression).getProperty(propertyName);
if (property) {
const initializer = (property as PropertyAssignment).getInitializer();
if (initializer?.getKind() === SyntaxKind.ArrayLiteralExpression) {
if (Array.isArray(propertyInitializer)) {
propertyInitializer.forEach(elem => {
(initializer as ArrayLiteralExpression).addElement(elem);
});
} else {
(initializer as ArrayLiteralExpression).addElement(propertyInitializer);
}
} else {
property.set({
initializer: Array.isArray(propertyInitializer)
? JSON.stringify(propertyInitializer).replace(/('|")/g, '')
: propertyInitializer,
});
}
this.updatePropertyAssignment(property, propertyInitializer);
} else {
(arg as ObjectLiteralExpression).addPropertyAssignment({
name: propertyName,
initializer: Array.isArray(propertyInitializer)
? JSON.stringify(propertyInitializer).replace(/('|")/g, '')
? JSON.stringify(propertyInitializer).replace(/(['"])/g, '')
: propertyInitializer,
});
}
Expand Down

0 comments on commit 52f047a

Please sign in to comment.