Skip to content

Commit

Permalink
✅ Add example for npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Jan 13, 2025
1 parent 2cd032e commit 42e92dc
Show file tree
Hide file tree
Showing 19 changed files with 191 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.xmake/
/prebuilds/
*.tar.gz
/xmake-*/
Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ repos:
rev: 3.0.0
hooks:
- id: check-mailmap
# https://github.com/koalaman/shellcheck/issues/2909
- id: shellcheck
exclude_types:
- zsh
- repo: https://github.com/rhysd/actionlint
rev: v1.7.5
hooks:
Expand Down Expand Up @@ -78,6 +82,10 @@ repos:
rev: "20240903.09"
hooks:
- id: perltidy
- repo: https://github.com/scop/pre-commit-shfmt
rev: v3.10.0-2
hooks:
- id: shfmt
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
Expand All @@ -98,4 +106,5 @@ repos:

ci:
skip:
- shellcheck
- pyright
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ Compared to other solutions:

examples:

- [coc-rime](https://github.com/tonyfettes/coc-rime)
- [1_hello_world](examples/nodejs/1_hello_world)
- [coc-rime](https://github.com/tonyfettes/coc-rime): use
[pkg-prebuilds](https://github.com/julusian/pkg-prebuilds) to replace
[node-gyp-build](https://github.com/prebuild/node-gyp-build)

## [luarocks.org](https://luarocks.org/modules/Freed-Wu/xmake/)

Expand Down
17 changes: 17 additions & 0 deletions examples/nodejs/1_hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example

Modified from
<https://github.com/nodejs/node-addon-examples/blob/main/src/1-getting-started/1_hello_world/>

**NOTE**: Don't support nan because
<https://github.com/xmake-io/xmake-repo/issues/10#issuecomment-2587462047>
and node_version.h is missing.

## Example 1: *Hello world*

To get started let's make a small addon which is the C++ equivalent of
the following JavaScript code:

```js
module.exports.hello = function() { return 'world'; };
```
23 changes: 23 additions & 0 deletions examples/nodejs/1_hello_world/napi/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <assert.h>
#include <node_api.h>

static napi_value Method(napi_env env, napi_callback_info info) {
napi_status status;
napi_value world;
status = napi_create_string_utf8(env, "world", 5, &world);
assert(status == napi_ok);
return world;
}

#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }

static napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
3 changes: 3 additions & 0 deletions examples/nodejs/1_hello_world/napi/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
15 changes: 15 additions & 0 deletions examples/nodejs/1_hello_world/napi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "hello_world",
"version": "0.0.0",
"description": "Node.js Addons Example #1",
"main": "hello.js",
"private": true,
"dependencies": {
"xmake-build-system": "^2.9.7",
"bindings": "~1.5.0"
},
"scripts": {
"prepack": "xmake -y && xmake install",
"test": "node hello.js"
}
}
12 changes: 12 additions & 0 deletions examples/nodejs/1_hello_world/napi/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- luacheck: ignore 113 143
---@diagnostic disable: undefined-global
add_rules("mode.debug", "mode.release")

add_requires("node-api-headers")

target("hello")
do
add_rules("nodejs.module")
add_packages("node-api-headers")
add_files("*.c")
end
16 changes: 16 additions & 0 deletions examples/nodejs/1_hello_world/node-addon-api-addon-class/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <napi.h>

class HelloAddon : public Napi::Addon<HelloAddon> {
public:
HelloAddon(Napi::Env env, Napi::Object exports) {
DefineAddon(exports,
{InstanceMethod("hello", &HelloAddon::Hello, napi_enumerable)});
}

private:
Napi::Value Hello(const Napi::CallbackInfo& info) {
return Napi::String::New(info.Env(), "world");
}
};

NODE_API_ADDON(HelloAddon)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "hello_world",
"version": "0.0.0",
"description": "Node.js Addons Example #1",
"main": "hello.js",
"private": true,
"dependencies": {
"xmake-build-system": "^2.9.7",
"bindings": "~1.5.0"
},
"scripts": {
"prepack": "xmake -y && xmake install",
"test": "node hello.js"
}
}
12 changes: 12 additions & 0 deletions examples/nodejs/1_hello_world/node-addon-api-addon-class/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- luacheck: ignore 113 143
---@diagnostic disable: undefined-global
add_rules("mode.debug", "mode.release")

add_requires("node-addon-api")

target("hello")
do
add_rules("nodejs.module")
add_packages("node-addon-api")
add_files("*.cc")
end
14 changes: 14 additions & 0 deletions examples/nodejs/1_hello_world/node-addon-api/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <napi.h>

Napi::String Method(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::String::New(env, "world");
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "hello"),
Napi::Function::New(env, Method));
return exports;
}

NODE_API_MODULE(hello, Init)
3 changes: 3 additions & 0 deletions examples/nodejs/1_hello_world/node-addon-api/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
15 changes: 15 additions & 0 deletions examples/nodejs/1_hello_world/node-addon-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "hello_world",
"version": "0.0.0",
"description": "Node.js Addons Example #1",
"main": "hello.js",
"private": true,
"dependencies": {
"xmake-build-system": "^2.9.7",
"bindings": "~1.5.0"
},
"scripts": {
"prepack": "xmake -y && xmake install",
"test": "node hello.js"
}
}
12 changes: 12 additions & 0 deletions examples/nodejs/1_hello_world/node-addon-api/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- luacheck: ignore 113 143
---@diagnostic disable: undefined-global
add_rules("mode.debug", "mode.release")

add_requires("node-addon-api")

target("hello")
do
add_rules("nodejs.module")
add_packages("node-addon-api")
add_files("*.cc")
end
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"tar": "^7.4.3"
},
"scripts": {
"prepack": "node scripts/build.cjs"
"prepack": "node scripts/build.cjs",
"test": "scripts/test.sh"
}
}
11 changes: 11 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e
cd "$(dirname "$(dirname "$(readlink -f "$0")")")"

cd examples/nodejs/1_hello_world
for dir in ./*/; do
cd "$dir"
npm install
npm run prepack
npm test
done
4 changes: 4 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ mkShell {
]
))
];
# https://github.com/NixOS/nixpkgs/issues/314313#issuecomment-2134252094
shellHook = ''
LD="$CC"
'';
}

0 comments on commit 42e92dc

Please sign in to comment.