Skip to content

Commit

Permalink
fix: Improve detect node executable
Browse files Browse the repository at this point in the history
close: #107
  • Loading branch information
Roman_Vasilev committed May 4, 2022
1 parent 30333f4 commit a10dcca
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
31 changes: 13 additions & 18 deletions import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions import_helper.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
13 changes: 10 additions & 3 deletions library/exec_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions library/get_node_executable.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion library/paste_import_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit a10dcca

Please sign in to comment.