@@ -33,7 +33,10 @@ let functionList = {},
3333 ignoreFilesEndingWith = [ ".test.js" , ".test.ts" , ".test.tsx" ] ,
3434 processExtensions = [ '.js' , '.ts' , '.tsx' ] ,
3535 ignoreErrors = [ 'BABEL_PARSER_SYNTAX_ERROR' ] ,
36- force
36+ force ,
37+ isFileToIgnore = ( fileName ) => {
38+ return ignoreFilesEndingWith . some ( ending => fileName . endsWith ( ending ) )
39+ }
3740;
3841
3942function resolveFilePath ( filePath , extension ) {
@@ -182,9 +185,9 @@ async function createTests(filePath, funcToTest, processingFunction = 'getUnitTe
182185 ) )
183186 ) ;
184187
185- sortFolderTree ( ) ;
186- const fileIndex = folderStructureTree . findIndex ( item => item . absolutePath === filePath ) ;
188+ sortFolderTree ( folderStructureTree ) ;
187189
190+ const fileIndex = folderStructureTree . findIndex ( item => item . absolutePath === filePath ) ;
188191 for ( const [ i , funcData ] of uniqueFoundFunctions . entries ( ) ) {
189192 let indexToPush = fileIndex + 1 + i ;
190193 let prefix = folderStructureTree [ fileIndex ] . line . split ( path . basename ( folderStructureTree [ fileIndex ] . absolutePath ) ) [ 0 ] ;
@@ -267,7 +270,7 @@ async function traverseDirectory(file, onlyCollectFunctionData, funcName, proces
267270 const absolutePath = path . resolve ( file ) ;
268271 const stat = fs . statSync ( absolutePath ) ;
269272
270- if ( ignoreFilesEndingWith . some ( ending => file . endsWith ( ending ) ) ) return ;
273+ if ( ! stat . isDirectory ( ) && isFileToIgnore ( file ) ) return ;
271274
272275 if ( stat . isDirectory ( ) ) {
273276 if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) return ;
@@ -285,7 +288,7 @@ async function traverseDirectory(file, onlyCollectFunctionData, funcName, proces
285288 return ! ignoreFolders . includes ( baseName ) && ! baseName . startsWith ( '.' ) ;
286289 } else {
287290 const ext = path . extname ( f ) ;
288- return processExtensions . includes ( ext ) && ! ignoreFilesEndingWith . some ( ending => f . endsWith ( ending ) ) ;
291+ return processExtensions . includes ( ext ) && ! isFileToIgnore ( f ) ;
289292 }
290293 } )
291294 . map ( f => path . join ( absolutePath , f ) ) ;
@@ -325,9 +328,9 @@ function updateFolderTree(absolutePath) {
325328 }
326329}
327330
328- function sortFolderTree ( ) {
331+ function sortFolderTree ( tree ) {
329332 // 1. Sort the folderStructureTree
330- folderStructureTree . sort ( ( a , b ) => {
333+ tree . sort ( ( a , b ) => {
331334 if ( a . absolutePath < b . absolutePath ) {
332335 return - 1 ;
333336 }
@@ -338,23 +341,29 @@ function sortFolderTree() {
338341 } ) ;
339342
340343 // 2. Set prefix according to the position in the directory
341- for ( let i = 0 ; i < folderStructureTree . length ; i ++ ) {
344+ for ( let i = 0 ; i < tree . length ; i ++ ) {
342345 // Get the current directory path
343- const currentDirPath = path . dirname ( folderStructureTree [ i ] . absolutePath ) ;
346+ const currentDirPath = path . dirname ( tree [ i ] . absolutePath ) ;
344347 // Check if it's the last file in the directory
345- if ( i === folderStructureTree . length - 1 || path . dirname ( folderStructureTree [ i + 1 ] . absolutePath ) !== currentDirPath ) {
348+ if ( i === tree . length - 1 || path . dirname ( tree [ i + 1 ] . absolutePath ) !== currentDirPath ) {
346349 // Update the prefix for the last file in the directory
347- folderStructureTree [ i ] . line = folderStructureTree [ i ] . line . replace ( "├───" , "└───" ) ;
350+ tree [ i ] . line = tree [ i ] . line . replace ( "├───" , "└───" ) ;
348351 }
349352 }
350353}
351354
352- async function getFunctionsForExport ( dirPath ) {
353- rootPath = dirPath ;
354- await traverseDirectory ( rootPath , true ) ;
355+ async function getFunctionsForExport ( path , pythagoraRoot , ignoreFilesRewrite ) {
356+ if ( ignoreFilesRewrite ) {
357+ isFileToIgnore = ignoreFilesRewrite ;
358+ }
359+ queriedPath = path ;
360+ rootPath = pythagoraRoot ;
361+
362+ await traverseDirectory ( queriedPath , true ) ;
363+ processedFiles = [ ] ;
364+ await traverseDirectory ( queriedPath , true ) ;
355365 processedFiles = [ ] ;
356- await traverseDirectory ( rootPath , true ) ;
357- return functionList ;
366+ return { functionList, folderStructureTree} ;
358367}
359368
360369async function generateTestsForDirectory ( args , processingFunction = 'getUnitTests' ) {
@@ -390,10 +399,12 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
390399 console . log ( `Tests are saved in the following directories:${ testsGenerated . reduce ( ( acc , item ) => acc + '\n' + blue + item , '' ) } ` ) ;
391400 console . log ( `${ bold + green } ${ testsGenerated . length } unit tests generated!${ reset } ` ) ;
392401 }
402+
393403 process . exit ( 0 ) ;
394404}
395405
396406module . exports = {
397407 getFunctionsForExport,
398- generateTestsForDirectory
408+ generateTestsForDirectory,
409+ sortFolderTree
399410}
0 commit comments