Node.js¶
lsp_utils.NodeManager
¶
Resolves a Node.js runtime and installs servers that run on Node.js.
on_pre_start_async
classmethod
¶
on_pre_start_async(context: OnPreStartContext, plugin_storage_path: Path, server_directory_resource_path: ResourcePath, server_binary_path: Path, *, node_version_requirement: str, skip_npm_install: bool = False, on_server_installed: ServerInstalledCallback | None = None) -> Path | None
Initialize NodeManager for an LspPlugin.
Automatically adds support for a root server_path package setting that defaults to "auto", meaning
that the package-managed server instance will be used. It can be overridden to use a custom server binary.
When the lsp_utils-managed Node.js runtime is used, its binary directory is prepended to the server's PATH
so that any processes spawned by the server that call node use that same runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
OnPreStartContext
|
The plugin context. |
required |
plugin_storage_path
|
Path
|
The path to the plugin's storage ( |
required |
server_directory_resource_path
|
ResourcePath
|
The |
required |
server_binary_path
|
Path
|
The path of the file used to start the server, relative to the server's
directory that contains |
required |
node_version_requirement
|
str
|
NPM semantic version (typically a range) specifying which Node.js version is required by the server. Examples:
See also: https://semver.npmjs.com/ |
required |
on_server_installed
|
ServerInstalledCallback | None
|
Callback called when the server gets installed or updated. Gets passed the Path to the server directory (containing 'package.json'). |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the server directory (containing 'package.json') if the server is managed. The directory name includes a hash of the installation inputs so it changes when the server or Node.js is updated. |
resolve
classmethod
¶
resolve(package_name: str, required_node_version: str) -> NodeRunner
Return a Node.js runtime that satisfies the required version.
The runtimes listed in the nodejs_runtime lsp_utils setting are tried in order. The system runtime uses
Node.js found on the PATH and the local runtime uses Node.js installed by lsp_utils in the package
storage, downloading it if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package that requires Node.js. Used in messages and logs. |
required |
required_node_version
|
str
|
NPM semantic version (typically a range) specifying which Node.js version is
required. See |
required |
Returns:
| Type | Description |
|---|---|
NodeRunner
|
The resolved runtime. |
lsp_utils.NodeRunner
¶
A Node.js runtime, as returned by NodeManager.resolve.
install_node
¶
Install this Node.js runtime.
Raises:
| Type | Description |
|---|---|
Exception
|
If this runtime doesn't support installation, as is the case for one found on the |
additional_paths
¶
Directories that should be prepended to the PATH so that node resolves to this runtime.
node_env
¶
Return environment variables that need to be set when running node.
apply_env
¶
Update env in place with the variables needed to run this runtime's node.
Merges in node_env() and puts additional_paths() first on the PATH so that node resolves to this
runtime, also in processes spawned by the started process.
check_binary_present
¶
Check that both the node and the npm binaries were found.
Raises:
| Type | Description |
|---|---|
NodeNotInitializedError
|
If either of the binaries is missing. |
check_satisfies_version
¶
Check that the version of this runtime satisfies the requirement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_node_version
|
NpmSpec
|
The version specification to satisfy. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
If the version doesn't satisfy the requirement. |
run_node
¶
run_node(args: list[str | PathLike[str]], stdin: int = PIPE, stdout: int = PIPE, stderr: int = PIPE, env: dict[str, Any] | None = None) -> Popen[bytes]
Start node with the given arguments and return the process.
run_install
¶
Install the production dependencies of the npm project in cwd with npm ci.
npm_command
¶
Return the command prefix for running npm.
Raises:
| Type | Description |
|---|---|
NodeNotInitializedError
|
If the |
install_project_dependencies
¶
install_project_dependencies(source_path: ResourcePath, target_path: Path, *, skip_npm_install: bool = False, on_server_installed: ServerInstalledCallback | None = None) -> None
Install the server into target_path, replacing an installation with outdated dependencies.
Deprecated: use install_versioned_project_dependencies instead. Replacing the files of a server
that might still be running fails on Windows if the files are locked.
install_versioned_project_dependencies
¶
install_versioned_project_dependencies(source_path: ResourcePath, parent_path: Path, *, skip_npm_install: bool = False, on_server_installed: ServerInstalledCallback | None = None) -> Path
Install the server into a <parent_path>/<source directory name>-<hash> directory.
The hash depends on package.json, package-lock.json and the Node.js version so an update installs into a
new directory instead of replacing files of a server that might still be running.
Returns:
| Type | Description |
|---|---|
Path
|
The path to the server directory. |