@@ -4,31 +4,57 @@ const { resolvePath } = require('./commons');
4
4
5
5
const requirePath = path . join ( __dirname , process . env . BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs' )
6
6
7
+ const logDebug = process . env . OPENCV4NODES_DEBUG_REQUIRE ? require ( 'npmlog' ) . info : ( ) => { }
8
+
7
9
function tryGetOpencvBinDir ( ) {
8
- // if the auto build is disabled via environment do not even attempt
10
+ if ( process . env . OPENCV_BIN_DIR ) {
11
+ logDebug ( 'tryGetOpencvBinDir' , 'OPENCV_BIN_DIR environment variable is set' )
12
+ return process . env . OPENCV_BIN_DIR
13
+ }
14
+ // if the auto build is not disabled via environment do not even attempt
9
15
// to read package.json
10
- if ( opencvBuild . isAutoBuildDisabled ( ) ) {
16
+ if ( ! opencvBuild . isAutoBuildDisabled ( ) ) {
17
+ logDebug ( 'tryGetOpencvBinDir' , 'auto build has not been disabled via environment variable, using opencv bin dir of opencv-build' )
11
18
return opencvBuild . opencvBinDir
12
19
}
13
20
21
+ logDebug ( 'tryGetOpencvBinDir' , 'auto build has not been explicitly disabled via environment variable, attempting to read envs from package.json...' )
14
22
const envs = opencvBuild . readEnvsFromPackageJson ( )
15
- return envs . disableAutoBuild
16
- ? ( envs . opencvBinDir || process . env . OPENCV_BIN_DIR )
17
- : opencvBuild . opencvBinDir
23
+
24
+ if ( ! envs . disableAutoBuild ) {
25
+ logDebug ( 'tryGetOpencvBinDir' , 'auto build has not been disabled via package.json, using opencv bin dir of opencv-build' )
26
+ return opencvBuild . opencvBinDir
27
+ }
28
+
29
+ if ( envs . opencvBinDir ) {
30
+ logDebug ( 'tryGetOpencvBinDir' , 'found opencv binary environment variable in package.json' )
31
+ return envs . opencvBinDir
32
+ }
33
+ logDebug ( 'tryGetOpencvBinDir' , 'failed to find opencv binary environment variable in package.json' )
34
+ return null
18
35
}
19
36
20
37
let cv = null
21
38
try {
39
+ logDebug ( 'require' , 'require path is ' + requirePath )
22
40
cv = require ( requirePath ) ;
23
41
} catch ( err ) {
42
+ logDebug ( 'require' , 'failed to require cv with exception: ' + err . toString ( ) )
43
+ logDebug ( 'require' , 'attempting to add opencv binaries to path' )
44
+
45
+ if ( ! process . env . path ) {
46
+ logDebug ( 'require' , 'there is no path environment variable, skipping...' )
47
+ throw err
48
+ }
24
49
25
- // TODO: non windows?
26
50
const opencvBinDir = tryGetOpencvBinDir ( )
51
+ logDebug ( 'require' , 'adding opencv binary dir to path: ' + opencvBinDir )
27
52
28
53
// ensure binaries are added to path on windows
29
54
if ( ! process . env . path . includes ( opencvBinDir ) ) {
30
55
process . env . path = `${ process . env . path } ;${ opencvBinDir } ;`
31
56
}
57
+ logDebug ( 'require' , 'process.env.path: ' + process . env . path )
32
58
cv = require ( requirePath ) ;
33
59
}
34
60
0 commit comments