Skip to content

Commit 74918ab

Browse files
author
Carol Hansen
authored
Standalone function refactor (#44)
skel with standalone function and new structure
1 parent 59d0092 commit 74918ab

File tree

13 files changed

+130
-30
lines changed

13 files changed

+130
-30
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ clean:
2525
rm -rf lib/binding
2626
rm -rf build
2727
# remove remains from running 'make coverage'
28-
rm *.profraw
29-
rm *.profdata
28+
rm -f *.profraw
29+
rm -f *.profdata
3030
@echo "run 'make distclean' to also clear node_modules, mason_packages, and .mason directories"
3131

3232
distclean: clean

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ This repository itself can be cloned and edited to your needs.
2323
```
2424
git clone git@github.com:mapbox/node-cpp-skel.git
2525
cd node-cpp-skel
26-
make # build binaries
26+
27+
# Build binaries. This looks to see if there were changes in the C++ code. This does not reinstall deps.
28+
make
29+
30+
# Run tests
2731
make test
32+
33+
# Cleans your current builds and removes potential cache
34+
make clean
35+
36+
# Cleans everything, including the things you download from the network in order to compile (ex: npm packages).
37+
# This is useful if you want to nuke everything and start from scratch.
38+
# For example, it's super useful for making sure everything works for Travis, production, someone else's machine, etc
39+
make disclean
2840
```
2941

3042
Note: by default the build errors on compiler warnings. To disable this do:
@@ -33,6 +45,7 @@ Note: by default the build errors on compiler warnings. To disable this do:
3345
WERROR=false make
3446
```
3547

48+
To build from scratch
3649

3750
# Code coverage
3851

@@ -53,13 +66,30 @@ If you're developing on macOS and have Xcode installed, you can also type `make
5366

5467
# Usage
5568

69+
### Standalone sync function
5670
```javascript
57-
var HelloWorld = require('./path/to/lib/index.js');
58-
var HW = new HelloWorld();
71+
var module = require('./path/to/lib/index.js');
72+
var check = module.hello();
73+
74+
console.log(check); // => world
75+
```
76+
77+
### Object
78+
```javascript
79+
var module = require('./path/to/lib/index.js');
80+
var HW = new module.HelloWorld();
5981
var hi = HW.wave();
82+
6083
console.log(hi); // => howdy world!
6184
```
6285

86+
# Add Custom Code
87+
`node-cpp-skel` was designed to make adding custom code simple and scalable, to form to whatever use-case you may need. Here's how!
88+
89+
- Create a dir in `./src` to hold your custom code. See `./src/standalone` as an example.
90+
- Add your new method or class to `./src/module.cpp`
91+
- Add your new file-to-be-compiled to the list of target sources in `./binding.gyp`
92+
6393
# Publishing Binaries
6494

6595
It's a good idea to publish pre-built binaries of your module if you want others to be able to install it properly on their own systems, which can very likely not have a compiler like `gcc` or `clang`. Node-pre-gyp does a lot of the heavy lifting for us (like detecting which system you are building on and deploying to s3) but you'll need a few things configured to get started.

binding.gyp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@
2828
'target_name': '<(module_name)',
2929
'product_dir': '<(module_path)',
3030
'dependencies': [ 'action_before_build' ],
31-
'sources': [ './src/hello_world.cpp' ],
31+
# "make" only watches files specified here, and will sometimes cache these files after the first compile.
32+
# This cache can sometimes cause confusing errors when removing/renaming/adding new files.
33+
# Running "make clean" helps to prevent this "mysterious error by cache" scenario
34+
# This also is where the benefits of using a glob come into play...
35+
# See: https://github.com/mapbox/node-cpp-skel/pull/44#discussion_r122050205
36+
'sources': [
37+
'./src/module.cpp',
38+
'./src/standalone/hello.cpp',
39+
'./src/hello_world.cpp'
40+
],
3241
'include_dirs': [
3342
'<!(node -e \'require("nan")\')'
3443
],

lib/hello_world.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
module.exports = require('./hello_world.js');
1+
"use strict";
2+
3+
var MODULE = module.exports = require('./binding/module.node');
4+
MODULE.version = require('../package.json').version;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"tape": "^4.5.1"
2727
},
2828
"binary": {
29-
"module_name": "hello_world",
29+
"module_name": "module",
3030
"module_path": "./lib/binding/",
3131
"host": "https://mapbox-node-binary.s3.amazonaws.com",
3232
"remote_path": "./{name}/v{version}/{configuration}/{toolset}/",

src/hello_world.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,4 @@ NAN_MODULE_INIT(HelloWorld::Init)
274274
const auto fn = Nan::GetFunction(fnTp).ToLocalChecked();
275275
constructor().Reset(fn);
276276
Nan::Set(target, whoami, fn);
277-
}
278-
279-
/*
280-
* This creates the module, started up with NAN_MODULE_INIT.
281-
* The naming/casing of the first argument is reflected in lib/hello_world.js
282-
*/
283-
NODE_MODULE(HelloWorld, HelloWorld::Init);
277+
}

src/hello_world.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class HelloWorld: public Nan::ObjectWrap
3232
// constructor
3333
// This includes a Default Argument
3434
// If a paramter value is passed in, it takes priority over the default arg
35+
// HelloWorld --> class
3536
HelloWorld(std::string name="hello");
3637

3738
private:

src/module.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <nan.h>
2+
#include "standalone/hello.hpp"
3+
#include "hello_world.hpp"
4+
5+
// "target" is a magic var that nodejs passes into modules scope
6+
// When you write things to target, they become available to call from js
7+
static void init(v8::Local<v8::Object> target) {
8+
9+
// expose hello method
10+
Nan::SetMethod(target, "hello", standalone::hello);
11+
12+
// expose hello_world class
13+
HelloWorld::Init(target);
14+
15+
// add more methods below that youd like to use in node.js-world
16+
// then create a .cpp and .hpp file in /src for each new method
17+
}
18+
19+
NODE_MODULE(module, init)

src/standalone/hello.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "hello.hpp"
2+
3+
namespace standalone {
4+
5+
// "hello" is a Standalone function because it's not a class.
6+
// If this function was not defined within a namespace, it would be in the global scope.
7+
NAN_METHOD(hello) {
8+
9+
// "info" comes from the NAN_METHOD macro, which returns differently
10+
// according to the version of node
11+
info.GetReturnValue().Set(Nan::New<v8::String>("world").ToLocalChecked());
12+
13+
}
14+
}

0 commit comments

Comments
 (0)