From e6c86bef7b153226dcbe1d2829b9764fe16898b5 Mon Sep 17 00:00:00 2001 From: Felipe Lalanne <1822826+pipex@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:01:26 -0300 Subject: [PATCH 1/2] Update module to properly work with node-gyp This updates the module to use the recommendations in https://nodejs.github.io/node-addon-examples/build-tools/node-pre-gyp/ Change-type: minor --- Dockerfile | 1 - binding.gyp | 11 ++++++++ lib/index.ts | 4 +-- lib/systemd-bindings.ts | 59 +++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 1 - tsconfig.release.json | 15 +++++------ typings/index.node.d.ts | 21 --------------- 7 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 binding.gyp create mode 100644 lib/systemd-bindings.ts delete mode 100644 typings/index.node.d.ts diff --git a/Dockerfile b/Dockerfile index adfcab3..f1ed309 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,7 +28,6 @@ RUN npm install RUN npx node-pre-gyp package COPY lib ./lib -COPY typings ./typings COPY tests ./tests COPY ./wait-for-it.sh / diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 0000000..b8d95f2 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,11 @@ +{ + "target_name": "action_after_build", + "type": "none", + "dependencies": [ "<(module_name)" ], + "copies": [ + { + "files": [ "<(PRODUCT_DIR)/<(module_name).node" ], + "destination": "<(module_path)" + } + ] +} diff --git a/lib/index.ts b/lib/index.ts index a91d942..a3e2e12 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -8,9 +8,9 @@ import { powerOff, reboot, system, -} from '../native/index.node'; +} from './systemd-bindings'; -export { system, SystemBus } from '../native/index.node'; +export { system, SystemBus } from './systemd-bindings'; /** * Convenience method to return a singleton instance of the system bus. diff --git a/lib/systemd-bindings.ts b/lib/systemd-bindings.ts new file mode 100644 index 0000000..8ea4df6 --- /dev/null +++ b/lib/systemd-bindings.ts @@ -0,0 +1,59 @@ +/** + * We need to import binaries according to + * https://nodejs.github.io/node-addon-examples/build-tools/node-pre-gyp/#javascript-updates + * however, node-pre-gyp does not have typescript declarations so + * eslint will complain, which is why we add the eslint-disable-line + * statements + */ +const binary = require('@mapbox/node-pre-gyp'); // eslint-disable-line +const path = require('path'); // eslint-disable-line +const bindingPath = binary.find( + path.resolve(path.join(__dirname, '../package.json')), +); +const binding = require(bindingPath); // eslint-disable-line + +export declare class SystemBus { + // Needed for typechecking + private static readonly __id: unique symbol; + + // Do not allow direct instantiation + // or sub-classing + private constructor(); +} + +export declare function system(): Promise; + +// These methods +export declare function unitActiveState( + bus: SystemBus, + unitName: string, +): Promise; +export declare function unitPartOf( + bus: SystemBus, + unitName: string, +): Promise; +export declare function unitStart( + bus: SystemBus, + unitName: string, + mode: string, +): Promise; +export declare function unitStop( + bus: SystemBus, + unitName: string, + mode: string, +): Promise; +export declare function unitRestart( + bus: SystemBus, + unitName: string, + mode: string, +): Promise; +export declare function reboot( + bus: SystemBus, + interactive: boolean, +): Promise; +export declare function powerOff( + bus: SystemBus, + interactive: boolean, +): Promise; + +module.exports = exports = binding; diff --git a/tsconfig.json b/tsconfig.json index d4db716..64ea675 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,5 @@ "include": [ "lib/**/*.ts", "tests/**/*.ts", - "typings/**/*.d.ts" ] } diff --git a/tsconfig.release.json b/tsconfig.release.json index 1f64ac3..b710cb2 100644 --- a/tsconfig.release.json +++ b/tsconfig.release.json @@ -1,10 +1,9 @@ { - "extends": "./tsconfig.json", - "include": [ - "lib/**/*.ts", - "typings/**/*.d.ts" - ], - "exclude": [ - "lib/**/*.spec.ts" - ] + "extends": "./tsconfig.json", + "include": [ + "lib/**/*.ts", + ], + "exclude": [ + "lib/**/*.spec.ts" + ] } diff --git a/typings/index.node.d.ts b/typings/index.node.d.ts deleted file mode 100644 index aa0d485..0000000 --- a/typings/index.node.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -declare module "*index.node" { - class SystemBus { - // Needed for typechecking - private __id: unique symbol - - // Do not allow direct instantiation - // or sub-classing - private constructor(); - }; - - function system(): Promise; - - // These methods - function unitActiveState(bus: SystemBus, unitName: string): Promise; - function unitPartOf(bus: SystemBus, unitName: string): Promise; - function unitStart(bus: SystemBus, unitName: string, mode: string): Promise; - function unitStop(bus: SystemBus, unitName: string, mode: string): Promise; - function unitRestart(bus: SystemBus, unitName: string, mode: string): Promise; - function reboot(bus: SystemBus, interactive: boolean): Promise; - function powerOff(bus: SystemBus, interactive: boolean): Promise; -} From 640de1e3c49ab4e4f53ac82605e95c036e9d93f5 Mon Sep 17 00:00:00 2001 From: Felipe Lalanne <1822826+pipex@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:01:10 -0300 Subject: [PATCH 2/2] Update @balena/lint and typescript Change-type: patch --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 18668a9..c767747 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "author": "Balena Inc. ", "license": "Apache-2.0", "devDependencies": { - "@balena/lint": "^7.2.0", + "@balena/lint": "^7.2.4", "@types/chai": "^4.3.5", "@types/chai-as-promised": "^7.1.5", "@types/mocha": "^10.0.1", @@ -49,7 +49,7 @@ "mocha": "^10.2.0", "rimraf": "^5.0.1", "ts-node": "^10.9.1", - "typescript": "^5.1.6" + "typescript": "^5.3.0" }, "versionist": { "publishedAt": "2023-09-29T15:45:49.697Z"