import path from 'path' ; import fs from 'fs' ; import globby from 'globby' ; import { Context , Diagnostic } from '../interfaces' ; import { getJSONPropertyPosition } from '../utils' ; param context returns export default ( context ) : Diagnostic [ ] => { const { pkg , typingsFile } = context ; const packageFiles = pkg . files ; if ( ! Array . isArray ( packageFiles ) ) { return [ ] ; } const normalizedTypingsFile = path . normalize ( typingsFile ) ; const patternProcessedPackageFiles = processGitIgnoreStylePatterns ( packageFiles ) ; const normalizedFiles = globby . sync ( patternProcessedPackageFiles , { cwd : context . cwd } ) . map ( path . normalize ) ; if ( normalizedFiles . includes ( normalizedTypingsFile ) ) { return [ ] ; } const packageJsonFullPath = path . join ( context . cwd , 'package.json' ) ; const content = fs . readFileSync ( packageJsonFullPath , 'utf8' ) ; return [ { fileName : packageJsonFullPath , message : `TypeScript type definition \`${ normalizedTypingsFile }\` is not part of the \`files\` list.` , severity : 'error' , ... getJSONPropertyPosition ( content , 'files' ) } ] ; } ; function processGitIgnoreStylePatterns ( patterns ) { const processedPatterns = patterns . map ( pattern => { const [ negatePatternMatch ] = /^!+/ . exec ( pattern ) ?? [ ] ; const negationMarkersCount = negatePatternMatch ? negatePatternMatch . length : 0 ; return [ pattern . slice ( negationMarkersCount ) . replace ( /^\/+/ , '' ) , negationMarkersCount % 2 === 0 ] as const ; } ) . filter ( ( [ , hasEvenCountOfNegationMarkers ] ) => hasEvenCountOfNegationMarkers ) . map ( ( [ processedPattern ] ) => processedPattern ) ; return [ ... new Set ( processedPatterns ) ] ; }