1 | 'use strict';
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 |
|
5 | var _ = require('lodash');
|
6 | var mothership = require('mothership');
|
7 |
|
8 | function trueIdentity () { return true; }
|
9 |
|
10 | function requireCMakeModule (name, conf) {
|
11 |
|
12 |
|
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 |
|
24 | conf = _.merge({}, requireCMakeModule.build, conf || {});
|
25 |
|
26 |
|
27 | if(!(conf.output instanceof Array)) conf.output = [conf.output];
|
28 | if(!(conf.config instanceof Array)) conf.config = [conf.config];
|
29 |
|
30 |
|
31 |
|
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 |
|
48 | return require(nativeModule);
|
49 | }
|
50 |
|
51 | requireCMakeModule.build = {
|
52 | output: [
|
53 | 'out', 'build'
|
54 | ],
|
55 | config: [
|
56 | 'MinSizeRel', 'Release', 'RelWithDebInfo', 'Debug'
|
57 | ]
|
58 | };
|
59 |
|
60 | module.exports = requireCMakeModule; |
\ | No newline at end of file |