|
1 | 1 | import * as fs from 'fs';
|
2 | 2 | import * as path from 'path';
|
| 3 | +import type { ExtractedData } from './fileProcessors'; |
3 | 4 |
|
4 | 5 | /**
|
5 | 6 | * Logs a message if debugging is enabled
|
@@ -27,7 +28,7 @@ export function isValidClassName(className: string): boolean {
|
27 | 28 | return CLASS_NAME_REGEX.test(className);
|
28 | 29 | }
|
29 | 30 |
|
30 |
| -interface FileInfo { |
| 31 | +export interface FileInfo { |
31 | 32 | name: string;
|
32 | 33 | path: string;
|
33 | 34 | }
|
@@ -59,3 +60,108 @@ export function findFiles(dir: string, pattern: RegExp): FileInfo[] {
|
59 | 60 | findFilesRecursive(dir);
|
60 | 61 | return files;
|
61 | 62 | }
|
| 63 | + |
| 64 | +/** |
| 65 | + * Writes data to a file in the specified directory |
| 66 | + * |
| 67 | + * @param {ExtractedData[]} data - Array of extracted data objects |
| 68 | + * @param {string} fileName - Name of the output file |
| 69 | + * @param {string} uncssTempDir - Path to the temporary directory |
| 70 | + */ |
| 71 | +export function writeDataToFile(data: ExtractedData[], fileName: string, uncssTempDir: string): void { |
| 72 | + const outputPath = path.join(uncssTempDir, fileName); |
| 73 | + fs.writeFileSync(outputPath, JSON.stringify(data, null, 2)); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Clears the temporary directory or creates it if it doesn't exist |
| 78 | + * |
| 79 | + * @param tempDir - Path to the temporary directory |
| 80 | + * @param options - Options for the operation |
| 81 | + */ |
| 82 | +export function clearOrCreateTempDir(tempDir: string, options: { isDebug: boolean }): void { |
| 83 | + if (fs.existsSync(tempDir)) { |
| 84 | + // Directory exists, clear its contents |
| 85 | + fs.readdirSync(tempDir).forEach((file) => { |
| 86 | + const filePath = path.join(tempDir, file); |
| 87 | + fs.unlinkSync(filePath); |
| 88 | + }); |
| 89 | + log(`[INFO] Cleared contents of ${tempDir}`, options.isDebug); |
| 90 | + } else { |
| 91 | + // Directory doesn't exist, create it |
| 92 | + fs.mkdirSync(tempDir); |
| 93 | + log(`[INFO] Created directory ${tempDir}`, options.isDebug); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Creates a flattened version of extracted classes |
| 99 | + * |
| 100 | + * @param {string} inputFileName - Name of the input file |
| 101 | + * @param {string} outputFileName - Name of the output file |
| 102 | + * @param {Object} options - Options for the operation |
| 103 | + * @param {boolean} options.isDebug - Whether to show debug information |
| 104 | + * @param {string} options.uncssTempDir - Path to the temporary directory |
| 105 | + */ |
| 106 | +export function createFlattenedClasses( |
| 107 | + inputFileName: string, |
| 108 | + outputFileName: string, |
| 109 | + options: { isDebug: boolean; uncssTempDir: string } |
| 110 | +): void { |
| 111 | + const inputPath = path.join(options.uncssTempDir, inputFileName); |
| 112 | + const outputPath = path.join(options.uncssTempDir, outputFileName); |
| 113 | + const items: ExtractedData[] = JSON.parse(fs.readFileSync(inputPath, 'utf8')); |
| 114 | + const flattenedItems = new Set<string>(); |
| 115 | + |
| 116 | + items.forEach((item) => { |
| 117 | + if (typeof item.data === 'string') { |
| 118 | + item.data.split(' ').forEach((cls: string) => flattenedItems.add(cls)); |
| 119 | + } else if (Array.isArray(item.data)) { |
| 120 | + item.data.forEach((cls: string) => flattenedItems.add(cls)); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + fs.writeFileSync(outputPath, JSON.stringify(Array.from(flattenedItems), null, 2)); |
| 125 | + log(`[INFO] Flattened classes written to: ${outputPath}`, options.isDebug); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Checks if a class should be ignored based on the ignoredClassPatterns |
| 130 | + * |
| 131 | + * @param className - The class name to check |
| 132 | + * @param ignoredClassPatterns - Array of RegExp patterns for classes to ignore |
| 133 | + * @returns {boolean} True if the class should be ignored, false otherwise |
| 134 | + */ |
| 135 | +export function isIgnoredClass(className: string, ignoredClassPatterns: RegExp[]): boolean { |
| 136 | + return ignoredClassPatterns.some((pattern) => pattern.test(className)); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Removes background images from a CSS content string |
| 141 | + * |
| 142 | + * @param {string} content - The CSS content to process |
| 143 | + * @returns {string} The processed CSS content |
| 144 | + */ |
| 145 | +export function removeBackgroundImages(content: string): string { |
| 146 | + return content.replace(/background(-image)?:\s*url\s*\([^)]*\)[^;]*;/g, ''); |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Removes url functions from a CSS content string |
| 151 | + * |
| 152 | + * @param {string} content - The CSS content to process |
| 153 | + * @returns {string} The processed CSS content |
| 154 | + */ |
| 155 | +export function removeUrlFunctions(content: string): string { |
| 156 | + return content.replace(/url\s*\([^)]*\)/g, ''); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Reads the content of a file |
| 161 | + * |
| 162 | + * @param {string} filePath - The path to the file |
| 163 | + * @returns {string} The content of the file |
| 164 | + */ |
| 165 | +export function readFileContent(filePath: string): string { |
| 166 | + return fs.readFileSync(filePath, 'utf8'); |
| 167 | +} |
0 commit comments