UNPKG

1.66 kBJavaScriptView Raw
1'use strict';
2var fs = require('fs');
3var path = require('path');
4
5var _ = require('lodash');
6var mothership = require('mothership');
7
8function trueIdentity () { return true; }
9
10function requireCMakeModule (name, conf) {
11 // Find the module root by finding the nearest package.json
12 // Use the path of the module that required this as the starting point
13 var base = path.dirname(module.parent.filename);
14 var json = mothership.sync(base, trueIdentity);
15 var root;
16 if(json) {
17 root = path.dirname(json.path);
18 }
19 else {
20 throw Error('Unable to find module root');
21 }
22
23 // Allow search path override
24 conf = _.merge({}, requireCMakeModule.build, conf || {});
25
26 // Allow single path definitions
27 if(!(conf.output instanceof Array)) conf.output = [conf.output];
28 if(!(conf.config instanceof Array)) conf.config = [conf.config];
29
30 // Search for the module in the specified locations
31 // Nest the searching so that things are tried in succession
32 var nativeModule;
33 if(!conf.output.some(function searchOutput (output) {
34 return conf.config.some(function searchConfig (config) {
35 nativeModule = path.join(
36 root, output, process.platform, process.arch, config, name + '.node'
37 );
38 try {
39 return fs.statSync(nativeModule);
40 }
41 catch(e) { return false; }
42 });
43 })) {
44 throw Error('Unable to find native module');
45 }
46
47 // Require the native module and return the loaded content
48 return require(nativeModule);
49}
50
51requireCMakeModule.build = {
52 output: [
53 'out', 'build'
54 ],
55 config: [
56 'MinSizeRel', 'Release', 'RelWithDebInfo', 'Debug'
57 ]
58};
59
60module.exports = requireCMakeModule;
\No newline at end of file