Skip to content

Commit 90f3d31

Browse files
committed
Merge pull request #433 from zhengbli/supportForOtherTsdSdk
Support customized typescript tsdk location
2 parents 0504a55 + ffab29f commit 90f3d31

File tree

4 files changed

+15
-71
lines changed

4 files changed

+15
-71
lines changed

main.py

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def plugin_loaded():
8484
else:
8585
print("ref view not found")
8686
log.debug("plugin_loaded ended")
87-
_check_typescript_version()
8887

8988

9089
def plugin_unloaded():
@@ -98,67 +97,3 @@ def plugin_unloaded():
9897
if ref_info:
9998
ref_view.settings().set('refinfo', ref_info.as_value())
10099
cli.service.exit()
101-
102-
103-
_UPDATE_TS_MESSAGE = "Warning from TypeScript Sublime Text plugin:\n\n\
104-
Detected command-line TypeScript compiler version '{0}'. The TypeScript \
105-
Sublime Text plugin is using compiler version '{1}'. There may be \
106-
differences in behavior between releases.\n\n\
107-
To update your command-line TypeScript compiler to the latest release, run \
108-
'npm update -g typescript'."
109-
110-
def _check_typescript_version():
111-
"""
112-
Notify user to upgrade npm typescript. Do this only once every time the
113-
plugin is updated.
114-
"""
115-
settings = sublime.load_settings('Preferences.sublime-settings')
116-
cached_plugin_version = settings.get("typescript_plugin_tsc_version")
117-
118-
try:
119-
plugin_tsc_version = _get_plugin_tsc_version()
120-
121-
if cached_plugin_version != plugin_tsc_version:
122-
# The version number getting from the tsc command is different to
123-
# the version number stored in the setting file. This means user
124-
# has just updated the plugin.
125-
126-
npm_tsc_version = _get_npm_tsc_version()
127-
128-
if npm_tsc_version != plugin_tsc_version:
129-
sublime.message_dialog(_UPDATE_TS_MESSAGE.format(
130-
npm_tsc_version, plugin_tsc_version))
131-
132-
# Update the version in setting file so we don't show this
133-
# message twice.
134-
settings.set("typescript_plugin_tsc_version", plugin_tsc_version)
135-
sublime.save_settings("Preferences.sublime-settings")
136-
137-
except Exception as error:
138-
log.error(error)
139-
140-
def _get_plugin_tsc_version():
141-
cmd = [get_node_path(), TSC_PATH, "-v"]
142-
return _execute_cmd_and_parse_version_from_output(cmd)
143-
144-
def _is_executable(path):
145-
return os.path.isfile(path) and os.access(path, os.X_OK)
146-
147-
def _get_npm_tsc_version():
148-
if os.name != 'nt' and _is_executable("/usr/local/bin/tsc"): # Default location on MacOS
149-
cmd = [get_node_path(), "/usr/local/bin/tsc", "-v"]
150-
else:
151-
cmd = ["tsc", "-v"]
152-
return _execute_cmd_and_parse_version_from_output(cmd)
153-
154-
def _execute_cmd_and_parse_version_from_output(cmd):
155-
if os.name != 'nt': # Linux/MacOS
156-
cmd = "'" + "' '".join(cmd) + "'"
157-
output = subprocess.check_output(cmd, shell=True).decode('UTF-8')
158-
159-
# Use regex to parse the verion number from <output> e.g. parse
160-
# "1.5.0-beta" from "message TS6029: Version 1.5.0-beta\r\n".
161-
match_object = re.search("Version\s*([\w.-]+)", output, re.IGNORECASE)
162-
if match_object is None:
163-
raise Exception("Cannot parse version number from ouput: '{0}'".format(output))
164-
return match_object.groups()[0]

typescript/commands/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def run(self):
1818
if "configFileName" in project_info["body"]:
1919
tsconfig_dir = dirname(project_info["body"]["configFileName"])
2020
self.window.run_command("exec", {
21-
"cmd": [get_node_path(), TSC_PATH, "-p", tsconfig_dir],
21+
"cmd": [get_node_path(), get_tsc_path(), "-p", tsconfig_dir],
2222
# regex to capture build result for displaying in the output panel
2323
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$"
2424
})
@@ -32,7 +32,7 @@ def run(self):
3232
)
3333

3434
def compile_inferred_project(self, file_name, params=""):
35-
cmd = [get_node_path(), TSC_PATH, file_name]
35+
cmd = [get_node_path(), get_tsc_path(), file_name]
3636
print(cmd)
3737
if params != "":
3838
cmd.extend(params.split(' '))

typescript/libs/editor_client.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .node_client import ServerClient, WorkerClient
33
from .service_proxy import ServiceProxy
44
from .global_vars import *
5+
from . import global_vars
56

67

78
class ClientFileInfo:
@@ -48,12 +49,17 @@ def initialize(self):
4849

4950
# retrieve the path to tsserver.js
5051
# first see if user set the path to the file
51-
settings = sublime.load_settings('Preferences.sublime-settings')
52-
proc_file = settings.get('typescript_proc_file')
53-
if not proc_file:
52+
settings = sublime.load_settings("Preferences.sublime-settings")
53+
tsdk_location = settings.get("typescript_tsdk")
54+
if tsdk_location:
55+
proc_file = os.path.join(tsdk_location, "tsserver.js")
56+
global_vars._tsc_path = os.path.join(tsdk_location, "tsc.js")
57+
else:
5458
# otherwise, get tsserver.js from package directory
5559
proc_file = os.path.join(PLUGIN_DIR, "tsserver", "tsserver.js")
60+
global_vars._tsc_path = os.path.join(PLUGIN_DIR, "tsserver", "tsc.js")
5661
print("Path of tsserver.js: " + proc_file)
62+
print("Path of tsc.js: " + get_tsc_path())
5763

5864
self.node_client = ServerClient(proc_file)
5965
self.worker_client = WorkerClient(proc_file)

typescript/libs/global_vars.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
def get_node_path():
3535
return _node_path
3636

37-
TSC_PATH = os.path.join(PLUGIN_DIR, "tsserver", "tsc.js")
37+
# The tsc.js path will be initialized in the editor_client.py module
38+
_tsc_path = None
39+
def get_tsc_path():
40+
return _tsc_path
3841

3942
# only Sublime Text 3 build after 3072 support tooltip
4043
TOOLTIP_SUPPORT = int(sublime.version()) >= 3072

0 commit comments

Comments
 (0)