Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhinavKumar-sf committed Nov 14, 2024
2 parents f57178f + 2387e73 commit c85b416
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 34 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@types/jsdom": "^21.1.7",
"@types/lodash.chunk": "^4.2.9",
"@types/shelljs": "^0.8.15",
"cheerio": "^1.0.0",
"jsdom": "^25.0.0",
"lodash.chunk": "^4.2.0",
"open": "^8.4.2",
Expand Down
5 changes: 4 additions & 1 deletion src/migration/related/LwcMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ export class LwcMigration extends BaseRelatedObjectMigration {
const jsonData: LWCAssessmentInfo[] = [];
fileMap.forEach((fileList, dir) => {
const changeInfos: FileChangeInfo[] = [];
if (dir !== 'lwc' && !dir.endsWith('English') && !dir.includes('_') && !dir.includes('cf')) {
if (dir !== 'lwc' && !dir.endsWith('English') && !dir.includes('_') && !dir.startsWith('cf')) {
for (const file of fileList) {
if (this.isValideFile(file.name)) {
const processor = FileProcessorFactory.getFileProcessor(file.ext);
if (processor != null) {
const path = file.location;
const name = file.name + file.ext;
if (file.ext === 'xml') {
// if (fileutil.isAutogenratedFile(path)) { }
}
const diff = processor.process(file, type, this.namespace);
if (diff != null) {
const fileInfo: FileChangeInfo = {
Expand Down
32 changes: 6 additions & 26 deletions src/utils/lwcparser/htmlParser/HTMLParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@
/* eslint-disable @typescript-eslint/member-ordering */
/* eslint-disable no-console */
import * as fs from 'fs';
import * as cheerio from 'cheerio';
import { FileConstant } from '../fileutils/FileConstant';

const DEFAULT_NAMESPACE = 'c';
const TAG = 'tag';

// const { window } = new JSDOM();
// const { document } = window;

export class HTMLParser {
private parser: cheerio.CheerioAPI;
html: string;
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
constructor(htmlFilePath: string) {
// Load the HTML file and initialize cheerio
this.html = this.loadHTMLFromFile(htmlFilePath);
this.parser = cheerio.load(this.html, { xmlMode: true });
}

// Method to load HTML from a file
Expand All @@ -39,27 +32,14 @@ export class HTMLParser {
// Method to replace custom tags
public replaceTags(namespaceTag: string): Map<string, string> {
const htmlContentMap = new Map<string, string>();
// Load the HTML into cheerio
const $ = this.parser;
htmlContentMap.set(FileConstant.BASE_CONTENT, this.html);
// Find all tags that contain the substring "omnistudio" in their tag name
$('*').each((i, element) => {
if (element.type === TAG && element.name && element.name.includes(namespaceTag + '-')) {
// Create a new tag with the same content and attributes as the old tag
const newTag = DEFAULT_NAMESPACE + element.name.substring(element.name.indexOf('-'));
const newElement = $(`<${newTag}>`).html($(element).html());
// Use a regular expression to match <omnistudio-input> to </omnistudio-input>

// Copy all attributes from the old element to the new one
Object.keys(element.attribs).forEach((attr) => {
newElement.attr(attr, $(element).attr(attr));
});
this.html = this.html
.replace('<' + namespaceTag, '<' + DEFAULT_NAMESPACE)
.replace('</' + namespaceTag, '</' + DEFAULT_NAMESPACE);

// Replace the old element with the new one
$(element).replaceWith(newElement);
}
});
$.html().replace(/\n\s*/g, '');
htmlContentMap.set(FileConstant.MODIFIED_CONTENT, $.html());
htmlContentMap.set(FileConstant.MODIFIED_CONTENT, this.html);
return htmlContentMap;
}

Expand All @@ -76,6 +56,6 @@ export class HTMLParser {

// Optional: Method to get the modified HTML as a string
public getModifiedHTML(): string {
return this.parser.html();
return this.html;
}
}
5 changes: 5 additions & 0 deletions src/utils/lwcparser/xmlParser/XmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ export class XmlParser {
throw error;
}
}

// public isAutogenratedFile(filePath: string) {
// this.fileContent = fs.readFileSync(filePath, 'utf-8');
// return this.fileContent.includes('OmniScript Auto-generated');
// }
}
8 changes: 2 additions & 6 deletions test/utils/lwc/parser/htmlparser.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
import { expect } from 'chai';
import { expect } from '@salesforce/command/lib/test';
import { HTMLParser } from '../../../../src/utils/lwcparser/htmlParser/HTMLParser';
import { FileDiffUtil } from '../../../../src/utils/lwcparser/fileutils/FileDiffUtil';

describe('HTMLParser test class', () => {
const mockFilePath = 'test/utils/lwc/parser/input/test.html';
it('should read file content correctly', () => {
const htmlParser = new HTMLParser(mockFilePath);
const html: Map<string, string> = htmlParser.replaceTags('omnistudio');
// eslint-disable-next-line no-console
console.log(new FileDiffUtil().getFileDiff('file.txt', html.get('original'), html.get('modified')));
htmlParser.saveToFile('test/utils/lwc/parser/output/test.html', html.get('modified'));
htmlParser.replaceTags('omnistudio');
expect(htmlParser.getModifiedHTML()).contains('c-input');
});
});

0 comments on commit c85b416

Please sign in to comment.