Skip to content

Support customized typescript tsdk location #433

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

Merged
merged 3 commits into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 0 additions & 65 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def plugin_loaded():
else:
print("ref view not found")
log.debug("plugin_loaded ended")
_check_typescript_version()


def plugin_unloaded():
Expand All @@ -98,67 +97,3 @@ def plugin_unloaded():
if ref_info:
ref_view.settings().set('refinfo', ref_info.as_value())
cli.service.exit()


_UPDATE_TS_MESSAGE = "Warning from TypeScript Sublime Text plugin:\n\n\
Detected command-line TypeScript compiler version '{0}'. The TypeScript \
Sublime Text plugin is using compiler version '{1}'. There may be \
differences in behavior between releases.\n\n\
To update your command-line TypeScript compiler to the latest release, run \
'npm update -g typescript'."

def _check_typescript_version():
"""
Notify user to upgrade npm typescript. Do this only once every time the
plugin is updated.
"""
settings = sublime.load_settings('Preferences.sublime-settings')
cached_plugin_version = settings.get("typescript_plugin_tsc_version")

try:
plugin_tsc_version = _get_plugin_tsc_version()

if cached_plugin_version != plugin_tsc_version:
# The version number getting from the tsc command is different to
# the version number stored in the setting file. This means user
# has just updated the plugin.

npm_tsc_version = _get_npm_tsc_version()

if npm_tsc_version != plugin_tsc_version:
sublime.message_dialog(_UPDATE_TS_MESSAGE.format(
npm_tsc_version, plugin_tsc_version))

# Update the version in setting file so we don't show this
# message twice.
settings.set("typescript_plugin_tsc_version", plugin_tsc_version)
sublime.save_settings("Preferences.sublime-settings")

except Exception as error:
log.error(error)

def _get_plugin_tsc_version():
cmd = [get_node_path(), TSC_PATH, "-v"]
return _execute_cmd_and_parse_version_from_output(cmd)

def _is_executable(path):
return os.path.isfile(path) and os.access(path, os.X_OK)

def _get_npm_tsc_version():
if os.name != 'nt' and _is_executable("/usr/local/bin/tsc"): # Default location on MacOS
cmd = [get_node_path(), "/usr/local/bin/tsc", "-v"]
else:
cmd = ["tsc", "-v"]
return _execute_cmd_and_parse_version_from_output(cmd)

def _execute_cmd_and_parse_version_from_output(cmd):
if os.name != 'nt': # Linux/MacOS
cmd = "'" + "' '".join(cmd) + "'"
output = subprocess.check_output(cmd, shell=True).decode('UTF-8')

# Use regex to parse the verion number from <output> e.g. parse
# "1.5.0-beta" from "message TS6029: Version 1.5.0-beta\r\n".
match_object = re.search("Version\s*([\w.-]+)", output, re.IGNORECASE)
if match_object is None:
raise Exception("Cannot parse version number from ouput: '{0}'".format(output))
return match_object.groups()[0]
4 changes: 2 additions & 2 deletions typescript/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run(self):
if "configFileName" in project_info["body"]:
tsconfig_dir = dirname(project_info["body"]["configFileName"])
self.window.run_command("exec", {
"cmd": [get_node_path(), TSC_PATH, "-p", tsconfig_dir],
"cmd": [get_node_path(), get_tsc_path(), "-p", tsconfig_dir],
# regex to capture build result for displaying in the output panel
"file_regex": "^(.+?)\\((\\d+),(\\d+)\\): (.+)$"
})
Expand All @@ -32,7 +32,7 @@ def run(self):
)

def compile_inferred_project(self, file_name, params=""):
cmd = [get_node_path(), TSC_PATH, file_name]
cmd = [get_node_path(), get_tsc_path(), file_name]
print(cmd)
if params != "":
cmd.extend(params.split(' '))
Expand Down
10 changes: 8 additions & 2 deletions typescript/libs/editor_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .node_client import ServerClient, WorkerClient
from .service_proxy import ServiceProxy
from .global_vars import *
from . import global_vars


class ClientFileInfo:
Expand Down Expand Up @@ -49,11 +50,16 @@ def initialize(self):
# retrieve the path to tsserver.js
# first see if user set the path to the file
settings = sublime.load_settings('Preferences.sublime-settings')
proc_file = settings.get('typescript_proc_file')
if not proc_file:
tsdk_location = settings.get('typescript_tsdk')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double quotes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if tsdk_location:
proc_file = os.path.join(tsdk_location, "tsserver.js")
global_vars._tsc_path = os.path.join(tsdk_location, "tsc.js")
else:
# otherwise, get tsserver.js from package directory
proc_file = os.path.join(PLUGIN_DIR, "tsserver", "tsserver.js")
global_vars._tsc_path = os.path.join(PLUGIN_DIR, "tsserver", "tsc.js")
print("Path of tsserver.js: " + proc_file)
print("Path of tsc.js: " + get_tsc_path())

self.node_client = ServerClient(proc_file)
self.worker_client = WorkerClient(proc_file)
Expand Down
4 changes: 3 additions & 1 deletion typescript/libs/global_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
def get_node_path():
return _node_path

TSC_PATH = os.path.join(PLUGIN_DIR, "tsserver", "tsc.js")
_tsc_path = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a comment above that says where _node_path gets initialized. Can you add a similar one here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

def get_tsc_path():
return _tsc_path

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