All files / src/config config-loader.ts

86.36% Statements 19/22
75% Branches 6/8
100% Functions 1/1
86.36% Lines 19/22
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 431x   1x 1x   1x   1x   512x 509x     3x       3x 3x   3x 18x   18x           18x 18x     18x 3x       3x 3x      
import { Config } from './config';
 
const path = require('path');
const fs = require('fs');
 
const cache: { [key: string]: Config } = {};
 
export class ConfigLoader {
	public fromTarget(filename: string): Config {
		if (filename === 'inline'){
			return Config.empty();
		}
 
		Iif (filename in cache){
			return cache[filename];
		}
 
		let current = path.resolve(path.dirname(filename));
		let config = Config.empty();
 
		for (;;){
			const search = path.join(current, '.htmlvalidate.json');
 
			Iif (fs.existsSync(search)){
				const local = Config.fromFile(search);
				config = local.merge(config);
			}
 
			/* get the parent directory */
			const child = current;
			current = path.dirname(current);
 
			/* stop if this is the root directory */
			if (current === child){
				break;
			}
		}
 
		cache[filename] = config;
		return config;
	}
}