2023-05-01 18:35:23 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use crate::args::CliOptions;
|
|
|
|
use crate::args::DenoSubcommand;
|
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::Lockfile;
|
2023-05-11 00:06:59 +00:00
|
|
|
use crate::args::PackageJsonDepsProvider;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::args::StorageKeyResolver;
|
|
|
|
use crate::args::TsConfigType;
|
|
|
|
use crate::cache::Caches;
|
|
|
|
use crate::cache::DenoDir;
|
2023-05-25 18:27:45 +00:00
|
|
|
use crate::cache::DenoDirProvider;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::cache::EmitCache;
|
2023-08-02 00:49:09 +00:00
|
|
|
use crate::cache::GlobalHttpCache;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::cache::HttpCache;
|
2023-08-02 00:49:09 +00:00
|
|
|
use crate::cache::LocalHttpCache;
|
2023-10-25 22:13:22 +00:00
|
|
|
use crate::cache::ModuleInfoCache;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
|
|
|
use crate::cache::ParsedSourceCache;
|
|
|
|
use crate::emit::Emitter;
|
|
|
|
use crate::file_fetcher::FileFetcher;
|
2023-06-14 22:29:19 +00:00
|
|
|
use crate::graph_util::FileWatcherReporter;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::graph_util::ModuleGraphBuilder;
|
|
|
|
use crate::graph_util::ModuleGraphContainer;
|
|
|
|
use crate::http_util::HttpClient;
|
|
|
|
use crate::module_loader::CjsResolutionStore;
|
|
|
|
use crate::module_loader::CliModuleLoaderFactory;
|
2023-10-03 23:05:06 +00:00
|
|
|
use crate::module_loader::CliNodeResolver;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::module_loader::ModuleLoadPreparer;
|
|
|
|
use crate::module_loader::NpmModuleLoader;
|
2023-07-19 08:30:04 +00:00
|
|
|
use crate::node::CliCjsCodeAnalyzer;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::node::CliNodeCodeTranslator;
|
2023-10-02 21:53:55 +00:00
|
|
|
use crate::npm::create_cli_npm_resolver;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::npm::CliNpmResolver;
|
2023-10-25 18:39:00 +00:00
|
|
|
use crate::npm::CliNpmResolverByonmCreateOptions;
|
2023-10-02 21:53:55 +00:00
|
|
|
use crate::npm::CliNpmResolverCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedPackageJsonInstallerOption;
|
|
|
|
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::resolver::CliGraphResolver;
|
2023-08-17 16:14:22 +00:00
|
|
|
use crate::resolver::CliGraphResolverOptions;
|
2023-05-11 00:06:59 +00:00
|
|
|
use crate::standalone::DenoCompileBinaryWriter;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::tools::check::TypeChecker;
|
2023-10-19 05:05:00 +00:00
|
|
|
use crate::util::file_watcher::WatcherCommunicator;
|
2023-11-29 14:32:23 +00:00
|
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
|
|
|
use crate::worker::CliMainWorkerFactory;
|
|
|
|
use crate::worker::CliMainWorkerOptions;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::parking_lot::Mutex;
|
2023-10-12 15:55:50 +00:00
|
|
|
use deno_core::FeatureChecker;
|
2023-05-01 18:35:23 +00:00
|
|
|
|
2023-06-06 21:07:46 +00:00
|
|
|
use deno_graph::GraphKind;
|
2023-05-04 18:28:42 +00:00
|
|
|
use deno_runtime::deno_fs;
|
2023-05-01 18:35:23 +00:00
|
|
|
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
|
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2023-05-01 20:42:05 +00:00
|
|
|
use deno_runtime::deno_tls::RootCertStoreProvider;
|
2023-05-01 18:35:23 +00:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
|
|
|
use deno_runtime::inspector_server::InspectorServer;
|
|
|
|
use import_map::ImportMap;
|
|
|
|
use log::warn;
|
|
|
|
use std::future::Future;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct CliFactoryBuilder {
|
2023-10-31 00:25:58 +00:00
|
|
|
watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliFactoryBuilder {
|
|
|
|
pub fn new() -> Self {
|
2023-10-19 05:05:00 +00:00
|
|
|
Self {
|
|
|
|
watcher_communicator: None,
|
|
|
|
}
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn build_from_flags(
|
|
|
|
self,
|
|
|
|
flags: Flags,
|
|
|
|
) -> Result<CliFactory, AnyError> {
|
|
|
|
Ok(self.build_from_cli_options(Arc::new(CliOptions::from_flags(flags)?)))
|
|
|
|
}
|
|
|
|
|
2023-10-19 05:05:00 +00:00
|
|
|
pub async fn build_from_flags_for_watcher(
|
|
|
|
mut self,
|
|
|
|
flags: Flags,
|
2023-10-31 00:25:58 +00:00
|
|
|
watcher_communicator: Arc<WatcherCommunicator>,
|
2023-10-19 05:05:00 +00:00
|
|
|
) -> Result<CliFactory, AnyError> {
|
|
|
|
self.watcher_communicator = Some(watcher_communicator);
|
|
|
|
self.build_from_flags(flags).await
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
pub fn build_from_cli_options(self, options: Arc<CliOptions>) -> CliFactory {
|
|
|
|
CliFactory {
|
2023-10-19 05:05:00 +00:00
|
|
|
watcher_communicator: self.watcher_communicator,
|
2023-05-01 18:35:23 +00:00
|
|
|
options,
|
|
|
|
services: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Deferred<T>(once_cell::unsync::OnceCell<T>);
|
|
|
|
|
|
|
|
impl<T> Default for Deferred<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(once_cell::unsync::OnceCell::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deferred<T> {
|
|
|
|
pub fn get_or_try_init(
|
|
|
|
&self,
|
|
|
|
create: impl FnOnce() -> Result<T, AnyError>,
|
|
|
|
) -> Result<&T, AnyError> {
|
|
|
|
self.0.get_or_try_init(create)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_or_init(&self, create: impl FnOnce() -> T) -> &T {
|
|
|
|
self.0.get_or_init(create)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_or_try_init_async(
|
|
|
|
&self,
|
|
|
|
create: impl Future<Output = Result<T, AnyError>>,
|
|
|
|
) -> Result<&T, AnyError> {
|
|
|
|
if self.0.get().is_none() {
|
|
|
|
// todo(dsherret): it would be more ideal if this enforced a
|
|
|
|
// single executor and then we could make some initialization
|
|
|
|
// concurrent
|
|
|
|
let val = create.await?;
|
|
|
|
_ = self.0.set(val);
|
|
|
|
}
|
|
|
|
Ok(self.0.get().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct CliFactoryServices {
|
2023-05-25 18:27:45 +00:00
|
|
|
deno_dir_provider: Deferred<Arc<DenoDirProvider>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
caches: Deferred<Arc<Caches>>,
|
|
|
|
file_fetcher: Deferred<Arc<FileFetcher>>,
|
2023-08-02 00:49:09 +00:00
|
|
|
global_http_cache: Deferred<Arc<GlobalHttpCache>>,
|
|
|
|
http_cache: Deferred<Arc<dyn HttpCache>>,
|
2023-05-01 20:42:05 +00:00
|
|
|
http_client: Deferred<Arc<HttpClient>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
emit_cache: Deferred<EmitCache>,
|
|
|
|
emitter: Deferred<Arc<Emitter>>,
|
2023-05-05 16:44:24 +00:00
|
|
|
fs: Deferred<Arc<dyn deno_fs::FileSystem>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
graph_container: Deferred<Arc<ModuleGraphContainer>>,
|
|
|
|
lockfile: Deferred<Option<Arc<Mutex<Lockfile>>>>,
|
|
|
|
maybe_import_map: Deferred<Option<Arc<ImportMap>>>,
|
|
|
|
maybe_inspector_server: Deferred<Option<Arc<InspectorServer>>>,
|
2023-05-01 20:42:05 +00:00
|
|
|
root_cert_store_provider: Deferred<Arc<dyn RootCertStoreProvider>>,
|
2023-07-01 22:52:30 +00:00
|
|
|
blob_store: Deferred<Arc<BlobStore>>,
|
2023-10-25 22:13:22 +00:00
|
|
|
module_info_cache: Deferred<Arc<ModuleInfoCache>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
parsed_source_cache: Deferred<Arc<ParsedSourceCache>>,
|
|
|
|
resolver: Deferred<Arc<CliGraphResolver>>,
|
|
|
|
maybe_file_watcher_reporter: Deferred<Option<FileWatcherReporter>>,
|
|
|
|
module_graph_builder: Deferred<Arc<ModuleGraphBuilder>>,
|
|
|
|
module_load_preparer: Deferred<Arc<ModuleLoadPreparer>>,
|
|
|
|
node_code_translator: Deferred<Arc<CliNodeCodeTranslator>>,
|
|
|
|
node_resolver: Deferred<Arc<NodeResolver>>,
|
2023-09-29 13:26:25 +00:00
|
|
|
npm_resolver: Deferred<Arc<dyn CliNpmResolver>>,
|
2023-05-11 00:06:59 +00:00
|
|
|
package_json_deps_provider: Deferred<Arc<PackageJsonDepsProvider>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
text_only_progress_bar: Deferred<ProgressBar>,
|
|
|
|
type_checker: Deferred<Arc<TypeChecker>>,
|
|
|
|
cjs_resolutions: Deferred<Arc<CjsResolutionStore>>,
|
2023-10-03 23:05:06 +00:00
|
|
|
cli_node_resolver: Deferred<Arc<CliNodeResolver>>,
|
2023-10-12 15:55:50 +00:00
|
|
|
feature_checker: Deferred<Arc<FeatureChecker>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CliFactory {
|
2023-10-31 00:25:58 +00:00
|
|
|
watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-05-01 18:35:23 +00:00
|
|
|
options: Arc<CliOptions>,
|
|
|
|
services: CliFactoryServices,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CliFactory {
|
|
|
|
pub async fn from_flags(flags: Flags) -> Result<Self, AnyError> {
|
|
|
|
CliFactoryBuilder::new().build_from_flags(flags).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_cli_options(options: Arc<CliOptions>) -> Self {
|
|
|
|
CliFactoryBuilder::new().build_from_cli_options(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cli_options(&self) -> &Arc<CliOptions> {
|
|
|
|
&self.options
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:27:45 +00:00
|
|
|
pub fn deno_dir_provider(&self) -> &Arc<DenoDirProvider> {
|
|
|
|
self.services.deno_dir_provider.get_or_init(|| {
|
|
|
|
Arc::new(DenoDirProvider::new(
|
|
|
|
self.options.maybe_custom_root().clone(),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
pub fn deno_dir(&self) -> Result<&DenoDir, AnyError> {
|
2023-05-25 18:27:45 +00:00
|
|
|
Ok(self.deno_dir_provider().get_or_create()?)
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn caches(&self) -> Result<&Arc<Caches>, AnyError> {
|
|
|
|
self.services.caches.get_or_try_init(|| {
|
2023-05-25 18:27:45 +00:00
|
|
|
let caches = Arc::new(Caches::new(self.deno_dir_provider().clone()));
|
2023-05-01 18:35:23 +00:00
|
|
|
// Warm up the caches we know we'll likely need based on the CLI mode
|
|
|
|
match self.options.sub_command() {
|
|
|
|
DenoSubcommand::Run(_) => {
|
|
|
|
_ = caches.dep_analysis_db();
|
|
|
|
_ = caches.node_analysis_db();
|
|
|
|
}
|
|
|
|
DenoSubcommand::Check(_) => {
|
|
|
|
_ = caches.dep_analysis_db();
|
|
|
|
_ = caches.node_analysis_db();
|
|
|
|
_ = caches.type_checking_cache_db();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
Ok(caches)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-01 22:52:30 +00:00
|
|
|
pub fn blob_store(&self) -> &Arc<BlobStore> {
|
|
|
|
self.services.blob_store.get_or_init(Default::default)
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 20:42:05 +00:00
|
|
|
pub fn root_cert_store_provider(&self) -> &Arc<dyn RootCertStoreProvider> {
|
2023-05-01 18:35:23 +00:00
|
|
|
self
|
|
|
|
.services
|
2023-05-01 20:42:05 +00:00
|
|
|
.root_cert_store_provider
|
|
|
|
.get_or_init(|| self.options.resolve_root_cert_store_provider())
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text_only_progress_bar(&self) -> &ProgressBar {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.text_only_progress_bar
|
|
|
|
.get_or_init(|| ProgressBar::new(ProgressBarStyle::TextOnly))
|
|
|
|
}
|
|
|
|
|
2023-08-02 00:49:09 +00:00
|
|
|
pub fn global_http_cache(&self) -> Result<&Arc<GlobalHttpCache>, AnyError> {
|
|
|
|
self.services.global_http_cache.get_or_try_init(|| {
|
|
|
|
Ok(Arc::new(GlobalHttpCache::new(
|
|
|
|
self.deno_dir()?.deps_folder_path(),
|
2023-08-08 14:23:02 +00:00
|
|
|
crate::cache::RealDenoCacheEnv,
|
2023-08-02 00:49:09 +00:00
|
|
|
)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn http_cache(&self) -> Result<&Arc<dyn HttpCache>, AnyError> {
|
|
|
|
self.services.http_cache.get_or_try_init(|| {
|
|
|
|
let global_cache = self.global_http_cache()?.clone();
|
2023-08-07 01:56:56 +00:00
|
|
|
match self.options.vendor_dir_path() {
|
2023-08-02 00:49:09 +00:00
|
|
|
Some(local_path) => {
|
|
|
|
let local_cache =
|
|
|
|
LocalHttpCache::new(local_path.clone(), global_cache);
|
|
|
|
Ok(Arc::new(local_cache))
|
|
|
|
}
|
|
|
|
None => Ok(global_cache),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-01 20:42:05 +00:00
|
|
|
pub fn http_client(&self) -> &Arc<HttpClient> {
|
|
|
|
self.services.http_client.get_or_init(|| {
|
|
|
|
Arc::new(HttpClient::new(
|
|
|
|
Some(self.root_cert_store_provider().clone()),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.options.unsafely_ignore_certificate_errors().clone(),
|
2023-05-01 20:42:05 +00:00
|
|
|
))
|
2023-05-01 18:35:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_fetcher(&self) -> Result<&Arc<FileFetcher>, AnyError> {
|
|
|
|
self.services.file_fetcher.get_or_try_init(|| {
|
|
|
|
Ok(Arc::new(FileFetcher::new(
|
2023-08-02 00:49:09 +00:00
|
|
|
self.http_cache()?.clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.options.cache_setting(),
|
|
|
|
!self.options.no_remote(),
|
2023-05-01 20:42:05 +00:00
|
|
|
self.http_client().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.blob_store().clone(),
|
|
|
|
Some(self.text_only_progress_bar().clone()),
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-05 16:44:24 +00:00
|
|
|
pub fn fs(&self) -> &Arc<dyn deno_fs::FileSystem> {
|
|
|
|
self.services.fs.get_or_init(|| Arc::new(deno_fs::RealFs))
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
pub fn maybe_lockfile(&self) -> &Option<Arc<Mutex<Lockfile>>> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.lockfile
|
|
|
|
.get_or_init(|| self.options.maybe_lockfile())
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:26:25 +00:00
|
|
|
pub async fn npm_resolver(
|
|
|
|
&self,
|
|
|
|
) -> Result<&Arc<dyn CliNpmResolver>, AnyError> {
|
2023-05-01 18:35:23 +00:00
|
|
|
self
|
|
|
|
.services
|
|
|
|
.npm_resolver
|
|
|
|
.get_or_try_init_async(async {
|
2023-10-03 23:05:06 +00:00
|
|
|
let fs = self.fs();
|
2023-10-25 18:39:00 +00:00
|
|
|
create_cli_npm_resolver(if self.options.unstable_byonm() {
|
|
|
|
CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions {
|
|
|
|
fs: fs.clone(),
|
2023-11-29 14:32:23 +00:00
|
|
|
root_node_modules_dir: match self.options.node_modules_dir_path().clone() {
|
|
|
|
Some(node_modules_path) => node_modules_path,
|
|
|
|
// path needs to be canonicalized for node resolution
|
|
|
|
// (node_modules_dir_path above is already canonicalized)
|
|
|
|
None => canonicalize_path_maybe_not_exists(self.options.initial_cwd())?
|
|
|
|
.join("node_modules"),
|
|
|
|
},
|
2023-10-25 18:39:00 +00:00
|
|
|
})
|
|
|
|
} else {
|
2023-10-03 23:05:06 +00:00
|
|
|
CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions {
|
|
|
|
snapshot: match self.options.resolve_npm_resolution_snapshot()? {
|
|
|
|
Some(snapshot) => {
|
|
|
|
CliNpmResolverManagedSnapshotOption::Specified(Some(snapshot))
|
2023-10-02 21:53:55 +00:00
|
|
|
}
|
2023-10-03 23:05:06 +00:00
|
|
|
None => match self.maybe_lockfile() {
|
|
|
|
Some(lockfile) => {
|
|
|
|
CliNpmResolverManagedSnapshotOption::ResolveFromLockfile(
|
|
|
|
lockfile.clone(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => CliNpmResolverManagedSnapshotOption::Specified(None),
|
|
|
|
},
|
2023-10-02 21:53:55 +00:00
|
|
|
},
|
2023-10-03 23:05:06 +00:00
|
|
|
maybe_lockfile: self.maybe_lockfile().as_ref().cloned(),
|
|
|
|
fs: fs.clone(),
|
|
|
|
http_client: self.http_client().clone(),
|
|
|
|
npm_global_cache_dir: self.deno_dir()?.npm_folder_path(),
|
|
|
|
cache_setting: self.options.cache_setting(),
|
|
|
|
text_only_progress_bar: self.text_only_progress_bar().clone(),
|
|
|
|
maybe_node_modules_path: self.options.node_modules_dir_path(),
|
|
|
|
package_json_installer:
|
|
|
|
CliNpmResolverManagedPackageJsonInstallerOption::ConditionalInstall(
|
|
|
|
self.package_json_deps_provider().clone(),
|
|
|
|
),
|
|
|
|
npm_system_info: self.options.npm_system_info(),
|
|
|
|
npm_registry_url: crate::args::npm_registry_default_url().to_owned(),
|
|
|
|
})
|
2023-10-25 18:39:00 +00:00
|
|
|
}).await
|
2023-05-01 18:35:23 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-05-11 00:06:59 +00:00
|
|
|
pub fn package_json_deps_provider(&self) -> &Arc<PackageJsonDepsProvider> {
|
|
|
|
self.services.package_json_deps_provider.get_or_init(|| {
|
|
|
|
Arc::new(PackageJsonDepsProvider::new(
|
|
|
|
self.options.maybe_package_json_deps(),
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
pub async fn maybe_import_map(
|
|
|
|
&self,
|
|
|
|
) -> Result<&Option<Arc<ImportMap>>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.maybe_import_map
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.options
|
|
|
|
.resolve_import_map(self.file_fetcher()?)
|
|
|
|
.await?
|
|
|
|
.map(Arc::new),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn resolver(&self) -> Result<&Arc<CliGraphResolver>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.resolver
|
|
|
|
.get_or_try_init_async(async {
|
2023-10-25 18:39:00 +00:00
|
|
|
Ok(Arc::new(CliGraphResolver::new(CliGraphResolverOptions {
|
|
|
|
fs: self.fs().clone(),
|
|
|
|
cjs_resolutions: Some(self.cjs_resolutions().clone()),
|
|
|
|
node_resolver: Some(self.node_resolver().await?.clone()),
|
|
|
|
npm_resolver: if self.options.no_npm() {
|
2023-09-30 16:06:38 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.npm_resolver().await?.clone())
|
|
|
|
},
|
2023-10-25 18:39:00 +00:00
|
|
|
package_json_deps_provider: self.package_json_deps_provider().clone(),
|
|
|
|
maybe_jsx_import_source_config: self
|
|
|
|
.options
|
|
|
|
.to_maybe_jsx_import_source_config()?,
|
|
|
|
maybe_import_map: self.maybe_import_map().await?.clone(),
|
|
|
|
maybe_vendor_dir: self.options.vendor_dir_path(),
|
|
|
|
bare_node_builtins_enabled: self
|
|
|
|
.options
|
2023-11-01 23:04:54 +00:00
|
|
|
.unstable_bare_node_builtins(),
|
2023-10-25 18:39:00 +00:00
|
|
|
})))
|
2023-05-01 18:35:23 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_file_watcher_reporter(&self) -> &Option<FileWatcherReporter> {
|
2023-10-19 05:05:00 +00:00
|
|
|
let maybe_file_watcher_reporter = self
|
|
|
|
.watcher_communicator
|
|
|
|
.as_ref()
|
|
|
|
.map(|i| FileWatcherReporter::new(i.clone()));
|
2023-05-01 18:35:23 +00:00
|
|
|
self
|
|
|
|
.services
|
|
|
|
.maybe_file_watcher_reporter
|
2023-10-19 05:05:00 +00:00
|
|
|
.get_or_init(|| maybe_file_watcher_reporter)
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn emit_cache(&self) -> Result<&EmitCache, AnyError> {
|
|
|
|
self.services.emit_cache.get_or_try_init(|| {
|
|
|
|
Ok(EmitCache::new(self.deno_dir()?.gen_cache.clone()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-25 22:13:22 +00:00
|
|
|
pub fn module_info_cache(&self) -> Result<&Arc<ModuleInfoCache>, AnyError> {
|
|
|
|
self.services.module_info_cache.get_or_try_init(|| {
|
|
|
|
Ok(Arc::new(ModuleInfoCache::new(
|
2023-05-01 18:35:23 +00:00
|
|
|
self.caches()?.dep_analysis_db(),
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-25 22:13:22 +00:00
|
|
|
pub fn parsed_source_cache(&self) -> &Arc<ParsedSourceCache> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.parsed_source_cache
|
|
|
|
.get_or_init(Default::default)
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
pub fn emitter(&self) -> Result<&Arc<Emitter>, AnyError> {
|
|
|
|
self.services.emitter.get_or_try_init(|| {
|
|
|
|
let ts_config_result = self
|
|
|
|
.options
|
|
|
|
.resolve_ts_config_for_emit(TsConfigType::Emit)?;
|
|
|
|
if let Some(ignored_options) = ts_config_result.maybe_ignored_options {
|
|
|
|
warn!("{}", ignored_options);
|
|
|
|
}
|
2023-08-24 09:21:34 +00:00
|
|
|
let emit_options =
|
|
|
|
crate::args::ts_config_to_emit_options(ts_config_result.ts_config);
|
2023-05-01 18:35:23 +00:00
|
|
|
Ok(Arc::new(Emitter::new(
|
|
|
|
self.emit_cache()?.clone(),
|
2023-10-25 22:13:22 +00:00
|
|
|
self.parsed_source_cache().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
emit_options,
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn node_resolver(&self) -> Result<&Arc<NodeResolver>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.node_resolver
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
Ok(Arc::new(NodeResolver::new(
|
2023-05-05 16:44:24 +00:00
|
|
|
self.fs().clone(),
|
2023-09-29 13:26:25 +00:00
|
|
|
self.npm_resolver().await?.clone().into_npm_resolver(),
|
2023-05-01 18:35:23 +00:00
|
|
|
)))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn node_code_translator(
|
|
|
|
&self,
|
|
|
|
) -> Result<&Arc<CliNodeCodeTranslator>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.node_code_translator
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
let caches = self.caches()?;
|
|
|
|
let node_analysis_cache =
|
|
|
|
NodeAnalysisCache::new(caches.node_analysis_db());
|
2023-07-24 19:35:13 +00:00
|
|
|
let cjs_esm_analyzer =
|
|
|
|
CliCjsCodeAnalyzer::new(node_analysis_cache, self.fs().clone());
|
2023-05-01 18:35:23 +00:00
|
|
|
|
|
|
|
Ok(Arc::new(NodeCodeTranslator::new(
|
|
|
|
cjs_esm_analyzer,
|
2023-05-05 16:44:24 +00:00
|
|
|
self.fs().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.node_resolver().await?.clone(),
|
2023-09-29 13:26:25 +00:00
|
|
|
self.npm_resolver().await?.clone().into_npm_resolver(),
|
2023-05-01 18:35:23 +00:00
|
|
|
)))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn type_checker(&self) -> Result<&Arc<TypeChecker>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.type_checker
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
Ok(Arc::new(TypeChecker::new(
|
|
|
|
self.caches()?.clone(),
|
|
|
|
self.options.clone(),
|
|
|
|
self.node_resolver().await?.clone(),
|
|
|
|
self.npm_resolver().await?.clone(),
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn module_graph_builder(
|
|
|
|
&self,
|
|
|
|
) -> Result<&Arc<ModuleGraphBuilder>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.module_graph_builder
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
Ok(Arc::new(ModuleGraphBuilder::new(
|
|
|
|
self.options.clone(),
|
2023-12-01 20:12:10 +00:00
|
|
|
self.fs().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.resolver().await?.clone(),
|
|
|
|
self.npm_resolver().await?.clone(),
|
2023-10-25 22:13:22 +00:00
|
|
|
self.module_info_cache()?.clone(),
|
|
|
|
self.parsed_source_cache().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.maybe_lockfile().clone(),
|
2023-06-14 22:29:19 +00:00
|
|
|
self.maybe_file_watcher_reporter().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.emit_cache()?.clone(),
|
|
|
|
self.file_fetcher()?.clone(),
|
2023-08-02 00:49:09 +00:00
|
|
|
self.global_http_cache()?.clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.type_checker().await?.clone(),
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn graph_container(&self) -> &Arc<ModuleGraphContainer> {
|
2023-06-06 21:07:46 +00:00
|
|
|
self.services.graph_container.get_or_init(|| {
|
|
|
|
let graph_kind = match self.options.sub_command() {
|
2023-06-07 14:09:10 +00:00
|
|
|
// todo(dsherret): ideally the graph container would not be used
|
|
|
|
// for deno cache because it doesn't dynamically load modules
|
2023-06-06 21:07:46 +00:00
|
|
|
DenoSubcommand::Cache(_) => GraphKind::All,
|
2023-06-07 14:09:10 +00:00
|
|
|
_ => self.options.type_check_mode().as_graph_kind(),
|
2023-06-06 21:07:46 +00:00
|
|
|
};
|
|
|
|
Arc::new(ModuleGraphContainer::new(graph_kind))
|
|
|
|
})
|
2023-05-01 18:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_inspector_server(&self) -> &Option<Arc<InspectorServer>> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.maybe_inspector_server
|
|
|
|
.get_or_init(|| self.options.resolve_inspector_server().map(Arc::new))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn module_load_preparer(
|
|
|
|
&self,
|
|
|
|
) -> Result<&Arc<ModuleLoadPreparer>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.module_load_preparer
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
Ok(Arc::new(ModuleLoadPreparer::new(
|
|
|
|
self.options.clone(),
|
2023-12-01 20:12:10 +00:00
|
|
|
self.fs().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.graph_container().clone(),
|
|
|
|
self.maybe_lockfile().clone(),
|
|
|
|
self.maybe_file_watcher_reporter().clone(),
|
|
|
|
self.module_graph_builder().await?.clone(),
|
2023-10-25 22:13:22 +00:00
|
|
|
self.module_info_cache()?.clone(),
|
|
|
|
self.parsed_source_cache().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.text_only_progress_bar().clone(),
|
|
|
|
self.resolver().await?.clone(),
|
|
|
|
self.type_checker().await?.clone(),
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cjs_resolutions(&self) -> &Arc<CjsResolutionStore> {
|
|
|
|
self.services.cjs_resolutions.get_or_init(Default::default)
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:05:06 +00:00
|
|
|
pub async fn cli_node_resolver(
|
|
|
|
&self,
|
|
|
|
) -> Result<&Arc<CliNodeResolver>, AnyError> {
|
|
|
|
self
|
|
|
|
.services
|
|
|
|
.cli_node_resolver
|
|
|
|
.get_or_try_init_async(async {
|
|
|
|
Ok(Arc::new(CliNodeResolver::new(
|
|
|
|
self.cjs_resolutions().clone(),
|
|
|
|
self.node_resolver().await?.clone(),
|
|
|
|
self.npm_resolver().await?.clone(),
|
|
|
|
)))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:55:50 +00:00
|
|
|
pub fn feature_checker(&self) -> &Arc<FeatureChecker> {
|
|
|
|
self.services.feature_checker.get_or_init(|| {
|
|
|
|
let mut checker = FeatureChecker::default();
|
|
|
|
checker.set_exit_cb(Box::new(crate::unstable_exit_cb));
|
|
|
|
// TODO(bartlomieju): enable, once we deprecate `--unstable` in favor
|
|
|
|
// of granular --unstable-* flags.
|
|
|
|
// feature_checker.set_warn_cb(Box::new(crate::unstable_warn_cb));
|
|
|
|
if self.options.unstable() {
|
|
|
|
checker.enable_legacy_unstable();
|
|
|
|
}
|
2023-11-01 22:15:08 +00:00
|
|
|
let unstable_features = self.options.unstable_features();
|
|
|
|
for (flag_name, _, _) in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if unstable_features.contains(&flag_name.to_string()) {
|
|
|
|
checker.enable_feature(flag_name);
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 15:55:50 +00:00
|
|
|
|
|
|
|
Arc::new(checker)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-11 00:06:59 +00:00
|
|
|
pub async fn create_compile_binary_writer(
|
|
|
|
&self,
|
|
|
|
) -> Result<DenoCompileBinaryWriter, AnyError> {
|
|
|
|
Ok(DenoCompileBinaryWriter::new(
|
|
|
|
self.file_fetcher()?,
|
|
|
|
self.http_client(),
|
|
|
|
self.deno_dir()?,
|
2023-09-29 13:26:25 +00:00
|
|
|
self.npm_resolver().await?.as_ref(),
|
2023-05-17 21:38:50 +00:00
|
|
|
self.options.npm_system_info(),
|
2023-05-11 00:06:59 +00:00
|
|
|
self.package_json_deps_provider(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
pub async fn create_cli_main_worker_factory(
|
|
|
|
&self,
|
|
|
|
) -> Result<CliMainWorkerFactory, AnyError> {
|
|
|
|
let node_resolver = self.node_resolver().await?;
|
2023-09-28 20:43:45 +00:00
|
|
|
let npm_resolver = self.npm_resolver().await?;
|
2023-05-11 00:06:59 +00:00
|
|
|
let fs = self.fs();
|
2023-10-03 23:05:06 +00:00
|
|
|
let cli_node_resolver = self.cli_node_resolver().await?;
|
2023-10-31 00:25:58 +00:00
|
|
|
let maybe_file_watcher_communicator = if self.options.has_hmr() {
|
|
|
|
Some(self.watcher_communicator.clone().unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2023-10-19 05:05:00 +00:00
|
|
|
|
2023-05-01 18:35:23 +00:00
|
|
|
Ok(CliMainWorkerFactory::new(
|
|
|
|
StorageKeyResolver::from_options(&self.options),
|
2023-11-14 21:06:00 +00:00
|
|
|
self.options.sub_command().clone(),
|
2023-09-28 20:43:45 +00:00
|
|
|
npm_resolver.clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
node_resolver.clone(),
|
|
|
|
self.blob_store().clone(),
|
|
|
|
Box::new(CliModuleLoaderFactory::new(
|
|
|
|
&self.options,
|
|
|
|
self.emitter()?.clone(),
|
|
|
|
self.graph_container().clone(),
|
|
|
|
self.module_load_preparer().await?.clone(),
|
2023-10-25 22:13:22 +00:00
|
|
|
self.parsed_source_cache().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.resolver().await?.clone(),
|
2023-10-03 23:05:06 +00:00
|
|
|
cli_node_resolver.clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
NpmModuleLoader::new(
|
|
|
|
self.cjs_resolutions().clone(),
|
|
|
|
self.node_code_translator().await?.clone(),
|
2023-05-11 00:06:59 +00:00
|
|
|
fs.clone(),
|
2023-10-03 23:05:06 +00:00
|
|
|
cli_node_resolver.clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
),
|
|
|
|
)),
|
2023-05-01 20:42:05 +00:00
|
|
|
self.root_cert_store_provider().clone(),
|
2023-05-05 16:44:24 +00:00
|
|
|
self.fs().clone(),
|
2023-10-31 00:25:58 +00:00
|
|
|
Some(self.emitter()?.clone()),
|
|
|
|
maybe_file_watcher_communicator,
|
2023-05-01 18:35:23 +00:00
|
|
|
self.maybe_inspector_server().clone(),
|
2023-05-22 20:55:04 +00:00
|
|
|
self.maybe_lockfile().clone(),
|
2023-10-12 15:55:50 +00:00
|
|
|
self.feature_checker().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
self.create_cli_main_worker_options()?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_cli_main_worker_options(
|
|
|
|
&self,
|
|
|
|
) -> Result<CliMainWorkerOptions, AnyError> {
|
|
|
|
Ok(CliMainWorkerOptions {
|
|
|
|
argv: self.options.argv().clone(),
|
2023-11-11 17:01:48 +00:00
|
|
|
// This optimization is only available for "run" subcommand
|
|
|
|
// because we need to register new ops for testing and jupyter
|
|
|
|
// integration.
|
|
|
|
skip_op_registration: self.options.sub_command().is_run(),
|
2023-05-30 15:34:50 +00:00
|
|
|
log_level: self.options.log_level().unwrap_or(log::Level::Info).into(),
|
2023-05-01 18:35:23 +00:00
|
|
|
coverage_dir: self.options.coverage_dir(),
|
2023-11-05 21:27:36 +00:00
|
|
|
enable_op_summary_metrics: self.options.enable_op_summary_metrics(),
|
2023-05-01 18:35:23 +00:00
|
|
|
enable_testing_features: self.options.enable_testing_features(),
|
|
|
|
has_node_modules_dir: self.options.has_node_modules_dir(),
|
2023-10-31 00:25:58 +00:00
|
|
|
hmr: self.options.has_hmr(),
|
2023-05-01 18:35:23 +00:00
|
|
|
inspect_brk: self.options.inspect_brk().is_some(),
|
|
|
|
inspect_wait: self.options.inspect_wait().is_some(),
|
2023-11-10 17:41:24 +00:00
|
|
|
strace_ops: self.options.strace_ops().clone(),
|
2023-05-01 18:35:23 +00:00
|
|
|
is_inspecting: self.options.is_inspecting(),
|
|
|
|
is_npm_main: self.options.is_npm_main(),
|
|
|
|
location: self.options.location_flag().clone(),
|
2023-12-06 21:36:06 +00:00
|
|
|
// if the user ran a binary command, we'll need to set process.argv[0]
|
|
|
|
// to be the name of the binary command instead of deno
|
|
|
|
maybe_binary_npm_command_name: self
|
|
|
|
.options
|
|
|
|
.take_binary_npm_command_name(),
|
2023-05-01 18:35:23 +00:00
|
|
|
origin_data_folder_path: Some(self.deno_dir()?.origin_data_folder_path()),
|
|
|
|
seed: self.options.seed(),
|
|
|
|
unsafely_ignore_certificate_errors: self
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors()
|
|
|
|
.clone(),
|
|
|
|
unstable: self.options.unstable(),
|
2023-10-03 23:05:06 +00:00
|
|
|
maybe_root_package_json_deps: self.options.maybe_package_json_deps(),
|
2023-05-01 18:35:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|