diff --git a/README.md b/README.md index 15645db..93d698d 100644 --- a/README.md +++ b/README.md @@ -12,30 +12,105 @@ This is a part of [Node3D](https://github.com/node-3d) project. ## Synopsis -Helpers for Node.js **NAPI** addons and dependency packages. +This module contains numerous helpers for Node.js **NAPI** +addons and dependency packages. In this file, helper scripts +are described. For details on **addon-tools.hpp** and some +additional snippets follow the links below. **Go to**: -[include/addon-tools.hpp](doc/addon-tools.md) -Macro shortcuts for NAPI. -[Es5 Class Wrapping](doc/class-wrapping.md) +* [include/addon-tools.hpp](doc/addon-tools.md) +Macro shortcuts for C++ addons using NAPI. +* [Es5 Class Wrapping](doc/class-wrapping.md) An alternative, lightweight native class-defining mechanism for addons. -* [Script Helpers](doc/script-helpers.md) -Additional scripts for addon management. * [Snippets](doc/snippets.md) Some repetitive bits of code for addons. ## index.js -Main exports for cross-platform addon configuration. Exports: +Main exports for cross-platform addon configuration. * `paths(dir)` - function. Returns a set of platform dependent paths depending on input `dir`. * `bin` - platform binary directory absolute path. * `include` - include directory for this `dir`. * `include` - both `'addon-tools-raub'` and `'node-addon-api'` include paths. -Use with `node -p` through list context command expansion ` useData(data), + err => emit('error', err) +); +// or +const data = await download(srcUrl); +useData(data); +``` + + +## cpbin.js + +Copies the addon binary from **src/build/Release** to the platform folder. +It is useful for development builds. Use it in your **src/package.json**: +``` + "scripts": { + "build": "node-gyp rebuild && node -e \"require('addon-tools-raub/cpbin')('ADDON')\"" + }, +``` +Here ADDON should be replaced with the name of your addon, without `.node` extension. + + +## install.js + +Downloads and unzips the platform specific binary for the calling package. +To use it, create a new script for your package, which may as well be named +**install.js**, with the following content: + +``` +'use strict'; +const install = require('addon-tools-raub/install'); +const prefix = 'https://github.com/USER/ADDON-NAME/releases/download'; +const tag = process.env.npm_package_config_install; +install(`${prefix}/${tag}`); +``` + +`prefix` - the constant base part of the download url. +`tag` - the version-dependent part of the url, +here `process.env.npm_package_config_install` is taken +([automatically](https://docs.npmjs.com/misc/config#per-package-config-settings)) +from **package.json**: + +``` + "config": { + "install": "v2.0.0" + }, + "scripts": { + "postinstall": "node install" + }, +``` + + +## writable-buffer.js + +A [Writable](https://nodejs.org/api/stream.html#stream_writable_streams) +stream buffer, that is stored in-memory and can be fully +obtained when writing was finished. It is equivalent to stream-writing +a temporary file and then reading it into a Buffer. + +Use `stream.get()` to obtain the data when writing was finished: +``` +const stream = new WritableBuffer(); +// ... +sourceStream.pipe(stream); +sourceStream.on('end', () => useData(stream.get())); +``` diff --git a/doc/class-wrapping.md b/doc/class-wrapping.md index c9a368a..dd60f16 100644 --- a/doc/class-wrapping.md +++ b/doc/class-wrapping.md @@ -1 +1,37 @@ # Es5 class wrapping + +## Class declaration + +``` +class ClassName { +DECLARE_ES5_CLASS(ClassName, JSClassName); + +public: + static void init(Napi::Env env, Napi::Object exports); + explicit ClassName(const Napi::CallbackInfo& info); + ~ClassName(); + + void _destroy(); + + private: + JS_DECLARE_GETTER(ClassName, isDestroyed); + + JS_DECLARE_METHOD(ClassName, ClassName, destroy); + + bool _isDestroyed; + +}; +``` + +`DECLARE_ES5_CLASS` - adds utility declarations, the first argument +must be this class name, and the second argument will become the +name (arbitrary) of this function (constructor) in JS. + +`init` - can be used to initialize this class and export it. + +`JS_DECLARE_METHOD` - declares a method, the first argument is this class, +the second is the name of the method to be created. +`JS_DECLARE_GETTER` - declares a getter, the first argument is this class, +the second is the name of the getter to be created. +`JS_DECLARE_SETTER` - declares a setter, the first argument is this class, +the second is the name of the setter to be created. diff --git a/doc/script-helpers.md b/doc/script-helpers.md deleted file mode 100644 index 5b755c2..0000000 --- a/doc/script-helpers.md +++ /dev/null @@ -1 +0,0 @@ -# Script Helpers diff --git a/include/addon-tools.hpp b/include/addon-tools.hpp index ebdca20..bc29d95 100644 --- a/include/addon-tools.hpp +++ b/include/addon-tools.hpp @@ -233,9 +233,6 @@ #define JS_METHOD(NAME) Napi::Value NAME(const Napi::CallbackInfo &info) -#define JS_GETTER(NAME) Napi::Value NAME(const Napi::CallbackInfo &info) -#define JS_SETTER(NAME) \ - void NAME(const Napi::CallbackInfo &info, const Napi::Value &value) #define ACCESSOR_RW(CLASS, NAME) \ InstanceAccessor(#NAME, &CLASS::NAME ## Getter, &CLASS::NAME ## Setter) diff --git a/index.js b/index.js index bd2dc7c..72a7b05 100644 --- a/index.js +++ b/index.js @@ -37,11 +37,6 @@ const paths = dir => { }; -const mkdirPath = isWindows ? `${rootPath}/bat/mkdir.bat` : 'mkdir'; -const rmPath = isWindows ? `${rootPath}/bat/rm.bat` : 'rm'; -const cpPath = isWindows ? `${rootPath}/bat/cp.bat` : 'cp'; - - module.exports = { paths, @@ -50,8 +45,4 @@ module.exports = { platform : platformName, include : includePath, - mkdir : mkdirPath, - rm : rmPath, - cp : cpPath, - }; diff --git a/package.json b/package.json index 5c70493..bdd3403 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ "README.md" ], "engines": { - "node": ">=10.16.1", - "npm": ">=6.9.0" + "node": ">=12.13.0", + "npm": ">=6.12.0" }, "repository": { "type": "git", @@ -39,6 +39,6 @@ }, "dependencies": { "node-addon-api": "1.7.1", - "unzipper": "0.10.1" + "unzipper": "0.10.5" } }