From 87966b6787af5e7fe089ef18a47211ddf7c47060 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 14 Oct 2014 14:59:28 -0700 Subject: [PATCH] Cleanup tmp output --- lib/execjs/external_runtime.rb | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/lib/execjs/external_runtime.rb b/lib/execjs/external_runtime.rb index dc5f337..04de55a 100644 --- a/lib/execjs/external_runtime.rb +++ b/lib/execjs/external_runtime.rb @@ -143,17 +143,19 @@ module ExecJS if ExecJS.windows? def exec_runtime(filename) - command = binary.split(" ") << filename + path = Dir::Tmpname.create(['execjs', 'json']) {} + begin + command = binary.split(" ") << filename + `#{shell_escape(*command)} 2>&1 > #{path}` + output = File.open(path, 'rb', @popen_options) { |f| f.read } + ensure + File.unlink(path) if path + end - Dir::Tmpname.create(['execjs', 'json']) do |output| - `#{shell_escape(*command)} 2>&1 > #{output}` - output = File.open(output, 'rb', @popen_options) { |f| f.read } - - if $?.success? - return output - else - raise RuntimeError, output - end + if $?.success? + output + else + raise RuntimeError, output end end @@ -164,16 +166,6 @@ module ExecJS arg }.join(" ") end - - # See Tempfile.create on Ruby 2.1 - def create_tempfile(basename) - tmpfile = nil - Dir::Tmpname.create(basename) do |tmpname| - mode = File::WRONLY | File::CREAT | File::EXCL - tmpfile = File.open(tmpname, mode, 0600) - end - tmpfile - end else def exec_runtime(filename) io = IO.popen(binary.split(' ') << filename, @popen_options.merge({err: [:child, :out]}))