tests/init.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.17

Size

Lines of Code 42
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 25
mnd 1
bc 1
fnc 6
dl 0
loc 42
bpm 0.1666
cpm 1.1666
noi 0
c 0
b 0
f 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A init.js ➔ clearRequireCache 0 5 2
A init.js ➔ preventParentScopeModules 0 11 3
A init.js ➔ initEnv 0 3 1
A init.js ➔ isPathInside 0 10 1
1
/* eslint-disable import/no-commonjs */
2
const path = require('path');
3
const { Module } = require('module');
4
const dotenv = require('dotenv');
5
6
function clearRequireCache() {
7
    for (const key of Object.keys(require.cache)) {
8
        delete require.cache[key];
9
    }
10
}
11
12
function isPathInside(childPath, parentPath) {
13
    const relation = path.relative(parentPath, childPath);
14
15
    return Boolean(
16
        relation &&
17
		relation !== '..' &&
18
		!relation.startsWith(`..${path.sep}`) &&
19
		relation !== path.resolve(childPath)
20
    );
21
}
22
23
const ROOT_FOLDER = process.cwd();
24
25
function preventParentScopeModules() {
26
    const nodeModulePaths = Module._nodeModulePaths;
27
28
    Module._nodeModulePaths = function (from) {
29
        const originalPath = nodeModulePaths.call(this, from);
30
31
        return originalPath.filter(function (p) {
32
            return isPathInside(p, ROOT_FOLDER);
33
        });
34
    };
35
}
36
37
function initEnv() {
38
    dotenv.config({ path: path.join(__dirname, './test.env') });
39
}
40
41
clearRequireCache();
42
preventParentScopeModules();
43
initEnv();
44