diff --git a/.travis.yml b/.travis.yml index 8d80631..f428d4e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -65,7 +65,7 @@ before_install: fi install: - npm install cmake-js -g -- npm install +- npm install --no-fetch script: - npm test - npm run unittest diff --git a/CMakeLists.txt b/CMakeLists.txt index 84f58da..a751b85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,4 +25,5 @@ add_subdirectory(src) if (CMAKE_JS_VERSION) # Build napa addon for node. add_subdirectory(node) + add_subdirectory(test/module/addon) endif() diff --git a/appveyor.yml b/appveyor.yml index 4779e98..1135ffb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -16,7 +16,7 @@ platform: install: - ps: Install-Product node $env:nodejs_version $env:platform - npm install cmake-js -g -- npm install +- npm install --no-fetch # Skip MSBuild stage build: off diff --git a/package.json b/package.json index cdb720b..d061ecd 100644 --- a/package.json +++ b/package.json @@ -9,18 +9,28 @@ "type": "git", "url": "https://github.com/Microsoft/napajs.git" }, + "binary": { + "module_name": "napa-binding", + "module_path": "./bin/", + "host": "https://github.com/Microsoft/napajs/releases/download/", + "remote_path": "{version}" + }, + "dependencies": { + "node-pre-gyp": "^0.6.36", + "npmlog": "^4.1.2" + }, "devDependencies": { - "mocha": ">= 3.4.2", - "typescript": ">= 2.2.1", - "@types/node": ">= 7.0.8", - "@types/mocha": ">= 2.2.0", - "markdown-table": "1.1.0" + "node-pre-gyp-github": "^1.3.1", + "mocha": "^3.5.0", + "typescript": "^2.4.2", + "@types/node": "^8.0.22", + "@types/mocha": "^2.2.41", + "markdown-table": "^1.1.1" }, "scripts": { "benchmark": "node benchmark/bench.js", - "install": "cmake-js compile", + "install": "node scripts/install.js", "prepare": "tsc -p lib && tsc -p test && tsc -p benchmark", - "pretest": "cmake-js compile -d test/module/addon", "test": "mocha test --recursive", "rebuild": "cmake-js rebuild && tsc -p lib", "rebuildd": "cmake-js rebuild --debug && tsc -p lib", diff --git a/scripts/install.js b/scripts/install.js new file mode 100644 index 0000000..ec262df --- /dev/null +++ b/scripts/install.js @@ -0,0 +1,82 @@ +#!/usr/bin/env node + +var log = require('npmlog'); +var execSync = require('child_process').execSync; + +// Default command +var fetchCommand = 'node-pre-gyp install --fallback-to-build=false'; +var buildCommand = 'cmake-js compile'; + +// ==== Determine install options ==== +// Skip the fetch stage. '--no-fetch', or '--fetch=false' +var skipFetch = process.env.hasOwnProperty('npm_config_fetch') && !process.env['npm_config_fetch']; +// Skip the build stage. '--no-build', or '--build=false' +var skipBuild = process.env.hasOwnProperty('npm_config_build') && !process.env['npm_config_build']; + +// Use debug build +if (process.env.hasOwnProperty('npm_config_debug') && process.env['npm_config_debug']) { + buildCommand += " --debug"; +} + +log.info('NAPA_INSTALL', 'installing napajs...'); + +var errorCode = 0; + +// ==== Try to fetch the pre-build binaries ==== +if (!skipFetch) { + try { + log.info('NAPA_INSTALL', 'downloading the pre-build binaries...'); + execSync(fetchCommand, { 'stdio': 'inherit' }); + + log.info('NAPA_INSTALL', 'completed successfully by download.'); + skipBuild = true; + } catch (e) { + errorCode = e.status; + + log.warn('NAPA_INSTALL', 'failed to download the pre-build binaries.'); + if (!skipBuild) { + log.warn('NAPA_INSTALL', 'will fallback to build stage.'); + } + } +} + +// ==== Try to build from sources ==== +if (!skipBuild) { + try { + log.info('NAPA_INSTALL', 'building from sources...'); + execSync(buildCommand, { 'stdio': 'inherit' }); + + log.info('NAPA_INSTALL', 'completed successfully by build.'); + } catch (e) { + errorCode = e.status; + + log.warning('NAPA_INSTALL', 'failed to build from sources.'); + } +} + +// ==== Compile Typescript files ==== +if (errorCode == 0) { + var npmVersion = execSync('npm --version').toString().trim(); + log.info('NAPA_INSTALL', `current NPM version=${npmVersion}.`); + + var npmMajorVersion = npmVersion.split('.')[0]; + var npmMajorVersionNumber = parseInt(npmMajorVersion); + if (npmMajorVersionNumber < 4) { + // Before npm 4.x, the 'prepare' script will not run automatically by npm. + // We have to run it explicitly in this script. + log.info('NAPA_INSTALL', 'NPM below 4.x does not recognize script "prepare". We need to run it explicitly.'); + log.info('NAPA_INSTALL', 'running "npm run prepare"...'); + + try { + execSync('npm run prepare', { 'stdio': 'inherit' }); + } catch (e) { + errorCode = e.status; + } + } +} + +if (errorCode != 0) { + log.error('NAPA_INSTALL', 'failed to install napajs.'); +} + +process.exit(errorCode); diff --git a/test/module-test.ts b/test/module-test.ts index 9385b09..e1ba887 100644 --- a/test/module-test.ts +++ b/test/module-test.ts @@ -40,7 +40,7 @@ describe('napajs/module', function () { return napaZone.execute(() => { var assert = require("assert"); var jsonModule = require('./module/test.json'); - + assert.notEqual(jsonModule, undefined); assert.equal(jsonModule.prop1, "val1"); assert.equal(jsonModule.prop2, "val2"); @@ -50,8 +50,8 @@ describe('napajs/module', function () { it('napa module', () => { return napaZone.execute(() => { var assert = require("assert"); - var napaModule = require('./module/addon/build/simple-napa-addon.napa'); - + var napaModule = require('../bin/simple-addon.napa'); + assert.notEqual(napaModule, undefined); assert.equal(napaModule.getModuleName(), "simple-napa-addon"); }); @@ -60,8 +60,8 @@ describe('napajs/module', function () { it('object wrap module', () => { return napaZone.execute(() => { var assert = require("assert"); - var napaModule = require('./module/addon/build/simple-napa-addon.napa'); - + var napaModule = require('../bin/simple-addon.napa'); + var obj = napaModule.createSimpleObjectWrap(); assert.notEqual(obj, undefined); obj.setValue(3); @@ -411,7 +411,7 @@ describe('napajs/module', function () { it('post async work', () => { return napaZone.execute(() => { var assert = require("assert"); - var napaModule = require('./module/addon/build/simple-napa-addon.napa'); + var napaModule = require('../bin/simple-addon.napa'); var obj = napaModule.createSimpleObjectWrap(); obj.setValue(3); @@ -434,7 +434,7 @@ describe('napajs/module', function () { it('do async work', () => { return napaZone.execute(() => { var assert = require("assert"); - var napaModule = require('./module/addon/build/simple-napa-addon.napa'); + var napaModule = require('../bin/simple-addon.napa'); var obj = napaModule.createSimpleObjectWrap(); obj.setValue(8); diff --git a/test/module/addon/CMakeLists.txt b/test/module/addon/CMakeLists.txt index 1de5a22..b546694 100644 --- a/test/module/addon/CMakeLists.txt +++ b/test/module/addon/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.2 FATAL_ERROR) -project("simple-napa-addon") +project("simple-addon") set(NAPA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) @@ -21,10 +21,10 @@ set_target_properties(${TARGET_NAME} PROPERTIES PREFIX "" SUFFIX ".napa") # Set output directory for the addon set_target_properties(${TARGET_NAME} PROPERTIES - RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/build - RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/build - LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/build - LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/build + RUNTIME_OUTPUT_DIRECTORY_DEBUG ${NAPA_ROOT}/bin + RUNTIME_OUTPUT_DIRECTORY_RELEASE ${NAPA_ROOT}/bin + LIBRARY_OUTPUT_DIRECTORY_DEBUG ${NAPA_ROOT}/bin + LIBRARY_OUTPUT_DIRECTORY_RELEASE ${NAPA_ROOT}/bin ) # Include directories diff --git a/test/module/resolution-tests.js b/test/module/resolution-tests.js index 37e9edd..8ea30ab 100644 --- a/test/module/resolution-tests.js +++ b/test/module/resolution-tests.js @@ -23,7 +23,7 @@ function run() { assert(require.resolve('./sub-folder/.././sub-folder/file.js'), __dirname + '/sub-folder/file.js'); // Relative path without extension to napa addon - assert(require.resolve('./addon/build/simple-napa-addon'), __dirname + '/addon/build/simple-napa-addon.napa'); + assert(require.resolve('./sub-folder/addon/mock-addon'), __dirname + '/sub-folder/addon/mock-addon.napa'); // From node_modules with extension assert(require.resolve('file.js'), __dirname + '/node_modules/file.js'); diff --git a/test/module/sub-folder/addon/mock-addon.napa b/test/module/sub-folder/addon/mock-addon.napa new file mode 100644 index 0000000..e69de29