import type { RawPkg , StyleType } from '../@types/pkg' ; import { datadog } from '../utils/datadog' ; import type { File } from './index' ; const styleFileExtensions = [ 'css' , 'less' , 'scss' ] ; const styleFilePattern = createFilePattern ( styleFileExtensions ) ; const jsFileExtensions = [ 'js' , 'mjs' , 'cjs' ] ; const jsFilePattern = createFilePattern ( jsFileExtensions ) ; function createFilePattern ( extensions ) { const extPattern = extensions . join ( '|' ) ; return new RegExp ( `^(?:(?!\\/(docs?|documentation|examples?|samples?|demos?|tests?)\\/)(?!\\/[._]).)+\\.(${ extPattern })$` , 'i' ) ; } export function getStyleTypes ( pkg , filelist ) { const start = Date . now ( ) ; try { const styleTypes = new Set < StyleType > ( pkg . styleTypes ) ; for ( const file of filelist ) { if ( ! styleFilePattern . test ( file . name ) ) { continue ; } const type = file . name . split ( '.' ) . pop ( ) ; if ( type ) { styleTypes . add ( type . toLowerCase ( ) ) ; } } if ( styleTypes . size === 0 ) { styleTypes . add ( 'none' ) ; } return { styleTypes : [ ... styleTypes ] } ; } finally { datadog . timing ( 'pkgTypes.getStyleTypes' , Date . now ( ) - start ) ; } } export function getStyleTypesForAll ( pkgs , filelists ) { const start = Date . now ( ) ; const all = pkgs . map ( ( pkg , index ) => { return getStyleTypes ( pkg , filelists [ index ] || [ ] ) ; } ) ; datadog . timing ( 'pkgTypes.getStyleTypesForAll' , Date . now ( ) - start ) ; return all ; } export function getModuleTypes ( pkg , filelist ) { const start = Date . now ( ) ; try { if ( ! pkg . moduleTypes . includes ( 'unknown' ) ) { return { moduleTypes : pkg . moduleTypes } ; } for ( const file of filelist ) { if ( jsFilePattern . test ( file . name ) ) { return { moduleTypes : pkg . moduleTypes } ; } } return { moduleTypes : [ 'none' ] } ; } finally { datadog . timing ( 'pkgTypes.getModuleTypes' , Date . now ( ) - start ) ; } } export function getModuleTypesForAll ( pkgs , filelists ) { const start = Date . now ( ) ; const all = pkgs . map ( ( pkg , index ) => { return getModuleTypes ( pkg , filelists [ index ] || [ ] ) ; } ) ; datadog . timing ( 'pkgTypes.getModuleTypesForAll' , Date . now ( ) - start ) ; return all ; }