mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
be97170a19
This PR adds a new unstable "bring your own node_modules" (BYONM) functionality currently behind a `--unstable-byonm` flag (`"unstable": ["byonm"]` in a deno.json). This enables users to run a separate install command (ex. `npm install`, `pnpm install`) then run `deno run main.ts` and Deno will respect the layout of the node_modules directory as setup by the separate install command. It also works with npm/yarn/pnpm workspaces. For this PR, the behaviour is opted into by specifying `--unstable-byonm`/`"unstable": ["byonm"]`, but in the future we may make this the default behaviour as outlined in https://github.com/denoland/deno/issues/18967#issuecomment-1761248941 This is an extremely rough initial implementation. Errors are terrible in this and the LSP requires frequent restarts. Improvements will be done in follow up PRs.
93 lines
2.7 KiB
Rust
93 lines
2.7 KiB
Rust
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
mod byonm;
|
|
mod cache_dir;
|
|
mod common;
|
|
mod managed;
|
|
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
use deno_core::error::AnyError;
|
|
use deno_runtime::deno_node::NpmResolver;
|
|
use deno_semver::package::PackageReq;
|
|
|
|
pub use self::byonm::CliNpmResolverByonmCreateOptions;
|
|
pub use self::cache_dir::NpmCacheDir;
|
|
pub use self::managed::CliNpmResolverManagedCreateOptions;
|
|
pub use self::managed::CliNpmResolverManagedPackageJsonInstallerOption;
|
|
pub use self::managed::CliNpmResolverManagedSnapshotOption;
|
|
pub use self::managed::ManagedCliNpmResolver;
|
|
|
|
use self::byonm::ByonmCliNpmResolver;
|
|
|
|
pub enum CliNpmResolverCreateOptions {
|
|
Managed(CliNpmResolverManagedCreateOptions),
|
|
Byonm(CliNpmResolverByonmCreateOptions),
|
|
}
|
|
|
|
pub async fn create_cli_npm_resolver_for_lsp(
|
|
options: CliNpmResolverCreateOptions,
|
|
) -> Arc<dyn CliNpmResolver> {
|
|
use CliNpmResolverCreateOptions::*;
|
|
match options {
|
|
Managed(options) => {
|
|
managed::create_managed_npm_resolver_for_lsp(options).await
|
|
}
|
|
Byonm(options) => byonm::create_byonm_npm_resolver(options),
|
|
}
|
|
}
|
|
|
|
pub async fn create_cli_npm_resolver(
|
|
options: CliNpmResolverCreateOptions,
|
|
) -> Result<Arc<dyn CliNpmResolver>, AnyError> {
|
|
use CliNpmResolverCreateOptions::*;
|
|
match options {
|
|
Managed(options) => managed::create_managed_npm_resolver(options).await,
|
|
Byonm(options) => Ok(byonm::create_byonm_npm_resolver(options)),
|
|
}
|
|
}
|
|
|
|
pub enum InnerCliNpmResolverRef<'a> {
|
|
Managed(&'a ManagedCliNpmResolver),
|
|
#[allow(dead_code)]
|
|
Byonm(&'a ByonmCliNpmResolver),
|
|
}
|
|
|
|
pub trait CliNpmResolver: NpmResolver {
|
|
fn into_npm_resolver(self: Arc<Self>) -> Arc<dyn NpmResolver>;
|
|
|
|
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver>;
|
|
|
|
fn as_inner(&self) -> InnerCliNpmResolverRef;
|
|
|
|
fn as_managed(&self) -> Option<&ManagedCliNpmResolver> {
|
|
match self.as_inner() {
|
|
InnerCliNpmResolverRef::Managed(inner) => Some(inner),
|
|
InnerCliNpmResolverRef::Byonm(_) => None,
|
|
}
|
|
}
|
|
|
|
fn as_byonm(&self) -> Option<&ByonmCliNpmResolver> {
|
|
match self.as_inner() {
|
|
InnerCliNpmResolverRef::Managed(_) => None,
|
|
InnerCliNpmResolverRef::Byonm(inner) => Some(inner),
|
|
}
|
|
}
|
|
|
|
fn root_node_modules_path(&self) -> Option<PathBuf>;
|
|
|
|
fn resolve_pkg_folder_from_deno_module_req(
|
|
&self,
|
|
req: &PackageReq,
|
|
referrer: &ModuleSpecifier,
|
|
) -> Result<PathBuf, AnyError>;
|
|
|
|
/// Gets the state of npm for the process.
|
|
fn get_npm_process_state(&self) -> String;
|
|
|
|
/// Returns a hash returning the state of the npm resolver
|
|
/// or `None` if the state currently can't be determined.
|
|
fn check_state_hash(&self) -> Option<u64>;
|
|
}
|