# Snippets ## C++ Addon building **NAPI** addons are built separately from the installation, so we shouldn't put the file **binding.gyp** to the module root anymore. It is better to have a separate folder with a separate **package.json**, **binding.gyp** and the sources. A snippet for **src/package.json**: ``` { "name": "build", "version": "0.0.0", "private": true, "scripts": { "build": "node-gyp rebuild" "build-dev": "node-gyp build && node -e \"require('addon-tools-raub/cpbin')('ADDON')\"" "rebuild-dev": "node-gyp rebuild && node -e \"require('addon-tools-raub/cpbin')('ADDON')\"" }, "dependencies": { "addon-tools-raub": "6.2.0", "DEPS": "1.0.0" } } ``` * `ADDON` - the name of this addon (and subsequently of its binary). * `DEPS` - dependency package(s). ### Binary distribution In **package.json** use the `"postinstall"` script to download the libraries. For example the following structure might work. Note that **Addon Tools** will append any given URL with `/${platform}.zip` In **package.json**: ``` "scripts": { "postinstall": "node install", }, "dependencies": { "addon-tools-raub": "^6.2.0", "adm-zip": "^0.5.10" }, "devDependencies": { "node-addon-api": "^5.0.0" } ``` Create the **install.js** file: ``` 'use strict'; const install = require('addon-tools-raub/install'); const prefix = 'https://github.com/node-3d/glfw-raub/releases/download'; const tag = '4.8.0'; install(`${prefix}/${tag}`); ``` **Addon Tools** will unzip (using **adm-zip**) the downloaded file into the platform binary directory. E.g. on Windows it will be **bin-windows**. * For a dependency package: Place the following piece of code in the `index.js` without changes. Method `paths()` is described [here](../README.md). ``` module.exports = require('addon-tools-raub').paths(__dirname); ``` * For a compiled addon: Require it in your **index.js** from the platform-specific directory. ``` const { bin } = require('addon-tools-raub'); const core = require(`./${bin}/ADDON`); ``` Publishing binaries is done by attaching a zipped platform folder to the GitHub release. Zip file must NOT contain platform folder as a subfolder, but rather contain the final binaries. The tag of the release should be the same as in **install.js**. > NOTE: You can publish your binaries to anywhere, not necessarily GitHub. Just tweak **YOUR install.js** script as appropriate. The only limitation from **Addon Tools** is that it should be a zipped set of files/folders. ### GYP Variables ``` 'variables': { 'bin': ' See a snipped for src/binding.gyp here * Assume `DEPS` is the name of an Addon Tools compliant dependency module. * Assume `ADDON` is the name of this addon's resulting binary. * Assume C++ code goes to `cpp` subdirectory. ``` { 'variables': { 'bin' : '