From c50469726095e2c7fb93d489146bea4b510029b6 Mon Sep 17 00:00:00 2001 From: Predrag Date: Sun, 15 Dec 2019 23:33:30 +0100 Subject: [PATCH] Revert "Eslint server already comes bundled in the vscode-eslint folder, so there is no need to run npm install in the plugin_loaded hook" because Predrag obviously didn't know what he was doing This reverts commit 6f47bed358da4597cab772e80efdf7d51ed0a2b1. --- plugin.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index dcc9a45..5f76cac 100644 --- a/plugin.py +++ b/plugin.py @@ -1,6 +1,8 @@ import os import shutil import sublime +import subprocess +import threading import webbrowser from LSP.plugin.core.handlers import LanguageHandler @@ -11,16 +13,62 @@ package_path = os.path.dirname(__file__) server_path = os.path.join(package_path, 'vscode-eslint', 'out', 'eslintServer.js') +vscode_eslint_path = os.path.join(package_path, 'vscode-eslint') +node_modules_path = os.path.join(vscode_eslint_path, 'node_modules') def plugin_loaded(): - print('LSP-eslint: Server is installed.') + dependencies_insalled = os.path.isdir(node_modules_path) + print('LSP-eslint: Server {} installed.'.format('is' if dependencies_insalled else 'is not')) + + if not dependencies_insalled: + # this will be called only when the plugin gets: + # - installed for the first time, + # - or when updated on package control + logAndShowMessage('LSP-eslint: Installing server.') + + runCommand( + onCommandDone, + ["npm", "install", "--verbose", "--prefix", vscode_eslint_path, vscode_eslint_path] + ) + + +def onCommandDone(): + logAndShowMessage('LSP-eslint: Server installed.') + + +def runCommand(onExit, popenArgs): + """ + Runs the given args in a subprocess.Popen, and then calls the function + onExit when the subprocess completes. + onExit is a callable object, and popenArgs is a list/tuple of args that + would give to subprocess.Popen. + """ + def runInThread(onExit, popenArgs): + try: + if sublime.platform() == 'windows': + subprocess.check_call(popenArgs, shell=True) + else: + subprocess.check_call(popenArgs) + onExit() + except subprocess.CalledProcessError as error: + logAndShowMessage('LSP-eslint: Error while installing the server.', error) + return + thread = threading.Thread(target=runInThread, args=(onExit, popenArgs)) + thread.start() + # returns immediately after the thread starts + return thread def is_node_installed(): return shutil.which('node') is not None +def logAndShowMessage(msg, additional_logs=None): + print(msg, '\n', additional_logs) if additional_logs else print(msg) + sublime.active_window().status_message(msg) + + class LspEslintPlugin(LanguageHandler): @property def name(self) -> str: