diff --git a/LICENSE b/LICENSE index 67da40e..de7807f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2021 +Copyright (c) 2022 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 14fc7e9..48c1e7d 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ Insert space after opening and before closing non empty braces. Sometimes sublime cannot find node executable, if it happens. Set `node_bin` explicitly (e.g. c:/nodejs/node.exe) - Type: `string` -- Default: 'node' (auto detect) +- Default: '' (auto detect) #### `import_path_mapping` diff --git a/import_helper.py b/import_helper.py index 7d488de..228ba20 100644 --- a/import_helper.py +++ b/import_helper.py @@ -6,14 +6,14 @@ PACKAGE_PATH = os.path.dirname(os.path.realpath(__file__)) RUN_PATH = os.path.join(PACKAGE_PATH, "backend_run.js") NODE_BIN = "node" -NODE_MODULES = [] # Collection of entries +# Collection of entries +NODE_MODULES = [] SOURCE_MODULES = [] TYPESCRIPT_PATHS = [] from .library.utils import get_time, status_message from .library.get_setting import get_setting from .library.debug import debug -from .library.find_executable import find_executable from .library.update_source_modules import update_source_modules from .library.update_node_modules import update_node_modules from .library.list_imports_command import list_imports_command @@ -22,36 +22,31 @@ from .library.paste_import_command import paste_import_command from .library.update_typescript_paths import update_typescript_paths from .library.query_completions_modules import query_completions_modules -from .library.exec_command import exec +from .library.get_node_executable import get_node_executable +from .library.exec_command import exec_sync def plugin_loaded(): print() debug("Plugin loaded", PROJECT_NAME) - sublime.set_timeout(initialize, 0) sublime.set_timeout(setup, 0) -def initialize(): - global NODE_BIN - if NODE_BIN == "node" or not bool(NODE_BIN): - NODE_BIN = get_setting("node_bin", "") - if not bool(NODE_BIN): - NODE_BIN = find_executable("node") - if not bool(NODE_BIN): - NODE_BIN = "node" - (err, out) = exec([NODE_BIN, "--version"], None) - version = float(".".join(out[1:].split(".")[0:2])) - if version < 10: - status_message("Node.js version is {0}, but 12+ is required".format(version)) - - def setup(): + check_node() update_source_modules(SOURCE_MODULES) update_node_modules(NODE_MODULES) update_typescript_paths(TYPESCRIPT_PATHS) +def check_node(): + node_executable = get_node_executable() + (err, out) = exec_sync([node_executable, "--version"], None) + version = float(".".join(out[1:].split(".")[0:2])) + if version < 12: + status_message("Node.js version is {0}, but 12+ is required".format(version)) + + # window.run_command('update_source_modules') class UpdateSourceModulesCommand(sublime_plugin.WindowCommand): def run(self): diff --git a/import_helper.sublime-settings b/import_helper.sublime-settings index 90e7a10..3a1978c 100644 --- a/import_helper.sublime-settings +++ b/import_helper.sublime-settings @@ -16,8 +16,8 @@ // Sometimes sublime cannot find node executable, if it happens. Set `node_bin` explicitly (e.g. c:/nodejs/node.exe) // - Type: `string` - // - Default: `node` (auto detect) - "node_bin": "node", + // - Default: `` (auto detect) + "node_bin": "", // How to apply path mapping (read more about [Module Resolution and Path Mapping](http://www.typescriptlang.org/docs/handbook/module-resolution.html)). // Disabled by default (`disabled`). diff --git a/library/exec_command.py b/library/exec_command.py index e25bb91..703219e 100644 --- a/library/exec_command.py +++ b/library/exec_command.py @@ -4,17 +4,24 @@ import threading import traceback -from ..import_helper import RUN_PATH, NODE_BIN, PACKAGE_PATH +from ..import_helper import RUN_PATH, PACKAGE_PATH from .debug import debug +from .get_node_executable import get_node_executable +from .utils import error_message, status_message + +NODE_BIN = None def run_command(command, data=None, callback=None): + global NODE_BIN + if not NODE_BIN: + NODE_BIN = get_node_executable() debug("run_command", [NODE_BIN, command, data]) json = sublime.encode_value({"command": command, "args": data}) err = None out = None try: - (err, out) = exec([NODE_BIN, "--no-warnings", RUN_PATH], json) + (err, out) = exec_sync([NODE_BIN, "--no-warnings", RUN_PATH], json) except Exception as e: err = traceback.format_exc() if bool(err): @@ -33,7 +40,7 @@ def run_command_async(command, data=None, callback=None): thread.start() -def exec(cmd, input): +def exec_sync(cmd, input): if os.name == "nt": si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.SW_HIDE | subprocess.STARTF_USESHOWWINDOW diff --git a/library/get_node_executable.py b/library/get_node_executable.py new file mode 100644 index 0000000..42f20a8 --- /dev/null +++ b/library/get_node_executable.py @@ -0,0 +1,12 @@ +from .get_setting import get_setting +from .find_executable import find_executable + + +def get_node_executable(): + node_executable = get_setting("node_bin", "") + if not bool(node_executable): + node_executable = find_executable("node") + if not bool(node_executable): + node_executable = "node" + + return node_executable diff --git a/library/paste_import_command.py b/library/paste_import_command.py index 096eebe..4f4dda0 100644 --- a/library/paste_import_command.py +++ b/library/paste_import_command.py @@ -46,7 +46,9 @@ def on_select(selected): "manipulationSettings": { "quoteKind": get_setting("from_quote", "'", settings), "noSemicolon": get_setting("no_semicolon", False, settings), - "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": get_setting("insert_space_in_braces", True, settings) + "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": get_setting( + "insert_space_in_braces", True, settings + ), }, "sorted": True, },