|
| 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 | +} |
0 commit comments