diff --git a/spec/nodejs/internal_spec.cr b/spec/nodejs/internal_spec.cr index 4bd78cb1..a6a51998 100644 --- a/spec/nodejs/internal_spec.cr +++ b/spec/nodejs/internal_spec.cr @@ -85,3 +85,20 @@ describe "Replace relative path" do res.should eq expect_code end end + +describe "Scan sub dir" do + it "Get sub directory name" do + dirpaths = [] of String + dirpaths << "#{Internal.home_dir}/js/spec" + dirpaths << "#{Internal.home_dir}/js/spec2" + dirpaths.map { |dir| Dir.mkdir(dir) } + res = Internal.scanning_sub_dir + dirpaths.map { |dir| Dir.rmdir(dir) } + res.size.should eq 4 + end + + it "No get sub directory" do + res = Internal.scanning_sub_dir + res.empty?.should be_true + end +end diff --git a/spec/nodejs_spec.cr b/spec/nodejs_spec.cr index 2e061429..8e058487 100644 --- a/spec/nodejs_spec.cr +++ b/spec/nodejs_spec.cr @@ -198,6 +198,15 @@ describe "Read js code file and Eval js code" do res["text"].to_s.empty?.should be_false end + it "read example js file in sub directory" do + subdir = "#{Nodejs::Internal.home_dir}/js/demo" + FileUtils.mkdir(subdir) + FileUtils.cp("spec/js/disp.js", "#{subdir}/disp.js") + res = Nodejs.file_run("spec/js/file_run.js") + FileUtils.rm_rf(subdir) + res["text"].to_s.empty?.should be_false + end + it "Not found js file" do expect_raises(CrystalSideException) do Nodejs.file_run("spec/js/hoge_fuga.js") diff --git a/src/nodejs.cr b/src/nodejs.cr index a0d6c702..8556bfbc 100644 --- a/src/nodejs.cr +++ b/src/nodejs.cr @@ -6,7 +6,7 @@ module Nodejs NODE_PATH = "#{Internal.home_dir}/bin/node" - def eval(source_code : String, node_path : Array = [] of String) : JSON::Any + def eval(source_code : String) : JSON::Any # use make audit Internal.create_raw_js(source_code) unless ENV["RAW_JS"]? == nil @@ -15,7 +15,7 @@ module Nodejs status = Process.run( NODE_PATH, args: {"-e", "#{Function.set_return_js} #{source_code}"}, - env: Internal.setup_env(node_path), + env: Internal.setup_node_path(Internal.scanning_sub_dir), output: io, error: io_error ) diff --git a/src/nodejs/internal.cr b/src/nodejs/internal.cr index 925dbae6..e329c097 100644 --- a/src/nodejs/internal.cr +++ b/src/nodejs/internal.cr @@ -22,17 +22,25 @@ module Nodejs::Internal {result: result, output: output} end - def setup_env(path : Array(String)) : Hash(String, String) + def setup_node_path(add_path : Array(String)) : Hash(String, String) js_dir = "#{ENV["HOME"]}/.crystal-nodejs/js" node_path = { - "NODE_PATH" => ".:./:#{js_dir}:#{js_dir}/node_modules:", + "NODE_PATH" => "#{js_dir}:#{js_dir}/node_modules:", } - if !path.empty? - node_path["NODE_PATH"] = "#{node_path["NODE_PATH"]}:#{path.join(":")}" + if !add_path.empty? + node_path["NODE_PATH"] = "#{node_path["NODE_PATH"]}:#{add_path.join(":")}" end node_path end + def scanning_sub_dir : Array(String) + dirs = [] of String + Dir.glob("#{home_dir}/js/*/") do |dir| + dirs << dir unless "#{home_dir}/js/node_modules/" == dir + end + dirs.clone.map { |dir| "#{dir}node_modules/" } + dirs + end + def create_raw_js(raw : String) : Void hash = Digest::MD5.hexdigest(raw) dir = "/tmp/raw_js"