forked from babel/babel-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabel.py
103 lines (90 loc) · 3.15 KB
/
Babel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import os
import platform
import subprocess
import sublime
import sublime_plugin
if platform.system() == 'Darwin':
os_name = 'osx'
elif platform.system() == 'Windows':
os_name = 'windows'
else:
os_name = 'linux'
BIN_PATH = os.path.join(
sublime.packages_path(),
os.path.dirname(os.path.realpath(__file__)),
'babel-transform.js'
)
class BabelCommand(sublime_plugin.TextCommand):
def run(self, edit):
selected_text = self.get_text()
code = self.babelify(selected_text)
if code:
w = sublime.Window.new_file(self.view.window())
w.settings().set('default_extension', 'js')
w.set_syntax_file(self.view.settings().get('syntax'))
w.set_scratch(True)
w.insert(edit, 0, code)
def babelify(self, data):
try:
return node_bridge(data, BIN_PATH, [json.dumps({
# from sublime
'filename': self.view.file_name(),
'newline_at_eof': self.view.settings().get('ensure_newline_at_eof_on_save'),
# from babel-sublime settings
'debug': self.get_setting('debug'),
'use_local_babel': self.get_setting('use_local_babel'),
'node_modules': self.get_setting_by_os('node_modules'),
'options': self.get_setting('options')
})])
except Exception as e:
return str(e)
def get_text(self):
if not self.has_selection():
region = sublime.Region(0, self.view.size())
return self.view.substr(region)
selected_text = ''
for region in self.view.sel():
selected_text = selected_text + self.view.substr(region) + '\n'
return selected_text
def has_selection(self):
for sel in self.view.sel():
if sel.a != sel.b:
return True
return False
def get_setting(self, key):
settings = self.view.settings().get('Babel')
if settings is None:
settings = sublime.load_settings('Babel.sublime-settings')
return settings.get(key)
def get_setting_by_os(self, key):
setting = self.get_setting(key)
if setting:
return setting.get(os_name)
def node_bridge(data, bin, args=[]):
env = os.environ.copy()
startupinfo = None
if os_name == 'osx':
# GUI apps in OS X don't contain .bashrc/.zshrc set paths
env['PATH'] += ':/usr/local/bin'
elif os_name == 'windows':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
try:
p = subprocess.Popen(
['node', bin] + args,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
startupinfo=startupinfo
)
except OSError:
raise Exception('Error: Couldn\'t find "node" in "%s"' % env['PATH'])
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