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

[FEATURE] : extends the static auditor to add Bundle Analyze #127

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ const startStaticAudit = async ({ applicationId, workspaceFolder }: AuditCommons

await forkWorker('./src/workers/CodeQualityAnalysisWorker.ts', workerConfig);
AppLogger.info(
'[StaticAuditorManager - startStaticAudit] CodeQuality Audit have completed successfully.',
'[StaticAuditorManager - startStaticAudit] CodeQuality Audits have completed successfully.',
);

await forkWorker('./src/workers/DependenciesAnalysisWorker.ts', workerConfig);
AppLogger.info(
'[StaticAuditorManager - startStaticAudit] Dependencies Audit have completed successfully.',
'[StaticAuditorManager - startStaticAudit] Dependencies Audits have completed successfully.',
);

await forkWorker('./src/workers/BundleAnalyzeWorker.ts', workerConfig);
AppLogger.info(
'[StaticAuditorManager - startStaticAudit] Bundle Analyze Audits have completed successfully.',
);

return true; // Indicates successful initiation of audits
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { AppLogger } from '@v6y/core-logic';

import { AuditCommonsType } from '../types/AuditCommonsType.ts';
import BundleAnalyzeUtils from './BundleAnalyzeUtils.ts';

/**
* Start the auditor analysis
* @param applicationId
* @param workspaceFolder
*/

const { formatBundleAnalyzeReports } = BundleAnalyzeUtils;

const startAuditorAnalysis = async ({ applicationId, workspaceFolder }: AuditCommonsType) => {
try {
AppLogger.info(
`[BundleAnalyzeAuditor - startAuditorAnalysis] applicationId: ${applicationId}`,
);
AppLogger.info(
`[BundleAnalyzeAuditor - startAuditorAnalysis] workspaceFolder: ${workspaceFolder}`,
);

const auditReports = formatBundleAnalyzeReports({ workspaceFolder, applicationId });
AppLogger.info(
`[BundleAnalyzeAuditor - startAuditorAnalysis] auditReports: ${auditReports}`,
);
} catch (error) {
AppLogger.error(
'[BundleAnalyzeAuditor - startAuditorAnalysis] An exception occurred during the audits:',
error,
);
return false;
}
};

const BundleAnalyzeAuditor = {
startAuditorAnalysis,
};

export default BundleAnalyzeAuditor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { AppLogger, AuditType } from '@v6y/core-logic';
import { exec } from 'child_process';
import { readdir } from 'fs/promises';

import { AuditCommonsType } from '../types/AuditCommonsType.ts';

/**
* Format bundle analyze reports.
* @param applicationId
* @param workspaceFolder
*/

const formatBundleAnalyzeReports = async ({
applicationId,
workspaceFolder,
}: AuditCommonsType): Promise<AuditType[] | null> => {
try {
AppLogger.info(
`[BundleAnalyzeUtils - formatBundleAnalyzeReports] applicationId: ${applicationId}`,
);
AppLogger.info(
`[BundleAnalyzeUtils - formatBundleAnalyzeReports] workspaceFolder: ${workspaceFolder}`,
);

if (!applicationId || !workspaceFolder) {
return [];
}

if (!workspaceFolder) {
throw new Error('workspaceFolder is undefined');
}
const files = await readdir(workspaceFolder);
AppLogger.info(
`[BundleAnalyzeUtils - formatBundleAnalyzeReports] the content of ${workspaceFolder}:`,
files,
);

const packageManagers = {
npm: 'package-lock.json',
pnpm: 'pnpm-lock.yaml',
yarn: 'yarn.lock',
};

function findPackageManager(files: string[], packageManagers: { [key: string]: string }) {
for (const packageManager in packageManagers) {
const lockFile = packageManagers[packageManager];
if (Object.values(files).includes(lockFile)) {
return packageManager;
}
}
return null;
}

const packageManager = findPackageManager(files, packageManagers);

AppLogger.info(
`[BundleAnalyzeUtils - formatBundleAnalyzeReports] packageManager: ${packageManager}`,
);

exec(`${packageManager} install`, (error, stdout, stderr) => {
if (error) {
AppLogger.error(
`[BundleAnalyzeUtils - formatBundleAnalyzeReports] Error while executing program : ${error.message}`,
);
return;
}

if (stderr) {
AppLogger.error(
`[BundleAnalyzeUtils - formatBundleAnalyzeReports] Error: ${stderr}`,
);
return;
}

AppLogger.info(`[BundleAnalyzeUtils - formatBundleAnalyzeReports] Output: ${stdout}`);
});

const auditReports: AuditType[] = [];
return auditReports;
} catch (error) {
AppLogger.info(`[BundleAnalyzeUtils - formatBundleAnalyzeReports] error: ${error}`);
return null;
}
};

const BundleAnalyzeUtils = {
formatBundleAnalyzeReports,
};

export default BundleAnalyzeUtils;
29 changes: 29 additions & 0 deletions v6y-apps/bfb-static-auditor/src/workers/BundleAnalyzeWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AppLogger, DataBaseManager, PerformancesUtils } from '@v6y/core-logic';
import { parentPort, workerData } from 'worker_threads';

import BundleAnalyzeAuditor from '../auditors/bundle-analyzer/BundleAnalyzeAuditor.ts';

AppLogger.info('******************** Starting background Audit **************************');

try {
const { applicationId, workspaceFolder } = workerData || {};
AppLogger.info(`[BundleAnalyzeWorker] applicationId: ${applicationId}`);
AppLogger.info(`[BundleAnalyzeWorker] workspaceFolder: ${workspaceFolder}`);

// *********************************************** Database Configuration and Connection ***********************************************
await DataBaseManager.connect();

// *********************************************** Audit Configuration and Launch ***********************************************
PerformancesUtils.startMeasure('BundleAnalyzeWorker-startAuditorAnalysis');
await BundleAnalyzeAuditor.startAuditorAnalysis({
applicationId,
workspaceFolder,
});
PerformancesUtils.endMeasure('BundleAnalyzeWorker-startAuditorAnalysis');

AppLogger.info('******************** Audit completed successfully ********************');
parentPort?.postMessage('Audit have completed.');
} catch (error) {
AppLogger.error('[BundleAnalyzeWorker] An exception occurred during the audits:', error);
parentPort?.postMessage('Audit encountered an error.'); // Notify the parent of the error
}