@@ -20,10 +20,18 @@ const binaryData = JSON.parse(
2020
2121async function analyse ( path ?: string , opts ?: T . Options ) : Promise < T . Results >
2222async function analyse ( paths ?: string [ ] , opts ?: T . Options ) : Promise < T . Results >
23- async function analyse ( rawPaths ?: string | string [ ] , opts : T . Options = { } ) : Promise < T . Results > {
24- const useRawContent = opts . fileContent !== undefined ;
25- const input = [ rawPaths ?? [ ] ] . flat ( ) ;
26- const manualFileContent = [ opts . fileContent ?? [ ] ] . flat ( ) ;
23+ async function analyse ( content ?: Record < string , string > , opts ?: T . Options ) : Promise < T . Results >
24+ async function analyse ( rawInput ?: string | string [ ] | Record < string , string > , opts : T . Options = { } ) : Promise < T . Results > {
25+ const inputs = {
26+ path : typeof rawInput === 'string' ? rawInput : null ,
27+ paths : Array . isArray ( rawInput ) ? rawInput : null ,
28+ content : typeof rawInput === 'object' && ! Array . isArray ( rawInput ) ? rawInput : null ,
29+ } ;
30+ const inputPaths = inputs . paths ?? ( inputs . path ? [ inputs . path ] : null ) ;
31+ const inputContent = inputs . content ;
32+ const useRawContent = inputContent !== null ;
33+
34+ const input = useRawContent ? Object . keys ( inputContent ) : inputPaths ?? [ ] ;
2735
2836 // Normalise input option arguments
2937 opts = {
@@ -233,7 +241,7 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
233241 // Check first line for readability
234242 let firstLine : string | null ;
235243 if ( useRawContent ) {
236- firstLine = manualFileContent [ files . indexOf ( file ) ] ?. split ( '\n' ) [ 0 ] ?? null ;
244+ firstLine = inputContent [ file ] ?. split ( '\n' ) [ 0 ] ?? null ;
237245 }
238246 else if ( FS . existsSync ( file ) && ! FS . lstatSync ( file ) . isDirectory ( ) ) {
239247 firstLine = await readFileChunk ( file , true ) . catch ( ( ) => null ) ;
@@ -353,7 +361,7 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
353361 }
354362
355363 // Check file contents and apply heuristic patterns
356- const fileContent = opts . fileContent ? manualFileContent [ files . indexOf ( file ) ] : await readFileChunk ( file ) . catch ( ( ) => null ) ;
364+ const fileContent = useRawContent ? inputContent [ file ] : await readFileChunk ( file ) . catch ( ( ) => null ) ;
357365
358366 // Skip if file read errors
359367 if ( fileContent === null ) continue ;
@@ -423,11 +431,11 @@ async function analyse(rawPaths?: string | string[], opts: T.Options = {}): Prom
423431 for ( const [ file , lang ] of Object . entries ( results . files . results ) ) {
424432 if ( lang && ! langData [ lang ] ) continue ;
425433 // Calculate file size
426- const fileSize = manualFileContent [ files . indexOf ( file ) ] ?. length ?? FS . statSync ( file ) . size ;
434+ const fileSize = useRawContent ? inputContent [ file ] ?. length : FS . statSync ( file ) . size ;
427435 // Calculate lines of code
428436 const loc = { total : 0 , content : 0 } ;
429437 if ( opts . calculateLines ) {
430- const fileContent = ( manualFileContent [ files . indexOf ( file ) ] ?? FS . readFileSync ( file ) . toString ( ) ) ?? '' ;
438+ const fileContent = useRawContent ? inputContent [ file ] : FS . readFileSync ( file ) . toString ( ) ;
431439 const allLines = fileContent . split ( / \r ? \n / gm) ;
432440 loc . total = allLines . length ;
433441 loc . content = allLines . filter ( line => line . trim ( ) . length > 0 ) . length ;
0 commit comments