Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run script on temp file #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions ESLint-Formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sublime, sublime_plugin
import platform
import glob
import tempfile
import os, sys, subprocess, codecs, webbrowser
from subprocess import Popen, PIPE

Expand Down Expand Up @@ -36,15 +37,22 @@ def run(self, edit):

buffer_text = self.get_buffer_text(entire_buffer_region)

output = self.run_script_on_file(self.view.file_name())
output = None
dirname = None
if self.view.file_name():
dirname = os.path.dirname(self.view.file_name())
with tempfile.NamedTemporaryFile(mode="w+", delete=False, dir=dirname, encoding="utf-8") as tmp:
try:
tmp.write(buffer_text)
tmp.close()
output = self.run_script_on_file(tmp.name)
finally:
os.unlink(tmp.name)

# log output in debug mode
if PluginUtils.get_pref("debug"):
print(output)

return
# eslint currently does not print the fixed file to stdout, it just modifies the file.

# If the prettified text length is nil, the current syntax isn't supported.
if output == None or len(output) < 1:
return
Expand Down Expand Up @@ -241,13 +249,9 @@ def get_output(cmd, cdir, data):
p = Popen(cmd,
stdout=PIPE, stdin=PIPE, stderr=PIPE,
cwd=cdir, shell=IS_WINDOWS)
p.communicate()
except OSError:
raise Exception('Couldn\'t find Node.js. Make sure it\'s in your $PATH by running `node -v` in your command-line.')
stdout, stderr = p.communicate(input=data.encode('utf-8'))
stdout = stdout.decode('utf-8')
stderr = stderr.decode('utf-8')

if stderr:
raise Exception('Error: %s' % stderr)
else:
return stdout
with open(data, "r", encoding="utf-8") as formatted:
return formatted.read()