Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Generator to allow using cmake-js and updated typescript definitions #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
207 changes: 153 additions & 54 deletions __tests__/app.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,210 @@
'use strict';
const path = require('path');
const assert = require('yeoman-assert');
const helpers = require('yeoman-test');
const uppercamelcase = require('uppercamelcase');
"use strict";
const path = require("path");
const assert = require("yeoman-assert");
const helpers = require("yeoman-test");
const uppercamelcase = require("uppercamelcase");

function defaultPackageJson() {
return {
description: "dummy module",
repository: "https://github.com/blah/blah",
license: "MIT",
private: true
private: true,
};
}

function defaultProps() {
const moduleName = 'test-napi-module';
const moduleName = "test-napi-module";
return {
moduleFileName: `${moduleName}-native`,
moduleSourceFileName: moduleName.replace(new RegExp('-', 'g'), '_') + '.cc',
moduleHeaderFileName: moduleName.replace(new RegExp('-', 'g'), '_') + '.h',
moduleSourceFileName: moduleName.replace(new RegExp("-", "g"), "_") + ".cc",
moduleHeaderFileName: moduleName.replace(new RegExp("-", "g"), "_") + ".h",
moduleClassName: uppercamelcase(moduleName),
bindingJsFile: '../lib/binding.js'
// bindingJsFile: '../lib/binding.js'
};
}

function defaultPackageConfigFunc() {
return new Promise(resolve => {
resolve({
props: defaultProps(),
packageJsonData: defaultPackageJson()
packageJsonData: defaultPackageJson(),
});
});
}

function defaultTypeScriptPackageConfigFunc() {
return new Promise(resolve => {
const tsProps = defaultProps();
tsProps.bindingJsFile = '../dist/binding.js';
resolve({
props: tsProps,
packageJsonData: defaultPackageJson()
});
/**
* Generator with all default options (gyp:basic:js)
*/
describe("generator-napi-module:app", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc });
});
}

describe('generator-napi-module:app', () => {
it("creates files", () => {
assert.file([
"binding.gyp",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"test/test_binding.js",
]);
});
});

/**
* Generator with options (cmake:basic:js)
*/
describe("generator-napi-module:app:cmake", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, cmake: true });
});

it("creates files", () => {
assert.file([
"CMakeLists.txt",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"test/test_binding.js",
]);
});
});

/**
* Generator with options (gyp:basic:ts)
*/
describe("generator-napi-module:app:ts", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, typescript: true });
});

it("creates files", () => {
assert.file([
"binding.gyp",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"test/test_binding.ts",
"tsconfig.json",
"types/test-napi-module-native.d.ts",
"types/index.d.ts",
]);
});
});

/**
* Generator with options (cmake:basic:ts)
*/
describe("generator-napi-module:app:cmake:ts", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, cmake: true, typescript: true });
});

it("creates files", () => {
assert.file([
"CMakeLists.txt",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"test/test_binding.ts",
"tsconfig.json",
"types/test-napi-module-native.d.ts",
"types/index.d.ts",
]);
});
});

/**
* Generator with options (gyp:inter:js)
*/
describe("generator-napi-module:app:inter", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({packageConfigFunc: defaultPackageConfigFunc});
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, intermediate: true});
});

it('creates files', () => {
it("creates files", () => {
assert.file([
'binding.gyp',
'lib/binding.js',
'src/test_napi_module.cc',
'test/test_binding.js'
"binding.gyp",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"src/test_napi_module.h",
"test/test_binding.js",
]);
});
});

describe('generator-napi-module:app:ts', () => {
/**
* Generator with options (cmake:inter:js)
*/
describe("generator-napi-module:app:cmake:inter", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({packageConfigFunc: defaultTypeScriptPackageConfigFunc, typescript: true});
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, intermediate: true, cmake: true });
});

it('creates files', () => {
it("creates files", () => {
assert.file([
'binding.gyp',
'lib/binding.ts',
'src/test_napi_module.cc',
'test/test_binding.js'
"CMakeLists.txt",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"src/test_napi_module.h",
"test/test_binding.js",
]);
});
});

describe('generator-napi-module:app:inter', () => {
/**
* Generator with options (gyp:inter:ts)
*/
describe("generator-napi-module:app:inter:ts", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({packageConfigFunc: defaultPackageConfigFunc, intermediate: true,});
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, intermediate: true, typescript: true});
});

it('creates files', () => {
it("creates files", () => {
assert.file([
'binding.gyp',
'lib/binding.js',
'src/test_napi_module.cc',
'src/test_napi_module.h',
'test/test_binding.js'
"binding.gyp",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"src/test_napi_module.h",
"tsconfig.json",
"test/test_binding.ts",
"types/test-napi-module-native.d.ts",
"types/index.d.ts"
]);
});
});

describe('generator-napi-module:app:interts', () => {
/**
* Generator with options (cmake:inter:ts)
*/
describe("generator-napi-module:app:cmake:inter:ts", () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({packageConfigFunc: defaultTypeScriptPackageConfigFunc, intermediate: true, typescript: true});
return helpers.run(path.join(__dirname, "../generators/app"))
.withOptions({ packageConfigFunc: defaultPackageConfigFunc, intermediate: true, typescript: true, cmake: true });
});

it('creates files', () => {
it("creates files", () => {
assert.file([
'binding.gyp',
'lib/binding.ts',
'src/test_napi_module.cc',
'src/test_napi_module.h',
'test/test_binding.js'
"CMakeLists.txt",
"index.js",
"scripts/preinstall.sh",
"src/test_napi_module.cc",
"src/test_napi_module.h",
"tsconfig.json",
"test/test_binding.ts",
"types/test-napi-module-native.d.ts",
"types/index.d.ts"
]);
});
});
32 changes: 32 additions & 0 deletions generators/app/templates/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0042 NEW)
set (CMAKE_CXX_STANDARD 17)

project(<%= moduleFileName %>)

# Find your dependency packages here
# find_package(... REQUIRED)

include_directories(${CMAKE_JS_INC})


add_library(${PROJECT_NAME} SHARED src/<%= moduleSourceFileName %> ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})

# Note: you can as many library as you like the same way, or put it all in the same lib

# Include Node-API wrappers
execute_process(COMMAND node -p "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)
string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})

# Warning: it is important to run the target_include_directory **AFTER**
# running the regex
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})


# define NAPI_VERSION
add_definitions(-DNAPI_VERSION=3)
4 changes: 4 additions & 0 deletions generators/app/templates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Project <%= moduleFileName %>

This project was auto-generated with yeoman using the `generator-napi-module`
generator.
File renamed without changes.
3 changes: 3 additions & 0 deletions generators/app/templates/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('bindings')('<%= moduleFileName %>')
}
4 changes: 0 additions & 4 deletions generators/app/templates/lib/binding.js

This file was deleted.

4 changes: 0 additions & 4 deletions generators/app/templates/lib/binding.ts

This file was deleted.

11 changes: 0 additions & 11 deletions generators/app/templates/lib/binding_intermediate.js

This file was deleted.

21 changes: 0 additions & 21 deletions generators/app/templates/lib/binding_intermediate.ts

This file was deleted.

4 changes: 4 additions & 0 deletions generators/app/templates/npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
build/
dist/
lib/
9 changes: 9 additions & 0 deletions generators/app/templates/package.cmake.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
"cmake-js": "^7.2.1"
},
"scripts": {
"build": "cmake-js compile -G 'Unix Makefiles'",
"clean": "cmake-js clean && rm -f lib/*.node"
}
}
10 changes: 10 additions & 0 deletions generators/app/templates/package.gyp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"devDependencies": {
"node-gyp": "^9.4.0"
},
"gypfile": true,
"scripts": {
"build": "node-gyp configure && node-gyp build",
"clean": "rm -rf build"
}
}
19 changes: 11 additions & 8 deletions generators/app/templates/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"main": "lib/binding.js",
"dependencies": {
"node-addon-api": "^1.1.0"
},
"scripts": {
"test": "node --napi-modules ./test/test_binding.js"
},
"gypfile": true
"dependencies": {
"node-addon-api": "^7.0.0",
"bindings": "^1.5.0"
},
"scripts": {
"//prepublish": "npm run clean",
"preinstall": "scripts/preinstall.sh",
"install": "npm run build",
"rebuild": "npm run clean && npm run build",
"test": "node --napi-modules ./test/test_binding.js"
}
}
Loading