forked from botpress/botpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.ts
38 lines (31 loc) · 987 Bytes
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Important:
*
* This file must be kept at the root of the src directory (and dist directory when built)
*/
import Module from 'module'
import pathlib from 'path'
const DEFAULT_DIRNAME = '.botpress'
const getOutDir = () => {
const { BP_OUTDIR } = process.env
if (!BP_OUTDIR) {
return pathlib.join(process.cwd(), DEFAULT_DIRNAME)
}
if (pathlib.isAbsolute(BP_OUTDIR)) {
return BP_OUTDIR
}
return pathlib.join(process.cwd(), BP_OUTDIR)
}
const outDirPath = getOutDir()
const outDirName = pathlib.basename(outDirPath)
const originalRequire = Module.prototype.require
const rewire = function (this: NodeRequire, mod: string) {
const importParts = mod.split('/')
if (importParts[0] === outDirName) {
const newMod = importParts.slice(1).join('/')
const fullpath = pathlib.join(outDirPath, newMod)
return originalRequire.apply(this, [fullpath])
}
return originalRequire.apply(this, [mod])
} as NodeRequire
Module.prototype.require = rewire