1 | /* eslint-disable import/no-commonjs */ |
||
2 | const { Module } = require('module'); |
||
3 | const path = require('path'); |
||
4 | const readline = require('readline'); |
||
5 | const dotenv = require('dotenv'); |
||
6 | |||
7 | function clearRequireCache() { |
||
8 | for (const key of Object.keys(require.cache)) { |
||
9 | delete require.cache[key]; |
||
10 | } |
||
11 | } |
||
12 | |||
13 | function isPathInside(childPath, parentPath) { |
||
14 | const relation = path.relative(parentPath, childPath); |
||
15 | |||
16 | return Boolean( |
||
17 | relation && |
||
18 | relation !== '..' && |
||
19 | !relation.startsWith(`..${path.sep}`) && |
||
20 | relation !== path.resolve(childPath) |
||
21 | ); |
||
22 | } |
||
23 | |||
24 | const ROOT_FOLDER = process.cwd(); |
||
25 | |||
26 | function preventParentScopeModules() { |
||
27 | const nodeModulePaths = Module._nodeModulePaths; |
||
28 | |||
29 | Module._nodeModulePaths = function (from) { |
||
30 | const originalPath = nodeModulePaths.call(this, from); |
||
31 | |||
32 | |||
33 | return originalPath.filter(function (p) { |
||
34 | return isPathInside(p, ROOT_FOLDER); |
||
35 | }); |
||
36 | }; |
||
37 | } |
||
38 | |||
39 | function loadEnv() { |
||
40 | dotenv.config({ |
||
41 | path : path.join(__dirname, './test.env') |
||
42 | }); |
||
43 | } |
||
44 | |||
45 | if (process.platform === 'win32') { |
||
46 | const rl = readline.createInterface({ |
||
47 | input : process.stdin, |
||
48 | output : process.stdout |
||
49 | }); |
||
50 | |||
51 | rl.on('SIGINT', function () { |
||
52 | console.log('readline SIGINT catched'); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
53 | |||
54 | process.emit('SIGINT'); |
||
55 | }); |
||
56 | } |
||
57 | |||
58 | [ 'SIGINT', 'exit' ].forEach(signal => { |
||
59 | process.on(signal, function (code) { |
||
60 | console.log(`${signal} catched, code:`, code); |
||
0 ignored issues
–
show
|
|||
61 | // eslint-disable-next-line no-process-exit |
||
62 | process.exit(code); |
||
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
|
|||
63 | }); |
||
64 | }); |
||
65 | |||
66 | clearRequireCache(); |
||
67 | preventParentScopeModules(); |
||
68 | loadEnv(); |
||
69 |