Skip to content

Commit 98350ce

Browse files
committedJan 23, 2019
Prettier
1 parent 68dae57 commit 98350ce

File tree

1 file changed

+133
-102
lines changed

1 file changed

+133
-102
lines changed
 

‎bindings.js

+133-102
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,75 @@
1-
21
/**
32
* Module dependencies.
43
*/
54

6-
var fs = require('fs')
7-
, path = require('path')
8-
, fileURLToPath = require('file-uri-to-path')
9-
, join = path.join
10-
, dirname = path.dirname
11-
, exists = ((fs.accessSync && function (path) { try { fs.accessSync(path); } catch (e) { return false; } return true; })
12-
|| fs.existsSync || path.existsSync)
13-
, defaults = {
14-
arrow: process.env.NODE_BINDINGS_ARROW || ' → '
15-
, compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled'
16-
, platform: process.platform
17-
, arch: process.arch
18-
, nodePreGyp: 'node-v' + process.versions.modules + '-' + process.platform + '-' + process.arch
19-
, version: process.versions.node
20-
, bindings: 'bindings.node'
21-
, try: [
22-
// node-gyp's linked version in the "build" dir
23-
[ 'module_root', 'build', 'bindings' ]
24-
// node-waf and gyp_addon (a.k.a node-gyp)
25-
, [ 'module_root', 'build', 'Debug', 'bindings' ]
26-
, [ 'module_root', 'build', 'Release', 'bindings' ]
27-
// Debug files, for development (legacy behavior, remove for node v0.9)
28-
, [ 'module_root', 'out', 'Debug', 'bindings' ]
29-
, [ 'module_root', 'Debug', 'bindings' ]
30-
// Release files, but manually compiled (legacy behavior, remove for node v0.9)
31-
, [ 'module_root', 'out', 'Release', 'bindings' ]
32-
, [ 'module_root', 'Release', 'bindings' ]
33-
// Legacy from node-waf, node <= 0.4.x
34-
, [ 'module_root', 'build', 'default', 'bindings' ]
35-
// Production "Release" buildtype binary (meh...)
36-
, [ 'module_root', 'compiled', 'version', 'platform', 'arch', 'bindings' ]
37-
// node-qbs builds
38-
, [ 'module_root', 'addon-build', 'release', 'install-root', 'bindings' ]
39-
, [ 'module_root', 'addon-build', 'debug', 'install-root', 'bindings' ]
40-
, [ 'module_root', 'addon-build', 'default', 'install-root', 'bindings' ]
41-
// node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
42-
, [ 'module_root', 'lib', 'binding', 'nodePreGyp', 'bindings' ]
43-
]
44-
}
5+
var fs = require('fs'),
6+
path = require('path'),
7+
fileURLToPath = require('file-uri-to-path'),
8+
join = path.join,
9+
dirname = path.dirname,
10+
exists =
11+
(fs.accessSync &&
12+
function(path) {
13+
try {
14+
fs.accessSync(path);
15+
} catch (e) {
16+
return false;
17+
}
18+
return true;
19+
}) ||
20+
fs.existsSync ||
21+
path.existsSync,
22+
defaults = {
23+
arrow: process.env.NODE_BINDINGS_ARROW || ' → ',
24+
compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled',
25+
platform: process.platform,
26+
arch: process.arch,
27+
nodePreGyp:
28+
'node-v' +
29+
process.versions.modules +
30+
'-' +
31+
process.platform +
32+
'-' +
33+
process.arch,
34+
version: process.versions.node,
35+
bindings: 'bindings.node',
36+
try: [
37+
// node-gyp's linked version in the "build" dir
38+
['module_root', 'build', 'bindings'],
39+
// node-waf and gyp_addon (a.k.a node-gyp)
40+
['module_root', 'build', 'Debug', 'bindings'],
41+
['module_root', 'build', 'Release', 'bindings'],
42+
// Debug files, for development (legacy behavior, remove for node v0.9)
43+
['module_root', 'out', 'Debug', 'bindings'],
44+
['module_root', 'Debug', 'bindings'],
45+
// Release files, but manually compiled (legacy behavior, remove for node v0.9)
46+
['module_root', 'out', 'Release', 'bindings'],
47+
['module_root', 'Release', 'bindings'],
48+
// Legacy from node-waf, node <= 0.4.x
49+
['module_root', 'build', 'default', 'bindings'],
50+
// Production "Release" buildtype binary (meh...)
51+
['module_root', 'compiled', 'version', 'platform', 'arch', 'bindings'],
52+
// node-qbs builds
53+
['module_root', 'addon-build', 'release', 'install-root', 'bindings'],
54+
['module_root', 'addon-build', 'debug', 'install-root', 'bindings'],
55+
['module_root', 'addon-build', 'default', 'install-root', 'bindings'],
56+
// node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch}
57+
['module_root', 'lib', 'binding', 'nodePreGyp', 'bindings']
58+
]
59+
};
4560

4661
/**
4762
* The main `bindings()` function loads the compiled bindings for a given module.
4863
* It uses V8's Error API to determine the parent filename that this function is
4964
* being invoked from, which is then used to find the root directory.
5065
*/
5166

52-
function bindings (opts) {
53-
67+
function bindings(opts) {
5468
// Argument surgery
5569
if (typeof opts == 'string') {
56-
opts = { bindings: opts }
70+
opts = { bindings: opts };
5771
} else if (!opts) {
58-
opts = {}
72+
opts = {};
5973
}
6074

6175
// maps `defaults` onto `opts` object
@@ -65,95 +79,106 @@ function bindings (opts) {
6579

6680
// Get the module root
6781
if (!opts.module_root) {
68-
opts.module_root = exports.getRoot(exports.getFileName())
82+
opts.module_root = exports.getRoot(exports.getFileName());
6983
}
7084

7185
// Ensure the given bindings name ends with .node
7286
if (path.extname(opts.bindings) != '.node') {
73-
opts.bindings += '.node'
87+
opts.bindings += '.node';
7488
}
7589

7690
// https://github.com/webpack/webpack/issues/4175#issuecomment-342931035
77-
var requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
78-
79-
var tries = []
80-
, i = 0
81-
, l = opts.try.length
82-
, n
83-
, b
84-
, err
85-
86-
for (; i<l; i++) {
87-
n = join.apply(null, opts.try[i].map(function (p) {
88-
return opts[p] || p
89-
}))
90-
tries.push(n)
91+
var requireFunc =
92+
typeof __webpack_require__ === 'function'
93+
? __non_webpack_require__
94+
: require;
95+
96+
var tries = [],
97+
i = 0,
98+
l = opts.try.length,
99+
n,
100+
b,
101+
err;
102+
103+
for (; i < l; i++) {
104+
n = join.apply(
105+
null,
106+
opts.try[i].map(function(p) {
107+
return opts[p] || p;
108+
})
109+
);
110+
tries.push(n);
91111
try {
92-
b = opts.path ? requireFunc.resolve(n) : requireFunc(n)
112+
b = opts.path ? requireFunc.resolve(n) : requireFunc(n);
93113
if (!opts.path) {
94-
b.path = n
114+
b.path = n;
95115
}
96-
return b
116+
return b;
97117
} catch (e) {
98118
if (!/not find/i.test(e.message)) {
99-
throw e
119+
throw e;
100120
}
101121
}
102122
}
103123

104-
err = new Error('Could not locate the bindings file. Tried:\n'
105-
+ tries.map(function (a) { return opts.arrow + a }).join('\n'))
106-
err.tries = tries
107-
throw err
124+
err = new Error(
125+
'Could not locate the bindings file. Tried:\n' +
126+
tries
127+
.map(function(a) {
128+
return opts.arrow + a;
129+
})
130+
.join('\n')
131+
);
132+
err.tries = tries;
133+
throw err;
108134
}
109-
module.exports = exports = bindings
110-
135+
module.exports = exports = bindings;
111136

112137
/**
113138
* Gets the filename of the JavaScript file that invokes this function.
114139
* Used to help find the root directory of a module.
115140
* Optionally accepts an filename argument to skip when searching for the invoking filename
Has a conversation. Original line has a conversation.
116141
*/
117142

118-
exports.getFileName = function getFileName (calling_file) {
119-
var origPST = Error.prepareStackTrace
120-
, origSTL = Error.stackTraceLimit
121-
, dummy = {}
122-
, fileName
143+
exports.getFileName = function getFileName(calling_file) {
144+
var origPST = Error.prepareStackTrace,
145+
origSTL = Error.stackTraceLimit,
146+
dummy = {},
147+
fileName;
123148

124-
Error.stackTraceLimit = 10
149+
Error.stackTraceLimit = 10;
125150

126-
Error.prepareStackTrace = function (e, st) {
127-
for (var i=0, l=st.length; i<l; i++) {
128-
fileName = st[i].getFileName()
151+
Error.prepareStackTrace = function(e, st) {
152+
for (var i = 0, l = st.length; i < l; i++) {
153+
fileName = st[i].getFileName();
129154
if (fileName !== __filename) {
130155
if (calling_file) {
131-
if (fileName !== calling_file) {
132-
return
133-
}
156+
if (fileName !== calling_file) {
157+
return;
158+
}
134159
} else {
135-
return
160+
return;
136161
}
137162
}
138163
}
139-
}
164+
};
140165

141166
// run the 'prepareStackTrace' function above
142-
Error.captureStackTrace(dummy)
143-
dummy.stack
167+
Error.captureStackTrace(dummy);
168+
dummy.stack;
144169

145170
// cleanup
146-
Error.prepareStackTrace = origPST
147-
Error.stackTraceLimit = origSTL
171+
Error.prepareStackTrace = origPST;
172+
Error.stackTraceLimit = origSTL;
148173

149174
// handle filename that starts with "file://"
150-
var fileSchema = "file://";
175+
var fileSchema = 'file://';
151176
if (fileName.indexOf(fileSchema) === 0) {
152177
fileName = fileURLToPath(fileName);
153178
}
154179

155-
return fileName
156-
}
180+
return fileName;
181+
};
157182

158183
/**
159184
* Gets the root directory of a module, given an arbitrary filename
@@ -164,25 +189,31 @@ exports.getFileName = function getFileName (calling_file) {
164189
* Out: /home/nate/node-native-module
165190
*/
166191

167-
exports.getRoot = function getRoot (file) {
168-
var dir = dirname(file)
169-
, prev
192+
exports.getRoot = function getRoot(file) {
193+
var dir = dirname(file),
194+
prev;
170195
while (true) {
171196
if (dir === '.') {
172197
// Avoids an infinite loop in rare cases, like the REPL
173-
dir = process.cwd()
198+
dir = process.cwd();
174199
}
175-
if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {
200+
if (
201+
exists(join(dir, 'package.json')) ||
202+
exists(join(dir, 'node_modules'))
203+
) {
176204
// Found the 'package.json' file or 'node_modules' dir; we're done
177-
return dir
205+
return dir;
178206
}
179207
if (prev === dir) {
180208
// Got to the top
181-
throw new Error('Could not find module root given file: "' + file
182-
+ '". Do you have a `package.json` file? ')
209+
throw new Error(
210+
'Could not find module root given file: "' +
211+
file +
212+
'". Do you have a `package.json` file? '
213+
);
183214
}
184215
// Try the parent dir next
185-
prev = dir
186-
dir = join(dir, '..')
216+
prev = dir;
217+
dir = join(dir, '..');
187218
}
188-
}
219+
};

0 commit comments

Comments
 (0)
Please sign in to comment.