diff --git a/build.hxml b/build.hxml index 608ce4b..32d6900 100644 --- a/build.hxml +++ b/build.hxml @@ -4,4 +4,4 @@ -main Main -lib utest --macro jsrequire.JSRequire.npmInstall(false) --cmd node bin/test.js \ No newline at end of file +-cmd node bin/test.js diff --git a/src/jsrequire/JSRequire.hx b/src/jsrequire/JSRequire.hx index cec3752..24330b3 100644 --- a/src/jsrequire/JSRequire.hx +++ b/src/jsrequire/JSRequire.hx @@ -6,7 +6,11 @@ import haxe.macro.Type; import haxe.macro.ExprTools; class JSRequire { - macro public static function npmInstall(?createPackageJson : Bool) { + + static var packageJsonPath = 'package.json'; + + macro public static function npmInstall(?createPackageJson : Bool,?jsonPath:String) { + if (jsonPath != null) packageJsonPath = jsonPath; installNpmDependencies(createPackageJson); return macro null; } @@ -43,11 +47,11 @@ class JSRequire { } static function ensurePackageJson(generate : Bool) { - if(sys.FileSystem.exists("package.json")) return true; + if(sys.FileSystem.exists(packageJsonPath)) return true; if(generate) { Sys.command('npm', ['init', '.']); } - return sys.FileSystem.exists("package.json"); + return sys.FileSystem.exists(packageJsonPath); } static function getDependencies(hasPackageJson : Bool) : Dynamic { @@ -59,7 +63,7 @@ class JSRequire { } static function getDependenciesFromPackageJson() : Dynamic { - var json = haxe.Json.parse(sys.io.File.getContent("package.json")), + var json = haxe.Json.parse(sys.io.File.getContent(packageJsonPath)), dependencies : Dynamic = json.dependencies; return null != dependencies ? dependencies : {}; } @@ -81,4 +85,4 @@ class JSRequire { Sys.command('npm', ['install', '--save', module]); else Sys.command('npm', ['install', module]); -} \ No newline at end of file +} diff --git a/test/Lodash.hx b/test/Lodash.hx index faf42fe..ef0ff84 100644 --- a/test/Lodash.hx +++ b/test/Lodash.hx @@ -1,4 +1,5 @@ @:jsRequire("lodash-node") extern class Lodash { static function pluck(arr : Array>, field : String) : T; -} \ No newline at end of file +} + diff --git a/test/Main.hx b/test/Main.hx index 7b7a943..194876b 100644 --- a/test/Main.hx +++ b/test/Main.hx @@ -15,4 +15,4 @@ class Main { public function testRequire() { Assert.equals("1", pluck([{ value : "1" }], "value")); } -} \ No newline at end of file +}