Skip to content

@W-18480437 @W-18480438 lwc and apex reports framework #278

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

Open
wants to merge 1 commit into
base: prerelease/alpha
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
13 changes: 13 additions & 0 deletions src/commands/omnistudio/migration/assess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { DebugTimer, DataRaptorAssessmentInfo, FlexCardAssessmentInfo } from '..

import { Logger } from '../../../utils/logger';
import OmnistudioRelatedObjectMigrationFacade from '../../../migration/related/OmnistudioRelatedObjectMigrationFacade';
import { OmnistudioOrgDetails, OrgUtils } from '../../../utils/orgUtils';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-omnistudio-migration-tool', 'assess');
Expand Down Expand Up @@ -47,6 +48,18 @@ export default class Assess extends OmniStudioBaseCommand {
const apiVersion = (this.flags.apiversion || '55.0') as string;
const allVersions = (this.flags.allversions || false) as boolean;
const conn = this.org.getConnection();
const orgs: OmnistudioOrgDetails = await OrgUtils.getOrgDetails(conn, namespace);

if (orgs.packageDetails.length === 0) {
this.ux.log('No package installed on given org.');
return;
}

if (orgs.omniStudioOrgPermissionEnabled) {
this.ux.log('The org is already on standard data model.');
return;
}

Logger.initialiseLogger(this.ux, this.logger);
const projectDirectory = OmnistudioRelatedObjectMigrationFacade.intializeProject();
conn.setApiVersion(apiVersion);
Expand Down
10 changes: 10 additions & 0 deletions src/commands/omnistudio/migration/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { OmniScriptExportType, OmniScriptMigrationTool } from '../../../migratio
import { Logger } from '../../../utils/logger';
import OmnistudioRelatedObjectMigrationFacade from '../../../migration/related/OmnistudioRelatedObjectMigrationFacade';
import { generatePackageXml } from '../../../utils/generatePackageXml';
import { OmnistudioOrgDetails, OrgUtils } from '../../../utils/orgUtils';

// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
Expand Down Expand Up @@ -71,6 +72,15 @@ export default class Migrate extends OmniStudioBaseCommand {
const conn = this.org.getConnection();
conn.setApiVersion(apiVersion);

const orgs: OmnistudioOrgDetails = await OrgUtils.getOrgDetails(conn, namespace);
if (orgs.packageDetails.length === 0) {
this.ux.log('No package installed on given org.');
return;
}
if (orgs.omniStudioOrgPermissionEnabled) {
this.ux.log('The org is already on standard data model.');
return;
}
// Let's time every step
DebugTimer.getInstance().start();
let projectPath: string;
Expand Down
6 changes: 3 additions & 3 deletions src/migration/related/ApexMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ApexMigration extends BaseRelatedObjectMigration {
if (file.ext !== '.cls') continue;
try {
const apexAssementInfo = this.processApexFile(file);
if (apexAssementInfo && apexAssementInfo.diff.length === 0) continue;
if (apexAssementInfo && apexAssementInfo.diff.length < 3) continue;
fileAssessmentInfo.push(apexAssementInfo);
} catch (err) {
Logger.logger.error(`Error processing ${file.name}`);
Expand Down Expand Up @@ -116,7 +116,7 @@ export class ApexMigration extends BaseRelatedObjectMigration {
updateMessages.push('File has been updated to allow calls to Omnistudio components');
tokenUpdates.push(...tokeUpdatesForMethodCalls);
}
let difference = '';
let difference = [];
if (tokenUpdates && tokenUpdates.length > 0) {
const updatedContent = parser.rewrite(tokenUpdates);
fs.writeFileSync(file.location, parser.rewrite(tokenUpdates));
Expand All @@ -134,7 +134,7 @@ export class ApexMigration extends BaseRelatedObjectMigration {
warnings: warningMessage,
infos: updateMessages,
path: file.location,
diff: difference,
diff: JSON.stringify(difference),
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/migration/related/LwcMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class LwcMigration extends BaseRelatedObjectMigration {
const path = file.location;
const name = file.name + file.ext;
const diff = processor.process(file, type, this.namespace);
if (diff !== undefined && diff !== '') {
if (diff !== undefined && diff !== '[]') {
const fileInfo: FileChangeInfo = {
path,
name,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/json/jsonutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
export class jsonutil {
// Recursive method to find a property in the JSON
public static findProperty(obj: any, propertyName: string): any {
public static findProperty(obj: Record<string, unknown>, propertyName: string): unknown {
if (obj === null || typeof obj !== 'object') {
return null;
}
Expand All @@ -23,7 +23,7 @@ export class jsonutil {
// Otherwise, search through all keys
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const result = this.findProperty(obj[key], propertyName);
const result = this.findProperty(obj[key] as Record<string, unknown>, propertyName);
if (result !== null) {
return result;
}
Expand Down
32 changes: 27 additions & 5 deletions src/utils/lwcparser/fileutils/FileDiffUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import * as fs from 'fs';
import { createPatch } from 'diff';
import { Logger } from '../../../utils/logger';

export class FileDiffUtil {
public getFileDiff(filename: string, originalFileContent: string, modifiedFileContent: string): string {
public getFileDiff(
filename: string,
originalFileContent: string,
modifiedFileContent: string
): Array<[string | null, string | null]> {
const patch: string = createPatch('', originalFileContent, modifiedFileContent);
try {
// Split the patch into lines
Expand All @@ -17,8 +22,8 @@ export class FileDiffUtil {
let newLineNumber = 1;
let firstPlusAlreadySkipped = false;
let firstMinusAlreadySkipped = false;
const diff: Array<[string | null, string | null]> = [];
// Initialize result as HTML string
let result = '';

patchLines.forEach((line) => {
// Parse the hunk header (e.g., @@ -2,3 +2,3 @@)
Expand All @@ -36,7 +41,7 @@ export class FileDiffUtil {
oldLineNumber++;
return;
}
result += `<div style="color: red;">- Line ${oldLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`;
diff.push([line.slice(1), null]);
oldLineNumber++;
} else if (line.startsWith('+')) {
// Skip the first line difference
Expand All @@ -45,16 +50,22 @@ export class FileDiffUtil {
newLineNumber++;
return;
}
result += `<div style="color: green;">+ Line ${newLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`;
diff.push([null, line.slice(1)]);
newLineNumber++;
} else if (line.startsWith(' ')) {
diff.push([line.slice(1), line.slice(1)]);
// Unchanged line, skip it
oldLineNumber++;
newLineNumber++;
}
});
const diffJson = {
fileName: filename,
diff,
};
this.appendToJsonFile('new_assessment_reports/lwc_reports/assess.json', diffJson);
// Return the result string, or an empty string if no differences
return result.trim() ? result : '';
return diff;
} catch (error) {
Logger.logger.error('Error in FileDiffUtil', error.message);
}
Expand All @@ -63,4 +74,15 @@ export class FileDiffUtil {
escapeHtml(text: string): string {
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;');
}

private appendToJsonFile(filePath: string, newData: Record<string, unknown>): void {
try {
const fileData = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '[]';
const jsonData = JSON.parse(fileData);
jsonData.push(newData);
fs.writeFileSync(filePath, JSON.stringify(jsonData, null, 2), 'utf8');
} catch (error) {
Logger.logger.error('Error appending to JSON file', error.message);
}
}
}
4 changes: 2 additions & 2 deletions src/utils/lwcparser/fileutils/HtmlFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export class HtmlFileProcessor implements FileProcessor {
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
if (type != null && type === 'migration' && diff !== '') {
if (type != null && type === 'migration' && diff.length > 0) {
fileutil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
}
return diff;
return JSON.stringify(diff);
}
}
}
2 changes: 1 addition & 1 deletion src/utils/lwcparser/fileutils/JavascriptFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class JavascriptFileProcessor implements FileProcessor {
if (type != null && type === 'migration') {
fileutil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
}
return diff;
return JSON.stringify(diff);
}
}
}
2 changes: 1 addition & 1 deletion src/utils/lwcparser/fileutils/XmlFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class XmlFileProcessor implements FileProcessor {
if (type != null && type === 'migration') {
fileutil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
}
return diff;
return JSON.stringify(diff);
}
}
}
Loading