Skip to content

Commit

Permalink
Merge pull request #18 from fukaoi/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
fukaoi authored Sep 5, 2019
2 parents de2fd6f + d848b71 commit 79c9871
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
11 changes: 7 additions & 4 deletions ext/audit/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions spec/nodejs/internal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/nodejs_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/nodejs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
)
Expand Down
16 changes: 12 additions & 4 deletions src/nodejs/internal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 79c9871

Please sign in to comment.