| Total Complexity | 7 |
| Complexity/F | 1 |
| Lines of Code | 43 |
| Function Count | 7 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | /* eslint-disable import/no-commonjs */ |
||
| 2 | const { Module } = require('module'); |
||
| 3 | const path = require('path'); |
||
| 4 | const dotenv = require('dotenv'); |
||
| 5 | |||
| 6 | function clearRequireCache() { |
||
| 7 | Object.keys(require.cache).forEach((key) => { |
||
| 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 | const insideRootPaths = originalPath.filter(function (p) { |
||
| 31 | return isPathInside(p, ROOT_FOLDER); |
||
| 32 | }); |
||
| 33 | |||
| 34 | return insideRootPaths; |
||
| 35 | }; |
||
| 36 | } |
||
| 37 | |||
| 38 | function loadEnv() { |
||
| 39 | dotenv.config({ path: path.join(__dirname, './test.env') }); |
||
| 40 | } |
||
| 41 | |||
| 42 | clearRequireCache(); |
||
| 43 | preventParentScopeModules(); |
||
| 44 | loadEnv(); |
||
| 45 |