Passed
Push — master ( 19fbeb...acc0f5 )
by Dmytro
01:54 queued 12s
created

init.js ➔ clearRequireCache   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 2
1
/* eslint-disable import/no-commonjs */
2
const { Module } = require('module');
3
const path = require('path');
4
5
function clearRequireCache() {
6
    Object.keys(require.cache).forEach((key) => {
7
        delete require.cache[key];
8
    });
9
}
10
11
function isPathInside(childPath, parentPath) {
12
    const relation = path.relative(parentPath, childPath);
13
14
    return Boolean(
15
        relation &&
16
		relation !== '..' &&
17
		!relation.startsWith(`..${path.sep}`) &&
18
		relation !== path.resolve(childPath)
19
    );
20
}
21
22
const ROOT_FOLDER = process.cwd();
23
24
function preventParentScopeModules() {
25
    const nodeModulePaths = Module._nodeModulePaths;
26
27
    Module._nodeModulePaths = function (from) {
28
        const originalPath = nodeModulePaths.call(this, from);
29
        const insideRootPaths = originalPath.filter(function (p) {
30
            return isPathInside(p, ROOT_FOLDER);
31
        });
32
33
        return insideRootPaths;
34
    };
35
}
36
37
clearRequireCache();
38
preventParentScopeModules();
39