-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.js
56 lines (52 loc) · 2.14 KB
/
common.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const fs = require('fs')
const path = require('path')
// Base name of the DXL configuration file used by the SDK samples in
// establishing a client connection
const CONFIG_FILE_NAME = 'dxlclient.config'
// Value in the 'name' field in the SDK package's 'package.json' file
const SDK_PACKAGE_NAME = '@opendxl/dxl-client'
module.exports = {
/**
* Location of the DXL configuration file used by the SDK samples in
* establishing a client connection.
*/
CONFIG_FILE: path.join(__dirname, CONFIG_FILE_NAME),
/**
* Load a module, adjusting for an alternate location when running from an
* SDK sample.
*
* This function adjusts for differences in the module path when
* running a sample from a repository source checkout vs. an installed
* release package and for flat (NPM version 2) vs. nested (NPM version 3
* and later) dependency installation.
*
* This function should only be needed for running the SDK samples. For
* code which references the SDK module as a dependency via an NPM
* `package.json` file, the built-in `require` can be used directly
* rather than this wrapper function.
*
* @param {String} module - Name of the module to load
* @returns The result from the underlying call to the built-in `require`
* function.
*/
require: function (module) {
if (module === SDK_PACKAGE_NAME) {
const packageFile = path.join(__dirname, '..', 'package.json')
if (fs.existsSync(packageFile)) {
const packageInfo = JSON.parse(fs.readFileSync(packageFile))
if (packageInfo.name === SDK_PACKAGE_NAME) {
// Use local library sources if the example is being run from source.
module = '..'
}
}
} else if (fs.existsSync(path.join(__dirname, '..',
'node_modules', SDK_PACKAGE_NAME, 'node_modules', module))) {
// Prior to NPM version 3, an 'npm install' would nest a package's
// dependencies under a 'node_modules' subdirectory. Adjust the module
// name to reflect the nested location if found there.
module = '../node_modules/' + SDK_PACKAGE_NAME + '/node_modules/' + module
}
return require(module)
}
}