Skip to content

Commit dea75ea

Browse files
committed
chore: add fileUtil
1 parent 62ba8d7 commit dea75ea

File tree

6 files changed

+197
-10
lines changed

6 files changed

+197
-10
lines changed

src/migration/related/ApexMigration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TokenUpdater,
1414
} from '../../utils/apex/parser/apexparser';
1515
import { sfProject } from '../../utils/sfcli/project/sfProject';
16-
import { fileUtil, File } from '../../utils/file/fileUtil';
16+
import { FileUtil, File } from '../../utils/file/fileUtil';
1717
import { Logger } from '../../utils/logger';
1818
import { ApexAssessmentInfo } from '../../utils';
1919
import { FileDiffUtil } from '../../utils/lwcparser/fileutils/FileDiffUtil';
@@ -71,7 +71,7 @@ export class ApexMigration extends BaseRelatedObjectMigration {
7171
public processApexFiles(dir: string): ApexAssessmentInfo[] {
7272
dir += APEX_CLASS_PATH;
7373
let files: File[] = [];
74-
files = fileUtil.readFilesSync(dir);
74+
files = FileUtil.readFilesSync(dir);
7575
const fileAssessmentInfo: ApexAssessmentInfo[] = [];
7676
for (const file of files) {
7777
if (file.ext !== '.cls') continue;

src/migration/related/LwcMigration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-shadow */
22
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
33
import * as shell from 'shelljs';
4-
import { fileUtil, File } from '../../utils/file/fileUtil';
4+
import { FileUtil, File } from '../../utils/file/fileUtil';
55
import { sfProject } from '../../utils/sfcli/project/sfProject';
66
import { Logger } from '../../utils/logger';
77
import { FileProcessorFactory } from '../../utils/lwcparser/fileutils/FileProcessorFactory';
@@ -51,7 +51,7 @@ export class LwcMigration extends BaseRelatedObjectMigration {
5151
dir += LWC_DIR_PATH;
5252
let filesMap: Map<string, File[]>;
5353
try {
54-
filesMap = fileUtil.readAndProcessFiles(dir, 'OmniScript Auto-generated');
54+
filesMap = FileUtil.readAndProcessFiles(dir, 'OmniScript Auto-generated');
5555
} catch (error) {
5656
Logger.logger.error('Error in reading files', error);
5757
}

src/utils/file/fileUtil.ts

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-return */
2+
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
3+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
4+
/* eslint-disable no-console */
5+
import * as fs from 'fs';
6+
import * as path from 'path';
7+
import { Logger } from '../logger';
8+
9+
export class FileUtil {
10+
public static readFilesSync(dir: string): File[] {
11+
const files: File[] = [];
12+
fs.readdirSync(dir).forEach((filename) => {
13+
const name = path.parse(filename).name;
14+
const ext = path.parse(filename).ext;
15+
const filepath = path.resolve(dir, filename);
16+
const stat = fs.statSync(filepath);
17+
const isFile = stat.isFile();
18+
19+
if (isFile) files.push(new File(name, filepath, ext));
20+
});
21+
return files;
22+
}
23+
24+
public static readAllFiles(
25+
dirPath: string,
26+
fileMap: Map<string, File[]> = new Map<string, File[]>()
27+
): Map<string, File[]> {
28+
if (!fs.existsSync(dirPath)) {
29+
console.error(`Directory does not exist: ${dirPath}`);
30+
return fileMap; // Return the map as is
31+
}
32+
// Read the directory contents
33+
const files = fs.readdirSync(dirPath);
34+
35+
// Initialize the list of files for the current directory
36+
const currentDirFiles: File[] = [];
37+
38+
files.forEach((filename) => {
39+
// Construct the full file/directory path
40+
const filePath = path.join(dirPath, filename);
41+
42+
// Check if the current path is a directory or a file
43+
if (fs.statSync(filePath).isDirectory()) {
44+
// If it's a directory, recurse into it
45+
this.readAllFiles(filePath, fileMap);
46+
} else {
47+
const name = path.parse(filename).name;
48+
const ext = path.parse(filename).ext;
49+
const filepath = path.resolve(dirPath, filename);
50+
const stat = fs.statSync(filepath);
51+
const isFile = stat.isFile();
52+
53+
// If it's a file, add it to the current directory's file list
54+
if (isFile) {
55+
currentDirFiles.push(new File(name, filepath, ext));
56+
}
57+
}
58+
});
59+
60+
// Add the current directory and its files to the map
61+
if (currentDirFiles.length > 0) {
62+
fileMap.set(path.basename(dirPath), currentDirFiles);
63+
}
64+
return fileMap;
65+
}
66+
67+
// Method to save modified HTML back to a file
68+
public static saveToFile(outputFilePath: string, modifiedHtml: string): void {
69+
try {
70+
fs.writeFileSync(outputFilePath, modifiedHtml);
71+
Logger.logger.info(`Modified HTML saved to ${outputFilePath}`);
72+
} catch (error) {
73+
console.error(`Error writing file to disk: ${error}`);
74+
throw error;
75+
}
76+
}
77+
78+
public static readAndProcessFiles(
79+
baseDir: string,
80+
searchString: string,
81+
fileMap: Map<string, File[]> = new Map<string, File[]>()
82+
): Map<string, File[]> {
83+
const subDirectories = fs.readdirSync(baseDir).filter((dir) => {
84+
const fullPath = path.join(baseDir, dir);
85+
return fs.statSync(fullPath).isDirectory();
86+
});
87+
88+
for (const subDirectory of subDirectories) {
89+
Logger.logger.info(`Processing subdirectory: ${subDirectory}`);
90+
const subDirPath = path.join(baseDir, subDirectory);
91+
92+
// Check the XML file for the substring
93+
const xmlFilePath = path.join(subDirPath, `${subDirectory}.js-meta.xml`);
94+
if (fs.existsSync(xmlFilePath)) {
95+
if (this.doesSubstringExist(xmlFilePath, searchString)) {
96+
Logger.logger.info(`Substring found in ${xmlFilePath}. Skipping all files in ${subDirectory}.`);
97+
continue; // Move to the next subdirectory
98+
}
99+
}
100+
101+
// Process all files if substring is not found
102+
const currentDirFiles: File[] = [];
103+
const files = fs
104+
.readdirSync(subDirPath)
105+
.filter((file) => file.endsWith('.html') || file.endsWith('.js') || file.endsWith('.xml'));
106+
files.forEach((file) => {
107+
const filePath = path.join(subDirPath, file);
108+
Logger.logger.info(`Processing file: ${filePath}`);
109+
const name = path.parse(file).name;
110+
const ext = path.parse(file).ext;
111+
const filepath = path.resolve(subDirPath, file);
112+
currentDirFiles.push(new File(name, filepath, ext));
113+
fileMap.set(path.basename(subDirPath), currentDirFiles);
114+
});
115+
}
116+
return fileMap;
117+
}
118+
119+
/**
120+
* Check if a substring exists in an XML file
121+
*
122+
* @param filePath Path of the XML file
123+
* @param searchString Substring to search for
124+
* @returns true if substring exists, otherwise false
125+
*/
126+
private static doesSubstringExist = (filePath: string, searchString: string): boolean => {
127+
try {
128+
const fileContent = fs.readFileSync(filePath, 'utf-8');
129+
return fileContent.includes(searchString);
130+
} catch (error) {
131+
console.error(`Error reading file ${filePath}:`, error);
132+
return false;
133+
}
134+
};
135+
}
136+
137+
export class File {
138+
public name: string;
139+
public location: string;
140+
public ext: string;
141+
public constructor(name: string, location: string, ext: string) {
142+
this.name = name;
143+
this.location = location;
144+
this.ext = ext;
145+
}
146+
}
147+
148+
/**
149+
* Copies `.js` and `.css` files from a source directory (based on `folderName`)
150+
* to a specified destination directory.
151+
*
152+
* @param folderName - The subdirectory under `/src/` where source asset files are located (e.g., `'javascripts'`, `'styles'`).
153+
* @param destDir - The absolute or relative path to the destination directory where the assets should be copied.
154+
*
155+
* @remarks
156+
* - If the destination directory does not exist, the method logs a warning and exits.
157+
* - Only `.js` and `.css` files are copied.
158+
* - The source files remain in place after copying.
159+
*/
160+
export function pushAssestUtilites(folderName: string, destDir: string): void {
161+
const sourceDir = path.join(process.cwd(), 'src', folderName);
162+
163+
if (!fs.existsSync(destDir)) {
164+
Logger.logger.warn(`Destination directory does not exist: ${destDir}`);
165+
return;
166+
}
167+
168+
try {
169+
const files = fs.readdirSync(sourceDir);
170+
171+
files.forEach((file) => {
172+
const ext = path.extname(file);
173+
if (ext === '.js' || ext === '.css') {
174+
const srcPath = path.join(sourceDir, file);
175+
const destPath = path.join(destDir, file);
176+
177+
try {
178+
fs.copyFileSync(srcPath, destPath);
179+
} catch (copyErr) {
180+
Logger.logger.error(`Error copying file ${srcPath} to ${destPath}: ${copyErr}`);
181+
}
182+
}
183+
});
184+
} catch (readDirErr) {
185+
Logger.logger.error(`Error reading directory ${sourceDir}: ${readDirErr}`);
186+
}
187+
}

src/utils/lwcparser/fileutils/HtmlFileProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
22
import { FileProcessor } from '../../../utils';
3-
import { File, fileUtil } from '../../file/fileUtil';
3+
import { File, FileUtil } from '../../file/fileUtil';
44
import { HTMLParser } from '../../lwcparser/htmlParser/HTMLParser';
55
import { FileConstant } from '../fileutils/FileConstant';
66
import { FileDiffUtil } from './FileDiffUtil';
@@ -22,7 +22,7 @@ export class HtmlFileProcessor implements FileProcessor {
2222
fileContent.get(FileConstant.MODIFIED_CONTENT)
2323
);
2424
if (type != null && type === 'migration' && diff.length > 0) {
25-
fileUtil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
25+
FileUtil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
2626
}
2727
return JSON.stringify(diff);
2828
}

src/utils/lwcparser/fileutils/JavascriptFileProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
55
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
66
import { FileProcessor } from '../../../utils';
7-
import { File, fileUtil } from '../../file/fileUtil';
7+
import { File, FileUtil } from '../../file/fileUtil';
88
import { JavaScriptParser } from '../../lwcparser/jsParser/JavaScriptParser';
99
import { FileConstant } from '../fileutils/FileConstant';
1010
import { FileDiffUtil } from './FileDiffUtil';
@@ -26,7 +26,7 @@ export class JavascriptFileProcessor implements FileProcessor {
2626
fileContent.get(FileConstant.MODIFIED_CONTENT)
2727
);
2828
if (type != null && type === 'migration') {
29-
fileUtil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
29+
FileUtil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
3030
}
3131
return JSON.stringify(diff);
3232
}

src/utils/lwcparser/fileutils/XmlFileProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* eslint-disable no-console */
44
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
55
import { FileProcessor } from '../../../utils';
6-
import { File, fileUtil } from '../../file/fileUtil';
6+
import { File, FileUtil } from '../../file/fileUtil';
77
import { XmlParser } from '../../lwcparser/xmlParser/XmlParser';
88
import { FileConstant } from '../fileutils/FileConstant';
99
import { FileDiffUtil } from './FileDiffUtil';
@@ -26,7 +26,7 @@ export class XmlFileProcessor implements FileProcessor {
2626
fileContent.get(FileConstant.MODIFIED_CONTENT)
2727
);
2828
if (type != null && type === 'migration') {
29-
fileUtil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
29+
FileUtil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
3030
}
3131
return JSON.stringify(diff);
3232
}

0 commit comments

Comments
 (0)