From 95de28fbe41f7ed6d649ba9dc616fdad8b77c5bf Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Sat, 12 Dec 2020 05:45:57 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#49583=20feat(bindi?= =?UTF-8?q?ngs):=20Update=C2=A0to=C2=A0v1.5=20by=20@ExE-Boss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/bindings/bindings-tests.ts | 39 +++++++++++++++++++++++++++-- types/bindings/index.d.ts | 43 ++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/types/bindings/bindings-tests.ts b/types/bindings/bindings-tests.ts index e66ab51353..1fdb82273e 100644 --- a/types/bindings/bindings-tests.ts +++ b/types/bindings/bindings-tests.ts @@ -1,4 +1,39 @@ import bindings = require('bindings'); -// Use your bindings defined in your C files -const result = bindings('binding.node').your_c_function(); +bindings('binding.node'); +bindings({ + arrow: process.env.NODE_BINDINGS_ARROW || ' → ', + compiled: process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled', + platform: process.platform, + arch: process.arch, + nodePreGyp: `node-v${process.versions.modules}-${process.platform}-${process.arch}`, + version: process.versions.node, + bindings: 'bindings.node', + try: [ + // node-gyp's linked version in the "build" dir + ['module_root', 'build', 'bindings'], + // node-waf and gyp_addon (a.k.a node-gyp) + ['module_root', 'build', 'Debug', 'bindings'], + ['module_root', 'build', 'Release', 'bindings'], + // Debug files, for development (legacy behavior, remove for node v0.9) + ['module_root', 'out', 'Debug', 'bindings'], + ['module_root', 'Debug', 'bindings'], + // Release files, but manually compiled (legacy behavior, remove for node v0.9) + ['module_root', 'out', 'Release', 'bindings'], + ['module_root', 'Release', 'bindings'], + // Legacy from node-waf, node <= 0.4.x + ['module_root', 'build', 'default', 'bindings'], + // Production "Release" buildtype binary (meh...) + ['module_root', 'compiled', 'version', 'platform', 'arch', 'bindings'], + // node-qbs builds + ['module_root', 'addon-build', 'release', 'install-root', 'bindings'], + ['module_root', 'addon-build', 'debug', 'install-root', 'bindings'], + ['module_root', 'addon-build', 'default', 'install-root', 'bindings'], + // node-pre-gyp path ./lib/binding/{node_abi}-{platform}-{arch} + ['module_root', 'lib', 'binding', 'nodePreGyp', 'bindings'], + ], +}); + +bindings.getFileName(); // $ExpectType string +bindings.getFileName('/node_modules/foo/index.js'); // $ExpectType string +bindings.getRoot(process.cwd()); // $ExpectType string diff --git a/types/bindings/index.d.ts b/types/bindings/index.d.ts index 9403a7a92b..4a06190305 100644 --- a/types/bindings/index.d.ts +++ b/types/bindings/index.d.ts @@ -1,13 +1,52 @@ -// Type definitions for bindings 1.3 +// Type definitions for bindings 1.5 // Project: https://github.com/TooTallNate/node-bindings // Definitions by: Daniel Perez Alvarez +// ExE Boss // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + /** * The main `bindings()` function loads the compiled bindings for a given module. * It uses V8's Error API to determine the parent filename that this function is * being invoked from, which is then used to find the root directory. */ -declare function bindings(mod: string): any; +declare function bindings(mod: string | bindings.Options): any; +declare namespace bindings { + interface Options { + /** @default process.env.NODE_BINDINGS_ARROW || ' → ' */ + arrow?: string; + /** @default process.env.NODE_BINDINGS_COMPILED_DIR || 'compiled' */ + compiled?: string; + /** @default process.platform */ + platform?: NodeJS.Platform; + /** @default process.arch */ + arch?: string; + /** @default `node-v${process.versions.modules}-${process.platform}-${process.arch}` */ + nodePreGyp?: string; + /** @default process.versions.node */ + version?: string; + /** @default 'bindings.node' */ + bindings?: string; + try?: ReadonlyArray>; + } + + /** + * Gets the filename of the JavaScript file that invokes this function. + * Used to help find the root directory of a module. + * Optionally accepts an filename argument to skip when searching for the invoking filename + */ + function getFileName(calling_file?: string): string; + + /** + * Gets the root directory of a module, given an arbitrary filename + * somewhere in the module tree. The "root directory" is the directory + * containing the `package.json` file. + * + * In: /home/nate/node-native-module/lib/index.js + * Out: /home/nate/node-native-module + */ + function getRoot(file: string): string; +} export = bindings;