Total Complexity | 6 |
Complexity/F | 1.2 |
Lines of Code | 37 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | const { Module } = require('module'); |
||
2 | const path = require('path'); |
||
3 | |||
4 | function clearRequireCache() { |
||
5 | for (const key of Object.keys(require.cache)) { |
||
6 | delete require.cache[key]; |
||
7 | } |
||
8 | } |
||
9 | |||
10 | function isPathInside(childPath, parentPath) { |
||
11 | const relation = path.relative(parentPath, childPath); |
||
12 | |||
13 | return Boolean( |
||
14 | relation && |
||
15 | relation !== '..' && |
||
16 | !relation.startsWith(`..${path.sep}`) && |
||
17 | relation !== path.resolve(childPath) |
||
18 | ); |
||
19 | } |
||
20 | |||
21 | const ROOT_FOLDER = process.cwd(); |
||
22 | |||
23 | function preventParentScopeModules() { |
||
24 | const nodeModulePaths = Module._nodeModulePaths; |
||
25 | |||
26 | Module._nodeModulePaths = function (from) { |
||
27 | const originalPath = nodeModulePaths.call(this, from); |
||
28 | |||
29 | |||
30 | return originalPath.filter(function (p) { |
||
31 | return isPathInside(p, ROOT_FOLDER); |
||
32 | }); |
||
33 | }; |
||
34 | } |
||
35 | |||
36 | clearRequireCache(); |
||
37 | preventParentScopeModules(); |
||
38 |