Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions napi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/
node_modules/
tmp/
src/
48 changes: 48 additions & 0 deletions napi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0042 NEW)
set (CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

include(ExternalProject)

project (node-version-weaver)

include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-addon-api)
include_directories(${CMAKE_JS_INC})

file(GLOB SOURCE_FILES "node-semver.cc")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})

# Define NAPI_VERSION
add_definitions(-DNAPI_VERSION=9)

ExternalProject_Add(version_weaver
SOURCE_DIR ${CMAKE_SOURCE_DIR}/../
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/version_weaver
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
)
add_dependencies(${PROJECT_NAME} version_weaver)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/version_weaver/include)
target_link_libraries(${PROJECT_NAME} ${CMAKE_CURRENT_BINARY_DIR}/version_weaver/lib/libversion_weaver.a)

function (add_custom_command_with_target _targetName _output firstDep)
add_custom_command(
OUTPUT ${_output}
COMMAND sed "s/__FUNCTION__/${FUNCTION}/" ${firstDep} > ${_output}
DEPENDS ${firstDep} ${ARGN}
)
add_custom_target(${_targetName} ALL DEPENDS ${_output})
endfunction()

set(FUNCTIONS coerce parse valid)
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/functions
COMMAND mkdir ${CMAKE_BINARY_DIR}/functions
VERBATIM
)
foreach(FUNCTION ${FUNCTIONS})
add_custom_command_with_target(${FUNCTION} ${CMAKE_BINARY_DIR}/functions/${FUNCTION}.js ${CMAKE_SOURCE_DIR}/templates/functions.template ${CMAKE_BINARY_DIR}/functions)
endforeach()

23 changes: 23 additions & 0 deletions napi/node-semver.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <napi.h>
#include "version_weaver.h"

Napi::Value CoerceWrapped(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "String expected").ThrowAsJavaScriptException();
return env.Null();
}

std::string arg0 = info[0].As<Napi::String>().Utf8Value();
std::optional<std::string> result = version_weaver::coerce(arg0);

return Napi::String::New(env, result.has_value() ? result.value() : "");
}

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

NODE_API_MODULE(myaddon, Init)
15 changes: 15 additions & 0 deletions napi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add package-lock and use npm? Yarn might be hard to maintain in the long term.

"name": "node-version-weaver",
"description": "N-API port of node-semver",
"dependencies": {
"bindings": "~1.5.0",
"node-addon-api": "^8.3.0"
},
"scripts": {
"build": "cmake-js --prefer-make compile",
"test": "TODO"
},
"devDependencies": {
"cmake-js": "^7.3.0"
}
}
2 changes: 2 additions & 0 deletions napi/templates/functions.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require("bindings")("node-version-weaver").__FUNCTION__;
Loading