Total Complexity | 6 |
Complexity/F | 1 |
Lines of Code | 37 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |