@@ -6,39 +6,45 @@ import {
66 processCssFilesToExtractClasses ,
77 writeTemplateClassesToFile ,
88 writeCssSelectorsToFile ,
9- ExtractedData // Add this import
9+ ExtractedData
1010} from './fileProcessors' ;
1111
1212/**
13- * Clears the UNCSS_TEMP_DIR directory or creates it if it doesn't exist
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
1417 */
15- function clearOrCreateTempDir ( tempDir : string , { isDebug = false } : { isDebug : boolean } ) : void {
18+ function clearOrCreateTempDir ( tempDir : string , options : { isDebug : boolean } ) : void {
1619 if ( fs . existsSync ( tempDir ) ) {
1720 // Directory exists, clear its contents
1821 fs . readdirSync ( tempDir ) . forEach ( ( file ) => {
1922 const filePath = path . join ( tempDir , file ) ;
2023 fs . unlinkSync ( filePath ) ;
2124 } ) ;
22- log ( `Cleared contents of ${ tempDir } ` , isDebug ) ;
25+ log ( `[INFO] Cleared contents of ${ tempDir } ` , options . isDebug ) ;
2326 } else {
2427 // Directory doesn't exist, create it
2528 fs . mkdirSync ( tempDir ) ;
26- log ( `Created directory ${ tempDir } ` , isDebug ) ;
29+ log ( `[INFO] Created directory ${ tempDir } ` , options . isDebug ) ;
2730 }
2831}
2932
3033/**
3134 * Creates a flattened version of extracted classes
32- * @param inputFileName - Name of the input file
33- * @param outputFileName - Name of the output file
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
3440 */
3541function createFlattenedClasses (
3642 inputFileName : string ,
3743 outputFileName : string ,
38- { isDebug = false , uncssTempDir } : { isDebug : boolean ; uncssTempDir : string }
44+ options : { isDebug : boolean ; uncssTempDir : string }
3945) : void {
40- const inputPath = path . join ( uncssTempDir , inputFileName ) ;
41- const outputPath = path . join ( uncssTempDir , outputFileName ) ;
46+ const inputPath = path . join ( options . uncssTempDir , inputFileName ) ;
47+ const outputPath = path . join ( options . uncssTempDir , outputFileName ) ;
4248 const items : ExtractedData [ ] = JSON . parse ( fs . readFileSync ( inputPath , 'utf8' ) ) ;
4349 const flattenedItems = new Set < string > ( ) ;
4450
@@ -51,7 +57,7 @@ function createFlattenedClasses(
5157 } ) ;
5258
5359 fs . writeFileSync ( outputPath , JSON . stringify ( Array . from ( flattenedItems ) , null , 2 ) ) ;
54- log ( `Flattened classes written to: ${ outputPath } ` , isDebug ) ;
60+ log ( `[INFO] Flattened classes written to: ${ outputPath } ` , options . isDebug ) ;
5561}
5662
5763/**
@@ -66,112 +72,44 @@ function isIgnoredClass(className: string, ignoredClassPatterns: RegExp[]): bool
6672
6773/**
6874 * Compares flattened classes from template and CSS files and generates a diff report
75+ * @param {Object } options - Options for the comparison
76+ * @param {boolean } options.isDebug - Whether to show debug information
77+ * @param {string } options.uncssTempDir - Path to the temporary directory
78+ * @param {RegExp[] } options.ignoredClassPatterns - Array of RegExp patterns for classes to ignore
79+ * @param {string } options.outputFile - Name of the output file for the diff report
80+ * @param {string } options.classesFromTemplatesFlattenedFileName - Name of the file containing flattened template classes
81+ * @param {string } options.classesFromCssFlattenedFileName - Name of the file containing flattened CSS classes
6982 */
70- function getUnusedCssClasses ( {
71- isDebug = false ,
72- uncssTempDir,
73- ignoredClassPatterns,
74- outputFile,
75- classesFromTemplatesFlattenedFileName,
76- classesFromCssFlattenedFileName
77- } : {
83+ function getUnusedCssClasses ( options : {
7884 isDebug : boolean ;
7985 uncssTempDir : string ;
8086 ignoredClassPatterns : RegExp [ ] ;
8187 outputFile : string ;
8288 classesFromTemplatesFlattenedFileName : string ;
8389 classesFromCssFlattenedFileName : string ;
8490} ) : void {
85- const templateClassesPath = path . join ( uncssTempDir , classesFromTemplatesFlattenedFileName ) ;
86- const cssClassesPath = path . join ( uncssTempDir , classesFromCssFlattenedFileName ) ;
91+ const templateClassesPath = path . join ( options . uncssTempDir , options . classesFromTemplatesFlattenedFileName ) ;
92+ const cssClassesPath = path . join ( options . uncssTempDir , options . classesFromCssFlattenedFileName ) ;
8793
8894 const templateClassesList = new Set < string > ( JSON . parse ( fs . readFileSync ( templateClassesPath , 'utf8' ) ) ) ;
8995 const cssClassesList = new Set < string > ( JSON . parse ( fs . readFileSync ( cssClassesPath , 'utf8' ) ) ) ;
9096
91- const cssClassesNotFoundInTemplates = Array . from ( cssClassesList ) . filter ( ( cls : string ) => ! templateClassesList . has ( cls ) && ! isIgnoredClass ( cls , ignoredClassPatterns ) ) ;
97+ const cssClassesNotFoundInTemplates = Array . from ( cssClassesList ) . filter (
98+ ( cls ) => ! templateClassesList . has ( cls ) && ! isIgnoredClass ( cls , options . ignoredClassPatterns )
99+ ) ;
92100
93- const templateClassesNotFoundInCss = Array . from ( templateClassesList ) . filter ( ( cls : string ) => ! cssClassesList . has ( cls ) && ! isIgnoredClass ( cls , ignoredClassPatterns ) ) ;
101+ const templateClassesNotFoundInCss = Array . from ( templateClassesList ) . filter (
102+ ( cls ) => ! cssClassesList . has ( cls ) && ! isIgnoredClass ( cls , options . ignoredClassPatterns )
103+ ) ;
94104
95105 const diffReport = {
96106 cssClassesNotFoundInTemplates,
97107 templateClassesNotFoundInCss,
98108 } ;
99109
100- const outputPath = path . join ( uncssTempDir , outputFile ) ;
110+ const outputPath = path . join ( options . uncssTempDir , options . outputFile ) ;
101111 fs . writeFileSync ( outputPath , JSON . stringify ( diffReport , null , 2 ) ) ;
102- log ( `Diff report written to: ${ outputPath } ` , isDebug ) ;
103- }
104-
105- interface InitOptions {
106- isDebug ?: boolean ;
107- }
108-
109- /**
110- * Initializes and runs the unused CSS classes check
111- * @param options - Options for the initialization
112- */
113- function init ( options : TwigUnusedCssFinderOptions = { } ) : void {
114- console . info ( '------------ START CheckUnusedCssClasses ------------' ) ;
115-
116- const {
117- uncssTempDir = './uncss-stats' ,
118- twigDir = './templates' ,
119- twigPattern = / \. t w i g $ / ,
120- vueDir = './assets/js' ,
121- vuePattern = / \. v u e $ / ,
122- cssDir = './public/assets' ,
123- cssPattern = / \. c s s $ / ,
124- ignoredClassPatterns = [ / ^ j s - / ] ,
125- classesFromCssFileName = 'all_classes_from_css.json' ,
126- classesFromCssFlattenedFileName = 'all_classes_from_css_flattened.json' ,
127- classesFromTemplatesFileName = 'all_classes_from_vue_and_twig.json' ,
128- classesFromTemplatesFlattenedFileName = 'all_classes_from_vue_and_twig_flattened.json' ,
129- outputFile = 'unused_css_classes_report.json' ,
130- isDebug = false ,
131- showHelperInfos = false ,
132- } = options ;
133-
134- log ( '[TASK] Clearing or creating temp directory' , isDebug ) ;
135- clearOrCreateTempDir ( uncssTempDir , { isDebug } ) ;
136-
137- log ( '[TASK] Reading template files' , isDebug ) ;
138- const templateFiles = [
139- ...findFiles ( twigDir , twigPattern ) ,
140- ...findFiles ( vueDir , vuePattern ) ,
141- ] ;
142-
143- log ( '[TASK] Processing template files and extracting CSS classes' , isDebug ) ;
144- const templateClasses : ExtractedData [ ] = processTemplateFilesToExtractClasses ( templateFiles ) ;
145-
146- log ( '[TASK] Reading CSS files' , isDebug ) ;
147- const cssFiles = findFiles ( cssDir , cssPattern ) ;
148-
149- log ( '[TASK] Processing CSS files' , isDebug ) ;
150- const cssSelectors : ExtractedData [ ] = processCssFilesToExtractClasses ( cssFiles ) ;
151-
152- log ( '[TASK] Writing extracted CSS classes to file' , isDebug ) ;
153- writeTemplateClassesToFile ( templateClasses , classesFromTemplatesFileName , uncssTempDir ) ;
154-
155- log ( '[TASK] Writing extracted CSS selectors to file' , isDebug ) ;
156- writeCssSelectorsToFile ( cssSelectors , classesFromCssFileName , uncssTempDir ) ;
157-
158- log ( '[TASK] Creating flattened version of template classes' , isDebug ) ;
159- createFlattenedClasses ( classesFromTemplatesFileName , classesFromTemplatesFlattenedFileName , { isDebug, uncssTempDir } ) ;
160-
161- log ( '[TASK] Creating flattened version of CSS classes' , isDebug ) ;
162- createFlattenedClasses ( classesFromCssFileName , classesFromCssFlattenedFileName , { isDebug, uncssTempDir } ) ;
163-
164- log ( '[TASK] Comparing flattened classes' , isDebug ) ;
165- getUnusedCssClasses ( {
166- isDebug,
167- uncssTempDir,
168- ignoredClassPatterns,
169- outputFile,
170- classesFromTemplatesFlattenedFileName,
171- classesFromCssFlattenedFileName
172- } ) ;
173-
174- console . info ( '------------ END CheckUnusedCssClasses ------------' ) ;
112+ log ( `[INFO] Diff report written to: ${ outputPath } ` , options . isDebug ) ;
175113}
176114
177115export interface TwigUnusedCssFinderOptions {
@@ -266,6 +204,74 @@ export interface TwigUnusedCssFinderOptions {
266204 showHelperInfos ?: boolean ;
267205}
268206
207+ /**
208+ * Initializes and runs the unused CSS classes check
209+ * @param {TwigUnusedCssFinderOptions } options - Options for the initialization
210+ */
211+ function initFn ( options : TwigUnusedCssFinderOptions = { } ) : void {
212+ console . info ( '------------ [START] Twig unused css finder ------------' ) ;
213+
214+ const {
215+ uncssTempDir = './uncss-stats' ,
216+ twigDir = './templates' ,
217+ twigPattern = / \. t w i g $ / ,
218+ vueDir = './assets/js' ,
219+ vuePattern = / \. v u e $ / ,
220+ cssDir = './public/assets' ,
221+ cssPattern = / \. c s s $ / ,
222+ ignoredClassPatterns = [ / ^ j s - / ] ,
223+ classesFromCssFileName = 'all_classes_from_css.json' ,
224+ classesFromCssFlattenedFileName = 'all_classes_from_css_flattened.json' ,
225+ classesFromTemplatesFileName = 'all_classes_from_vue_and_twig.json' ,
226+ classesFromTemplatesFlattenedFileName = 'all_classes_from_vue_and_twig_flattened.json' ,
227+ outputFile = 'unused_css_classes_report.json' ,
228+ isDebug = false ,
229+ showHelperInfos = false ,
230+ } = options ;
231+
232+ log ( '[TASK] Clearing or creating temp directory' , isDebug ) ;
233+ clearOrCreateTempDir ( uncssTempDir , { isDebug } ) ;
234+
235+ log ( '[TASK] Reading template files' , isDebug ) ;
236+ const templateFiles = [
237+ ...findFiles ( twigDir , twigPattern ) ,
238+ ...findFiles ( vueDir , vuePattern ) ,
239+ ] ;
240+
241+ log ( '[TASK] Processing template files and extracting CSS classes' , isDebug ) ;
242+ const templateClasses : ExtractedData [ ] = processTemplateFilesToExtractClasses ( templateFiles ) ;
243+
244+ log ( '[TASK] Reading CSS files' , isDebug ) ;
245+ const cssFiles = findFiles ( cssDir , cssPattern ) ;
246+
247+ log ( '[TASK] Processing CSS files' , isDebug ) ;
248+ const cssSelectors : ExtractedData [ ] = processCssFilesToExtractClasses ( cssFiles ) ;
249+
250+ log ( '[TASK] Writing extracted CSS classes to file' , isDebug ) ;
251+ writeTemplateClassesToFile ( templateClasses , classesFromTemplatesFileName , uncssTempDir ) ;
252+
253+ log ( '[TASK] Writing extracted CSS selectors to file' , isDebug ) ;
254+ writeCssSelectorsToFile ( cssSelectors , classesFromCssFileName , uncssTempDir ) ;
255+
256+ log ( '[TASK] Creating flattened version of template classes' , isDebug ) ;
257+ createFlattenedClasses ( classesFromTemplatesFileName , classesFromTemplatesFlattenedFileName , { isDebug, uncssTempDir } ) ;
258+
259+ log ( '[TASK] Creating flattened version of CSS classes' , isDebug ) ;
260+ createFlattenedClasses ( classesFromCssFileName , classesFromCssFlattenedFileName , { isDebug, uncssTempDir } ) ;
261+
262+ log ( '[TASK] Comparing flattened classes' , isDebug ) ;
263+ getUnusedCssClasses ( {
264+ isDebug,
265+ uncssTempDir,
266+ ignoredClassPatterns,
267+ outputFile,
268+ classesFromTemplatesFlattenedFileName,
269+ classesFromCssFlattenedFileName
270+ } ) ;
271+
272+ console . info ( '------------ [END] Twig unused css finder ------------' ) ;
273+ }
274+
269275/**
270276 * Runs the unused CSS classes check for Twig and Vue templates
271277 * @param {Object } options - Configuration options
@@ -285,6 +291,6 @@ export interface TwigUnusedCssFinderOptions {
285291 * @param {boolean } [options.isDebug=false] - Enable debug logging
286292 * @param {boolean } [options.showHelperInfos=false] - Show additional helper information
287293 */
288- export function twigUnusedCssFinder ( options : Partial < TwigUnusedCssFinderOptions > = { } ) {
289- return init ( options ) ;
294+ export function twigUnusedCssFinder ( options : Partial < TwigUnusedCssFinderOptions > = { } ) : void {
295+ initFn ( options ) ;
290296}
0 commit comments