Skip to content

Commit 5946ece

Browse files
authored
Merge pull request #8 from the-ress/add-ci
Add GH actions and bump version
2 parents 72598df + d62e92a commit 5946ece

File tree

6 files changed

+161
-11
lines changed

6 files changed

+161
-11
lines changed

.github/workflows/ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
windows:
7+
name: Windows
8+
runs-on: windows-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-node@v3
12+
with:
13+
node-version: 18
14+
- run: npm ci
15+
- run: npm test
16+
17+
linux:
18+
name: Linux
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v3
22+
- uses: actions/setup-node@v3
23+
with:
24+
node-version: 18
25+
- run: npm ci
26+
- run: npm test
27+
28+
macos:
29+
name: macOS
30+
runs-on: macos-latest
31+
steps:
32+
- uses: actions/checkout@v3
33+
- uses: actions/setup-node@v3
34+
with:
35+
node-version: 18
36+
- run: npm ci
37+
- run: npm test

.github/workflows/publish.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Publish to NPM
2+
3+
on:
4+
release:
5+
types: [released]
6+
7+
jobs:
8+
publish:
9+
name: Publish
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 18
16+
registry-url: "https://registry.npmjs.org"
17+
- run: npm ci
18+
- run: npm publish
19+
env:
20+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ console.log(allowSetForegroundWindow()) // all processes will be enabled to set
2020

2121
## Developing
2222
* `npm install -g node-gyp`
23-
* `node-gyp configure`
24-
* `node-gyp build`
23+
* `npm install`
2524
* `npm test`
2625

2726
## Resources

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "windows-foreground-love",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "API wrapper for AllowSetForegroundWindow",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"typings": "index.d.ts",
77
"files": [
88
"src",
99
"binding.gyp",

src/common.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// """
2+
// Copyright Node.js contributors. All rights reserved.
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to
6+
// deal in the Software without restriction, including without limitation the
7+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
// sell copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
// IN THE SOFTWARE.
21+
// """
22+
23+
// Empty value so that macros here are able to return NULL or void
24+
#define NAPI_RETVAL_NOTHING // Intentionally blank #define
25+
26+
#define GET_AND_THROW_LAST_ERROR(env) \
27+
do { \
28+
const napi_extended_error_info *error_info; \
29+
napi_get_last_error_info((env), &error_info); \
30+
bool is_pending; \
31+
napi_is_exception_pending((env), &is_pending); \
32+
/* If an exception is already pending, don't rethrow it */ \
33+
if (!is_pending) { \
34+
const char* error_message = error_info->error_message != NULL ? \
35+
error_info->error_message : \
36+
"empty error message"; \
37+
napi_throw_error((env), NULL, error_message); \
38+
} \
39+
} while (0)
40+
41+
#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
42+
do { \
43+
if (!(assertion)) { \
44+
napi_throw_error( \
45+
(env), \
46+
NULL, \
47+
"assertion (" #assertion ") failed: " message); \
48+
return ret_val; \
49+
} \
50+
} while (0)
51+
52+
// Returns NULL on failed assertion.
53+
// This is meant to be used inside napi_callback methods.
54+
#define NAPI_ASSERT(env, assertion, message) \
55+
NAPI_ASSERT_BASE(env, assertion, message, NULL)
56+
57+
// Returns empty on failed assertion.
58+
// This is meant to be used inside functions with void return type.
59+
#define NAPI_ASSERT_RETURN_VOID(env, assertion, message) \
60+
NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)
61+
62+
#define NAPI_CALL_BASE(env, the_call, ret_val) \
63+
do { \
64+
if ((the_call) != napi_ok) { \
65+
GET_AND_THROW_LAST_ERROR((env)); \
66+
return ret_val; \
67+
} \
68+
} while (0)
69+
70+
// Returns NULL if the_call doesn't return napi_ok.
71+
#define NAPI_CALL(env, the_call) \
72+
NAPI_CALL_BASE(env, the_call, NULL)
73+
74+
// Returns empty if the_call doesn't return napi_ok.
75+
#define NAPI_CALL_RETURN_VOID(env, the_call) \
76+
NAPI_CALL_BASE(env, the_call, NAPI_RETVAL_NOTHING)
77+
78+
// Returns empty if the_call doesn't return napi_ok.
79+
#define NAPI_CALL_RETURN_STATUS(env, the_call) \
80+
do { \
81+
napi_status status = (the_call); \
82+
if (status != napi_ok) { \
83+
GET_AND_THROW_LAST_ERROR((env)); \
84+
return status; \
85+
} \
86+
} while (0)
87+
88+
#define DECLARE_NAPI_PROPERTY(name, func) \
89+
{ (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL }
90+
91+
#define DECLARE_NAPI_GETTER(name, func) \
92+
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }

src/foreground-love.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
#include <windows.h>
55
#endif
66

7+
#include "common.h"
8+
79
namespace foreground_love {
810

911
napi_value AllowSetForegroundWindow(napi_env env, napi_callback_info info) {
1012
#if _WIN32
1113
napi_value argv[1];
1214
size_t argc = 1;
1315

14-
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
16+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
1517

1618
int pid = 0;
1719

1820
if (argc != 0) {
1921
napi_valuetype valuetype0;
20-
napi_typeof(env, argv[0], &valuetype0);
22+
NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype0));
2123

2224
if (valuetype0 != napi_undefined) {
2325
napi_status status = napi_get_value_int32(env, argv[0], &pid);
2426

2527
if (status != napi_ok) {
26-
napi_throw_type_error(env, NULL, "Invalid number was passed as argument.");
28+
NAPI_CALL(env, napi_throw_type_error(env, NULL, "Invalid number was passed as argument."));
2729
return NULL;
2830
}
2931
}
@@ -38,19 +40,19 @@ napi_value AllowSetForegroundWindow(napi_env env, napi_callback_info info) {
3840
}
3941

4042
napi_value napi_result;
41-
napi_get_boolean(env, result, &napi_result);
43+
NAPI_CALL(env, napi_get_boolean(env, result, &napi_result));
4244
#else
4345
napi_value napi_result;
44-
napi_get_undefined(env, &napi_result);
46+
NAPI_CALL(env, napi_get_undefined(env, &napi_result));
4547
#endif
4648

4749
return napi_result;
4850
}
4951

5052
napi_value Init(napi_env env, napi_value exports) {
5153
napi_value allowSetForegroundWindow;
52-
napi_create_function(env, "allowSetForegroundWindow", NAPI_AUTO_LENGTH, AllowSetForegroundWindow, NULL, &allowSetForegroundWindow);
53-
napi_set_named_property(env, exports, "allowSetForegroundWindow", allowSetForegroundWindow);
54+
NAPI_CALL(env, napi_create_function(env, "allowSetForegroundWindow", NAPI_AUTO_LENGTH, AllowSetForegroundWindow, NULL, &allowSetForegroundWindow));
55+
NAPI_CALL(env, napi_set_named_property(env, exports, "allowSetForegroundWindow", allowSetForegroundWindow));
5456

5557
return exports;
5658
}

0 commit comments

Comments
 (0)