Skip to content

Commit 78c0b8a

Browse files
committed
Move utils related functions to its file
1 parent e52e934 commit 78c0b8a

File tree

4 files changed

+113
-121
lines changed

4 files changed

+113
-121
lines changed

src/extractors.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import { isValidClassName } from './utils';
1+
import { isValidClassName, removeBackgroundImages, removeUrlFunctions } from './utils';
22

33
/**
44
* Represents a set of CSS class names
55
*/
66
type ClassSet = Set<string>;
77

8-
/**
9-
* Function type for extracting classes from content
10-
*/
11-
type ClassExtractor = (content: string, classes: ClassSet) => void;
12-
13-
/**
14-
* Function type for processing class strings
15-
*/
16-
type ClassProcessor = (classString: string, classes: ClassSet) => void;
17-
188
/**
199
* Options for class extraction
2010
*/
@@ -235,26 +225,6 @@ function processSimpleBinding(classBinding: string, classes: ClassSet): void {
235225
}
236226
}
237227

238-
/**
239-
* Removes background images from a CSS content string
240-
*
241-
* @param {string} content - The CSS content to process
242-
* @returns {string} The processed CSS content
243-
*/
244-
function removeBackgroundImages(content: string): string {
245-
return content.replace(/background(-image)?:\s*url\s*\([^)]*\)[^;]*;/g, '');
246-
}
247-
248-
/**
249-
* Removes url functions from a CSS content string
250-
*
251-
* @param {string} content - The CSS content to process
252-
* @returns {string} The processed CSS content
253-
*/
254-
function removeUrlFunctions(content: string): string {
255-
return content.replace(/url\s*\([^)]*\)/g, '');
256-
}
257-
258228
/**
259229
* Gets the extraction pattern based on the extraction type
260230
*

src/fileProcessors.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { extractClassesFromTemplate, extractClassesFromCss } from './extractors';
4-
5-
export interface FileInfo {
6-
name: string;
7-
path: string;
8-
}
4+
import { FileInfo, readFileContent, writeDataToFile } from './utils';
95

106
export interface ExtractedData {
117
data: string[] | string;
128
path: string;
139
}
1410

15-
/**
16-
* Reads the content of a file
17-
*
18-
* @param {string} filePath - The path to the file
19-
* @returns {string} The content of the file
20-
*/
21-
function readFileContent(filePath: string): string {
22-
return fs.readFileSync(filePath, 'utf8');
23-
}
24-
2511
/**
2612
* Processes files using the provided extractor function and data processor
2713
*
@@ -81,18 +67,6 @@ export function processCssFilesToExtractClasses(cssFiles: FileInfo[]): Extracted
8167
);
8268
}
8369

84-
/**
85-
* Writes data to a file in the specified directory
86-
*
87-
* @param {ExtractedData[]} data - Array of extracted data objects
88-
* @param {string} fileName - Name of the output file
89-
* @param {string} uncssTempDir - Path to the temporary directory
90-
*/
91-
function writeDataToFile(data: ExtractedData[], fileName: string, uncssTempDir: string): void {
92-
const outputPath = path.join(uncssTempDir, fileName);
93-
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2));
94-
}
95-
9670
/**
9771
* Writes extracted template classes to a file
9872
*

src/index.ts

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { log, findFiles } from './utils';
3+
import { log, findFiles, clearOrCreateTempDir, createFlattenedClasses, isIgnoredClass } from './utils';
44
import {
55
processTemplateFilesToExtractClasses,
66
processCssFilesToExtractClasses,
@@ -9,69 +9,9 @@ import {
99
ExtractedData
1010
} from './fileProcessors';
1111

12-
/**
13-
* Clears the temporary directory or creates it if it doesn't exist
14-
*
15-
* @param tempDir - Path to the temporary directory
16-
* @param options - Options for the operation
17-
*/
18-
function clearOrCreateTempDir(tempDir: string, options: { isDebug: boolean }): void {
19-
if (fs.existsSync(tempDir)) {
20-
// Directory exists, clear its contents
21-
fs.readdirSync(tempDir).forEach((file) => {
22-
const filePath = path.join(tempDir, file);
23-
fs.unlinkSync(filePath);
24-
});
25-
log(`[INFO] Cleared contents of ${tempDir}`, options.isDebug);
26-
} else {
27-
// Directory doesn't exist, create it
28-
fs.mkdirSync(tempDir);
29-
log(`[INFO] Created directory ${tempDir}`, options.isDebug);
30-
}
31-
}
32-
33-
/**
34-
* Creates a flattened version of extracted classes
35-
* @param {string} inputFileName - Name of the input file
36-
* @param {string} outputFileName - Name of the output file
37-
* @param {Object} options - Options for the operation
38-
* @param {boolean} options.isDebug - Whether to show debug information
39-
* @param {string} options.uncssTempDir - Path to the temporary directory
40-
*/
41-
function createFlattenedClasses(
42-
inputFileName: string,
43-
outputFileName: string,
44-
options: { isDebug: boolean; uncssTempDir: string }
45-
): void {
46-
const inputPath = path.join(options.uncssTempDir, inputFileName);
47-
const outputPath = path.join(options.uncssTempDir, outputFileName);
48-
const items: ExtractedData[] = JSON.parse(fs.readFileSync(inputPath, 'utf8'));
49-
const flattenedItems = new Set<string>();
50-
51-
items.forEach((item) => {
52-
if (typeof item.data === 'string') {
53-
item.data.split(' ').forEach((cls: string) => flattenedItems.add(cls));
54-
} else if (Array.isArray(item.data)) {
55-
item.data.forEach((cls: string) => flattenedItems.add(cls));
56-
}
57-
});
58-
59-
fs.writeFileSync(outputPath, JSON.stringify(Array.from(flattenedItems), null, 2));
60-
log(`[INFO] Flattened classes written to: ${outputPath}`, options.isDebug);
61-
}
62-
63-
/**
64-
* Checks if a class should be ignored based on the ignoredClassPatterns
65-
* @param className - The class name to check
66-
* @param ignoredClassPatterns - Array of RegExp patterns for classes to ignore
67-
* @returns True if the class should be ignored, false otherwise
68-
*/
69-
function isIgnoredClass(className: string, ignoredClassPatterns: RegExp[]): boolean {
70-
return ignoredClassPatterns.some((pattern) => pattern.test(className));
71-
}
72-
7312
/**
7413
* Compares flattened classes from template and CSS files and generates a diff report
14+
*
7515
* @param {Object} options - Options for the comparison
7616
* @param {boolean} options.isDebug - Whether to show debug information
7717
* @param {string} options.uncssTempDir - Path to the temporary directory
@@ -206,6 +146,7 @@ export interface TwigUnusedCssFinderOptions {
206146

207147
/**
208148
* Initializes and runs the unused CSS classes check
149+
*
209150
* @param {TwigUnusedCssFinderOptions} options - Options for the initialization
210151
*/
211152
function initFn(options: TwigUnusedCssFinderOptions = {}): void {
@@ -274,6 +215,7 @@ function initFn(options: TwigUnusedCssFinderOptions = {}): void {
274215

275216
/**
276217
* Runs the unused CSS classes check for Twig and Vue templates
218+
*
277219
* @param {Object} options - Configuration options
278220
* @param {string} [options.uncssTempDir='./uncss-stats'] - Temporary directory for uncss
279221
* @param {string} [options.twigDir='./templates'] - Directory containing Twig templates

src/utils.ts

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import type { ExtractedData } from './fileProcessors';
34

45
/**
56
* Logs a message if debugging is enabled
@@ -27,7 +28,7 @@ export function isValidClassName(className: string): boolean {
2728
return CLASS_NAME_REGEX.test(className);
2829
}
2930

30-
interface FileInfo {
31+
export interface FileInfo {
3132
name: string;
3233
path: string;
3334
}
@@ -59,3 +60,108 @@ export function findFiles(dir: string, pattern: RegExp): FileInfo[] {
5960
findFilesRecursive(dir);
6061
return files;
6162
}
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

Comments
 (0)