import fs from 'fs' ; import unusedClassMapper from '../helpers/unusedClassMapper' ; import findHtml from './../helpers/findHtml' ; import findUnusedCss from './findUnusedCss' ; export default class UnusedClasses { private allHtmlContent = '' ; private styleExtension : string ; constructor ( private _styleExtension ) { this . styleExtension = _styleExtension ; } getUnusedClasses ( projectPath ) { const list = findHtml ( projectPath ) ; return this . mapClasses ( list ) . then ( ( r ) => { return r . filter ( ( c ) => { const unusedCssClasses = c ?. length ? c [ 0 ] : [ ] ; return unusedCssClasses && unusedCssClasses . length > 0 ; } ) ; } ) as Promise < [ [ string [ ] , string ] ] > ; } getGlobalUnusedClasses ( globalStyles ) { const classes = findUnusedCss ( this . allHtmlContent , globalStyles ) ; return classes ; } private mapClasses ( list ) { const promiseArray = list . map ( ( element ) => { const htmlPath = element ; const htmlContent = fs . readFileSync ( htmlPath , 'utf8' ) ; const cssPath = htmlPath . replace ( '.html' , this . styleExtension ) ; this . allHtmlContent += htmlContent ; return unusedClassMapper ( cssPath , htmlContent , htmlPath ) ; } ) ; return Promise . all ( promiseArray ) ; } }