Skip to content

Commit dea3cdf

Browse files
author
Shailesh M. Pachbhai
committed
feat: lwc filtering logic
1 parent 56ad3b7 commit dea3cdf

File tree

4 files changed

+114
-42
lines changed

4 files changed

+114
-42
lines changed

src/commands/omnistudio/migration/assess.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ export default class Assess extends OmniStudioBaseCommand {
6767
this.ux.log(`Using Namespace: ${namespace}`);
6868

6969
const dataRaptorAssessmentInfos: DataRaptorAssessmentInfo[] = await drMigrator.assess();
70-
this.ux.log('dataRaptorAssessmentInfos');
71-
this.ux.log(dataRaptorAssessmentInfos.toString());
70+
if (dataRaptorAssessmentInfos) {
71+
this.ux.log('dataRaptorAssessmentInfos');
72+
this.ux.log(dataRaptorAssessmentInfos.toString());
73+
}
7274
const flexCardAssessmentInfos: FlexCardAssessmentInfo[] = await flexMigrator.assess();
7375
const omniAssessmentInfo = await osMigrator.assess(dataRaptorAssessmentInfos, flexCardAssessmentInfos);
7476

src/migration/related/LwcMigration.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */
12
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
23
import * as shell from 'shelljs';
34
import { Org } from '@salesforce/core';
@@ -27,7 +28,7 @@ export class LwcMigration extends BaseRelatedObjectMigration {
2728
const type = 'assessment';
2829
const pwd = shell.pwd();
2930
shell.cd(this.projectPath);
30-
sfProject.retrieve(LWCTYPE, this.org.getUsername());
31+
// sfProject.retrieve(LWCTYPE, this.org.getUsername());
3132
const filesMap = this.processLwcFiles(this.projectPath);
3233
shell.cd(pwd);
3334
return this.processFiles(filesMap, type);
@@ -50,7 +51,7 @@ export class LwcMigration extends BaseRelatedObjectMigration {
5051
dir += LWC_DIR_PATH;
5152
let filesMap: Map<string, File[]>;
5253
try {
53-
filesMap = fileutil.readAllFiles(dir);
54+
filesMap = fileutil.readAndProcessFiles(dir, 'OmniScript Auto-generated');
5455
} catch (error) {
5556
Logger.logger.error('Error in reading files', error);
5657
}
@@ -79,11 +80,8 @@ export class LwcMigration extends BaseRelatedObjectMigration {
7980
if (processor != null) {
8081
const path = file.location;
8182
const name = file.name + file.ext;
82-
if (file.ext === 'xml') {
83-
// if (fileutil.isAutogenratedFile(path)) { }
84-
}
8583
const diff = processor.process(file, type, this.namespace);
86-
if (diff !== null || diff !== '') {
84+
if (diff !== undefined && diff !== '') {
8785
const fileInfo: FileChangeInfo = {
8886
path,
8987
name,

src/utils/file/fileutil.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-return */
2+
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
13
/* eslint-disable @typescript-eslint/restrict-template-expressions */
24
/* eslint-disable no-console */
35
import * as fs from 'fs';
@@ -71,6 +73,64 @@ export class fileutil {
7173
throw error;
7274
}
7375
}
76+
77+
public static readAndProcessFiles(
78+
baseDir: string,
79+
searchString: string,
80+
fileMap: Map<string, File[]> = new Map<string, File[]>()
81+
): Map<string, File[]> {
82+
const subDirectories = fs.readdirSync(baseDir).filter((dir) => {
83+
const fullPath = path.join(baseDir, dir);
84+
return fs.statSync(fullPath).isDirectory();
85+
});
86+
87+
for (const subDirectory of subDirectories) {
88+
console.log(`Processing subdirectory: ${subDirectory}`);
89+
const subDirPath = path.join(baseDir, subDirectory);
90+
91+
// Check the XML file for the substring
92+
const xmlFilePath = path.join(subDirPath, `${subDirectory}.js-meta.xml`);
93+
if (fs.existsSync(xmlFilePath)) {
94+
if (this.doesSubstringExist(xmlFilePath, searchString)) {
95+
console.log(`Substring found in ${xmlFilePath}. Skipping all files in ${subDirectory}.`);
96+
continue; // Move to the next subdirectory
97+
}
98+
}
99+
100+
// Process all files if substring is not found
101+
const currentDirFiles: File[] = [];
102+
const files = fs
103+
.readdirSync(subDirPath)
104+
.filter((file) => file.endsWith('.html') || file.endsWith('.js') || file.endsWith('.xml'));
105+
files.forEach((file) => {
106+
const filePath = path.join(subDirPath, file);
107+
console.log(`Processing file: ${filePath}`);
108+
const name = path.parse(file).name;
109+
const ext = path.parse(file).ext;
110+
const filepath = path.resolve(subDirPath, file);
111+
currentDirFiles.push(new File(name, filepath, ext));
112+
fileMap.set(path.basename(subDirPath), currentDirFiles);
113+
});
114+
}
115+
return fileMap;
116+
}
117+
118+
/**
119+
* Check if a substring exists in an XML file
120+
*
121+
* @param filePath Path of the XML file
122+
* @param searchString Substring to search for
123+
* @returns true if substring exists, otherwise false
124+
*/
125+
private static doesSubstringExist = (filePath: string, searchString: string): boolean => {
126+
try {
127+
const fileContent = fs.readFileSync(filePath, 'utf-8');
128+
return fileContent.includes(searchString);
129+
} catch (error) {
130+
console.error(`Error reading file ${filePath}:`, error);
131+
return false;
132+
}
133+
};
74134
}
75135

76136
export class File {

src/utils/lwcparser/fileutils/FileDiffUtil.ts

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
1-
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
2-
/* eslint-disable no-console */
31
/* eslint-disable @typescript-eslint/no-unsafe-call */
2+
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
3+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
44
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
55
import { createPatch } from 'diff';
6+
import { Logger } from '../../../utils/logger';
67

78
export class FileDiffUtil {
89
public getFileDiff(filename: string, originalFileContent: string, modifiedFileContent: string): string {
9-
const patch: string = createPatch(filename, originalFileContent, modifiedFileContent);
10-
11-
// Split the patch into lines
12-
const patchLines = patch.split('\n');
10+
const patch: string = createPatch('', originalFileContent, modifiedFileContent);
11+
try {
12+
// Split the patch into lines
13+
const patchLines = patch.split('\n');
1314

14-
// Initialize variables to track line numbers
15-
let oldLineNumber = 1;
16-
let newLineNumber = 1;
15+
// Initialize variables to track line numbers
16+
let oldLineNumber = 1;
17+
let newLineNumber = 1;
1718

18-
// Initialize result as HTML string
19-
let result = '';
19+
// Initialize result as HTML string
20+
let result = '';
2021

21-
patchLines.forEach((line) => {
22-
// Parse the hunk header (e.g., @@ -2,3 +2,3 @@)
23-
const hunkHeader = /^@@ -(\d+),\d+ \+(\d+),\d+ @@/;
24-
const match = hunkHeader.exec(line);
22+
patchLines.forEach((line) => {
23+
// Parse the hunk header (e.g., @@ -2,3 +2,3 @@)
24+
const hunkHeader = /^@@ -(\d+),\d+ \+(\d+),\d+ @@/;
25+
const match = hunkHeader.exec(line);
2526

26-
if (match) {
27-
oldLineNumber = parseInt(match[1], 10);
28-
newLineNumber = parseInt(match[2], 10);
29-
result += `<div>${this.escapeHtml(line)}</div>`;
30-
} else if (line.startsWith('-')) {
31-
result += `<div style="color: red;">- Line ${oldLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`;
32-
oldLineNumber++;
33-
} else if (line.startsWith('+')) {
34-
result += `<div style="color: green;">+ Line ${newLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`;
35-
newLineNumber++;
36-
} else if (line.startsWith(' ')) {
37-
// Unchanged line, just increment both line counters
38-
result += `<div>${this.escapeHtml(line)}</div>`;
39-
oldLineNumber++;
40-
newLineNumber++;
41-
}
42-
});
43-
// Return the result string with color codes
44-
return result;
27+
if (match) {
28+
oldLineNumber = parseInt(match[1], 10);
29+
newLineNumber = parseInt(match[2], 10);
30+
} else if (line.startsWith('-')) {
31+
// Skip the first line difference
32+
if (oldLineNumber === 1) {
33+
oldLineNumber++;
34+
return;
35+
}
36+
result += `<div style="color: red;">- Line ${oldLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`;
37+
oldLineNumber++;
38+
} else if (line.startsWith('+')) {
39+
// Skip the first line difference
40+
if (newLineNumber === 1) {
41+
newLineNumber++;
42+
return;
43+
}
44+
result += `<div style="color: green;">+ Line ${newLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`;
45+
newLineNumber++;
46+
} else if (line.startsWith(' ')) {
47+
// Unchanged line, skip it
48+
oldLineNumber++;
49+
newLineNumber++;
50+
}
51+
});
52+
// Return the result string, or an empty string if no differences
53+
return result.trim() ? result : '';
54+
} catch (error) {
55+
Logger.logger.error('Error in FileDiffUtil', error.message);
56+
}
4557
}
4658

4759
escapeHtml(text: string): string {

0 commit comments

Comments
 (0)