Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): fix sonar issues #2127

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 69 additions & 56 deletions packages/cli/src/generators/cdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from 'node-fetch';
import {existsSync, mkdirSync} from 'node:fs';
import { existsSync, mkdirSync } from 'node:fs';
import {
appendFile,
readFile,
Expand All @@ -15,9 +15,9 @@ import {
Project,
PropertyAssignmentStructure,
} from 'ts-morph';
import {BaseGenerator} from '../../base-generator';
import {IacList} from '../../enum';
import {CdkOptions} from '../../types';
import { BaseGenerator } from '../../base-generator';
import { IacList } from '../../enum';
import { CdkOptions } from '../../types';
yeshamavani marked this conversation as resolved.
Show resolved Hide resolved
const chalk = require('chalk'); //NOSONAR

/**
Expand Down Expand Up @@ -131,7 +131,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
return filesToKeep.some(reqPath => filePath.startsWith(reqPath));
};

const {owner, repo, tag, templateDir: dir} = this.remoteConfig;
const { owner, repo, tag, templateDir: dir } = this.remoteConfig;
/**
* When the tar file is downloaded and extracted it creates a dir structure like
* ${repo}-${tag}
Expand All @@ -146,7 +146,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {

try {
if (!existsSync(outputDir)) {
mkdirSync(outputDir, {recursive: true});
mkdirSync(outputDir, { recursive: true });
}

const response = await fetch(url);
Expand Down Expand Up @@ -195,51 +195,61 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {

async setupFiles() {
this.log('🛠️ Configuring files to meet your requirements...');
if (this.options.applicationClassName && this.options.relativePathToApp) {
let appImport = '';

let appImport = '';
const isDefaultExport = await this._isDefaultExport(
this.options.applicationClassName,
this.options.relativePathToApp,
);

const isDefaultExport = await this._isDefaultExport(
this.options.applicationClassName!,
this.options.relativePathToApp!,
);
const appImportPath = this._getAppImportPath(
this.options.relativePathToApp,
);

Comment on lines +198 to 209
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using an if else block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type of this.options.applicationClassName is string | undefined, but _isDefaultExport require arguement to be string only

const appImportPath = this._getAppImportPath(
this.options.relativePathToApp!,
);
if (!isDefaultExport) {
appImport = `import {${this.options.applicationClassName}} from '${appImportPath}'`;
} else {
appImport = `import ${this.options.applicationClassName} from '${appImportPath}`;
}

if (!isDefaultExport) {
appImport = `import {${this.options.applicationClassName}} from '${appImportPath}'`;
await this._updateFile(
this.destinationPath(
`${this.options.dir}/${(this[this.options.iac!] as LambdaConfig).handlerFile
}`,
),
'{{app_class_name_placeholder}}',
this.options.applicationClassName,
);

await this._updateFile(
this.destinationPath(
`${this.options.dir}/${(this[this.options.iac!] as LambdaConfig).handlerFile
}`,
),
'{{app_import_placeholder}}',
appImport,
);
} else {
appImport = `import ${this.options.applicationClassName} from '${appImportPath}`;
// Handle the case where applicationClassName or relativePathToApp is undefined
this.log.error("Application class name or relative path to app is undefined.");
}

await this._updateFile(
this.destinationPath(
`${this.options.dir}/${
(this[this.options.iac!] as LambdaConfig).handlerFile
}`,
),
'{{app_class_name_placeholder}}',
this.options.applicationClassName!,
);

await this._updateFile(
this.destinationPath(
`${this.options.dir}/${
(this[this.options.iac!] as LambdaConfig).handlerFile
}`,
),
'{{app_import_placeholder}}',
appImport,
);
}

async updatePackageJsonName() {
await this._updateFile(
this.destinationPath(`${this.options.dir}/package.json`),
'{{package_json_name_placeholder}}',
this.options.packageJsonName!,
);
if (this.options.packageJsonName) {
await this._updateFile(
this.destinationPath(`${this.options.dir}/package.json`),
'{{package_json_name_placeholder}}',
this.options.packageJsonName,
);
} else {
// Handle the case where applicationClassName or relativePathToApp is undefined
this.log.error("packageJsonName is undefined.");
}

}

async configureEnvs() {
Expand All @@ -248,19 +258,17 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
const keysToCreate = await this._getEnvKeys(envFile);
await this._appendEmptyKeysToEnv(
this.destinationPath(
`${this.options.dir}/${
(this[this.options.iac!] as IacConfig).envSchemaFile
`${this.options.dir}/${(this[this.options.iac!] as IacConfig).envSchemaFile
}`,
),
keysToCreate,
);

// Create entries for env variables in stack
try {
const {project, sourcefile} = this._parseTsFile(
const { project, sourcefile } = this._parseTsFile(
this.destinationPath(
`${this.options.dir!}/${
(this[this.options.iac!] as IacConfig).mainStackFile
`${this.options.dir!}/${(this[this.options.iac!] as IacConfig).mainStackFile
}`,
),
);
Expand Down Expand Up @@ -292,8 +300,7 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
this.log.ok('Your files are ready for action! 🎉');
} catch (error) {
this.log.error(
`Failed to update env vars of lambda stack in ${this.options.dir}/${
(this[this.options.iac!] as IacConfig).mainStackFile
`Failed to update env vars of lambda stack in ${this.options.dir}/${(this[this.options.iac!] as IacConfig).mainStackFile
}.`,
error,
);
Expand Down Expand Up @@ -361,19 +368,25 @@ export default class CdkGenerator extends BaseGenerator<CdkOptions> {
cwd: this.destinationRoot(),
});

this.spawnCommandSync('npm', ['install'], {
cwd: this.destinationPath(this.options.dir!),
});
if (this.options.dir) {
this.spawnCommandSync('npm', ['install'], {
cwd: this.destinationPath(this.options.dir),
});
} else {
// Handle the case where applicationClassName or relativePathToApp is undefined
this.log.error("dir is undefined.");
}

}

async end() {
const {owner, repo, templateDir: dir} = this.remoteConfig;
const { owner, repo, templateDir: dir } = this.remoteConfig;
this.log(`
${chalk.green("🚀 Hooray! You're all set to launch your app.")}
Next steps:
1. Fill up the environment variables in your ${chalk.yellow(
this.options.dir,
)} directory.
this.options.dir,
)} directory.
2. Build your app.
3. Run ${chalk.blue(`cdktf deploy ${this.options.iac}`)} to deploy the iac.

Expand Down Expand Up @@ -486,7 +499,7 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
_parseTsFile(filePath: string) {
const project = new Project();
const sourcefile = project.addSourceFileAtPathIfExists(filePath);
return {project, sourcefile};
return { project, sourcefile };
}

/**
Expand Down Expand Up @@ -526,9 +539,9 @@ ${chalk.blue(`https://github.com/${owner}/${repo}/blob/main/${dir}/README.md`)}
encoding: BufferEncoding = 'utf-8',
) {
try {
let data = await readFile(filePath, {encoding});
let data = await readFile(filePath, { encoding });
data = data.replace(new RegExp(placeholder, 'g'), replaceWith);
await writeFile(filePath, data, {encoding});
await writeFile(filePath, data, { encoding });
} catch (error) {
if (error instanceof Error) {
this.log.error(error.message);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function addTenantId(
const encryptedTenantId = req.headers['tenant-id'];
const decryptedTenantId = CryptoJS.AES.decrypt(
encryptedTenantId as string,
secretKey as string,
secretKey,
).toString(CryptoJS.enc.Utf8);
reqResponse['tenant-id'] = decryptedTenantId;
}
Expand Down
28 changes: 16 additions & 12 deletions services/task-service/src/commands/create-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,22 @@ export class CreateTaskCommand implements ICommand {
return;
}
const promises = tasks.map(async dbTask => {
const result = await workflowCtrl.startWorkflow(
workflow.id!,
new ExecuteWorkflowDto({
input: {
taskId: dbTask.id,
},
}),
);
await userTaskService.updateList(dbTask.id!, result['id']);
await taskRepo.updateById(dbTask.id!, {
externalId: result['id'],
});
if (workflow.id) {
const result = await workflowCtrl.startWorkflow(
workflow.id!,
new ExecuteWorkflowDto({
input: {
taskId: dbTask.id,
},
}),
);
await userTaskService.updateList(dbTask.id!, result['id']);
await taskRepo.updateById(dbTask.id!, {
externalId: result['id'],
});
} else {
this.logger.debug(`No workflow found for key ${workflowKey}`);
}
});
await Promise.all(promises);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ export class EventProcessorService implements IEventProcessor {
});
if (!workflow) {
this.logger.debug(`No workflow found for key ${workflowKey}`);
} else if (!workflow.id) {
this.logger.debug(
`Workflow found for key ${workflowKey}, but it has no id`,
);
} else {
await workflowCtrl.startWorkflow(
workflow.id!,
workflow.id,
new ExecuteWorkflowDto({
input: payload,
}),
Expand Down
Loading