@@ -33,7 +33,10 @@ let functionList = {},
33
33
ignoreFilesEndingWith = [ ".test.js" , ".test.ts" , ".test.tsx" ] ,
34
34
processExtensions = [ '.js' , '.ts' , '.tsx' ] ,
35
35
ignoreErrors = [ 'BABEL_PARSER_SYNTAX_ERROR' ] ,
36
- force
36
+ force ,
37
+ isFileToIgnore = ( fileName ) => {
38
+ return ignoreFilesEndingWith . some ( ending => fileName . endsWith ( ending ) )
39
+ }
37
40
;
38
41
39
42
function resolveFilePath ( filePath , extension ) {
@@ -182,9 +185,9 @@ async function createTests(filePath, funcToTest, processingFunction = 'getUnitTe
182
185
) )
183
186
) ;
184
187
185
- sortFolderTree ( ) ;
186
- const fileIndex = folderStructureTree . findIndex ( item => item . absolutePath === filePath ) ;
188
+ sortFolderTree ( folderStructureTree ) ;
187
189
190
+ const fileIndex = folderStructureTree . findIndex ( item => item . absolutePath === filePath ) ;
188
191
for ( const [ i , funcData ] of uniqueFoundFunctions . entries ( ) ) {
189
192
let indexToPush = fileIndex + 1 + i ;
190
193
let prefix = folderStructureTree [ fileIndex ] . line . split ( path . basename ( folderStructureTree [ fileIndex ] . absolutePath ) ) [ 0 ] ;
@@ -267,7 +270,7 @@ async function traverseDirectory(file, onlyCollectFunctionData, funcName, proces
267
270
const absolutePath = path . resolve ( file ) ;
268
271
const stat = fs . statSync ( absolutePath ) ;
269
272
270
- if ( ignoreFilesEndingWith . some ( ending => file . endsWith ( ending ) ) ) return ;
273
+ if ( ! stat . isDirectory ( ) && isFileToIgnore ( file ) ) return ;
271
274
272
275
if ( stat . isDirectory ( ) ) {
273
276
if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) return ;
@@ -285,7 +288,7 @@ async function traverseDirectory(file, onlyCollectFunctionData, funcName, proces
285
288
return ! ignoreFolders . includes ( baseName ) && ! baseName . startsWith ( '.' ) ;
286
289
} else {
287
290
const ext = path . extname ( f ) ;
288
- return processExtensions . includes ( ext ) && ! ignoreFilesEndingWith . some ( ending => f . endsWith ( ending ) ) ;
291
+ return processExtensions . includes ( ext ) && ! isFileToIgnore ( f ) ;
289
292
}
290
293
} )
291
294
. map ( f => path . join ( absolutePath , f ) ) ;
@@ -325,9 +328,9 @@ function updateFolderTree(absolutePath) {
325
328
}
326
329
}
327
330
328
- function sortFolderTree ( ) {
331
+ function sortFolderTree ( tree ) {
329
332
// 1. Sort the folderStructureTree
330
- folderStructureTree . sort ( ( a , b ) => {
333
+ tree . sort ( ( a , b ) => {
331
334
if ( a . absolutePath < b . absolutePath ) {
332
335
return - 1 ;
333
336
}
@@ -338,23 +341,29 @@ function sortFolderTree() {
338
341
} ) ;
339
342
340
343
// 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 ++ ) {
342
345
// Get the current directory path
343
- const currentDirPath = path . dirname ( folderStructureTree [ i ] . absolutePath ) ;
346
+ const currentDirPath = path . dirname ( tree [ i ] . absolutePath ) ;
344
347
// 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 ) {
346
349
// 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 ( "├───" , "└───" ) ;
348
351
}
349
352
}
350
353
}
351
354
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 ) ;
355
365
processedFiles = [ ] ;
356
- await traverseDirectory ( rootPath , true ) ;
357
- return functionList ;
366
+ return { functionList, folderStructureTree} ;
358
367
}
359
368
360
369
async function generateTestsForDirectory ( args , processingFunction = 'getUnitTests' ) {
@@ -390,10 +399,12 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
390
399
console . log ( `Tests are saved in the following directories:${ testsGenerated . reduce ( ( acc , item ) => acc + '\n' + blue + item , '' ) } ` ) ;
391
400
console . log ( `${ bold + green } ${ testsGenerated . length } unit tests generated!${ reset } ` ) ;
392
401
}
402
+
393
403
process . exit ( 0 ) ;
394
404
}
395
405
396
406
module . exports = {
397
407
getFunctionsForExport,
398
- generateTestsForDirectory
408
+ generateTestsForDirectory,
409
+ sortFolderTree
399
410
}
0 commit comments