All files / src/mongodb/functions process.models.ts

50% Statements 18/36
33.33% Branches 3/9
30% Functions 3/10
60.71% Lines 17/28

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 38 39 40 41 42 43 44 45    2x 2x   2x 2x   2x   2x 1x 1x 1x 1x     2x 3x 2x   1x     2x                             2x       2x  
'use strict';
 
import * as fs from 'fs';
import * as path from 'path';
 
import {each, endsWith, filter, find, isString} from 'lodash';
import {listSubfoldersByName} from '@owservable/folders';
 
import CollectionsModelsMap from '../collections.models.map';
 
const _processFile = (folder: string, file: string): void => {
	const fullPath = path.join(folder, file);
	const model = require(fullPath).default;
	Iif (!model) throw new Error(`Model not found in ${folder}/${file}`);
	CollectionsModelsMap.addCollectionToModelMapping(model);
};
 
const _isExcluded = (folder: string, exclude: string | string[]): boolean => {
	if (!exclude) return false;
	return isString(exclude) //
		? endsWith(folder, exclude) //
		: !!find(exclude, (e: string) => endsWith(folder, e));
};
 
const _processModels = (folder: string, exclude?: string | string[]): void => {
	Iif (_isExcluded(folder, exclude)) return;
 
	const subfolderNames = fs.readdirSync(folder);
	const files = filter(subfolderNames, (fileName: string) => !fs.lstatSync(path.join(folder, fileName)).isDirectory());
	each(files, (file: string) => {
		const ext = path.extname(file);
		Iif (ext !== '.ts' && ext !== '.js') return;
		_processFile(folder, file);
	});
 
	const folders = filter(subfolderNames, (fileName: string) => fs.lstatSync(path.join(folder, fileName)).isDirectory());
	each(folders, (sub: string) => _processModels(path.join(folder, sub), exclude));
};
 
const processModels = (root: string, name: string = 'models', exclude?: string | string[]): void => {
	const folders: string[] = listSubfoldersByName(root, name);
	each(folders, (folder: string) => _processModels(folder, exclude));
};
export default processModels;