All files / src/functions/execute execute.on.files.recursively.ts

100% Statements 18/18
100% Branches 0/0
100% Functions 6/6
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37    6x 6x       6x 5x     5x 5x 5x 5x               5x 5x     5x 4x 4x       5x     6x  
'use strict';
 
import * as fs from 'fs';
import * as path from 'path';
 
import {ItemStat} from '@owservable/folders';
 
const executeOnFilesRecursively = (folder: string, execute: Function): void => {
	const children: string[] = fs.readdirSync(folder);
 
	// PERFORMANCE: Single lstat call per item instead of two
	const itemStats: ItemStat[] = children.map((name: string): ItemStat => {
		const fullPath: string = path.join(folder, name);
		const stat: fs.Stats = fs.lstatSync(fullPath);
		return {
			name,
			fullPath,
			isDirectory: stat.isDirectory()
		};
	});
 
	// Separate files and folders using cached stat results
	const files: ItemStat[] = itemStats.filter((item: ItemStat): boolean => !item.isDirectory);
	const folders: ItemStat[] = itemStats.filter((item: ItemStat): boolean => item.isDirectory);
 
	// Process files using native forEach
	files.forEach((file: ItemStat): void => {
		const obj: any = require(file.fullPath).default;
		execute(obj);
	});
 
	// Process subfolders recursively using native forEach
	folders.forEach((subFolder: ItemStat): void => executeOnFilesRecursively(subFolder.fullPath, execute));
};
 
export default executeOnFilesRecursively;