2024-01-01 19:58:21 +00:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2022-06-27 20:54:09 +00:00
2024-03-05 20:45:44 +00:00
pub mod deno_json ;
2022-11-25 22:00:28 +00:00
mod flags ;
2023-08-03 11:19:19 +00:00
mod flags_net ;
2023-01-25 20:13:40 +00:00
mod import_map ;
mod lockfile ;
2023-02-20 18:14:06 +00:00
pub mod package_json ;
2022-06-27 20:54:09 +00:00
2024-02-24 05:21:09 +00:00
pub use self ::import_map ::resolve_import_map ;
2023-03-03 22:27:05 +00:00
use self ::package_json ::PackageJsonDeps ;
2023-01-25 20:13:40 +00:00
use ::import_map ::ImportMap ;
2024-04-11 23:00:17 +00:00
use deno_ast ::SourceMapOption ;
2023-03-22 14:15:53 +00:00
use deno_core ::resolve_url_or_path ;
2023-04-13 14:47:45 +00:00
use deno_npm ::resolution ::ValidSerializedNpmResolutionSnapshot ;
2023-05-17 21:38:50 +00:00
use deno_npm ::NpmSystemInfo ;
2023-05-01 20:42:05 +00:00
use deno_runtime ::deno_tls ::RootCertStoreProvider ;
2023-04-06 22:46:44 +00:00
use deno_semver ::npm ::NpmPackageReqReference ;
2023-09-01 20:13:13 +00:00
use indexmap ::IndexMap ;
2023-02-20 18:14:06 +00:00
2024-01-16 00:15:39 +00:00
pub use deno_config ::glob ::FilePatterns ;
2023-08-24 09:21:34 +00:00
pub use deno_config ::BenchConfig ;
pub use deno_config ::ConfigFile ;
pub use deno_config ::FmtOptionsConfig ;
pub use deno_config ::JsxImportSourceConfig ;
pub use deno_config ::LintRulesConfig ;
pub use deno_config ::ProseWrap ;
pub use deno_config ::TsConfig ;
pub use deno_config ::TsConfigForEmit ;
pub use deno_config ::TsConfigType ;
pub use deno_config ::TsTypeLib ;
2023-11-17 01:28:38 +00:00
pub use deno_config ::WorkspaceConfig ;
2022-06-27 20:54:09 +00:00
pub use flags ::* ;
2022-11-25 22:00:28 +00:00
pub use lockfile ::Lockfile ;
pub use lockfile ::LockfileError ;
2023-05-11 00:06:59 +00:00
pub use package_json ::PackageJsonDepsProvider ;
2022-06-28 20:45:55 +00:00
use deno_ast ::ModuleSpecifier ;
use deno_core ::anyhow ::bail ;
use deno_core ::anyhow ::Context ;
use deno_core ::error ::AnyError ;
2022-11-02 15:32:30 +00:00
use deno_core ::parking_lot ::Mutex ;
2023-02-20 18:14:06 +00:00
use deno_core ::serde_json ;
2022-06-28 20:45:55 +00:00
use deno_core ::url ::Url ;
2023-02-20 18:14:06 +00:00
use deno_runtime ::deno_node ::PackageJson ;
2023-10-31 11:55:46 +00:00
use deno_runtime ::deno_tls ::deno_native_certs ::load_native_certs ;
2023-01-07 20:22:09 +00:00
use deno_runtime ::deno_tls ::rustls ;
2022-06-28 20:45:55 +00:00
use deno_runtime ::deno_tls ::rustls ::RootCertStore ;
2023-01-07 20:22:09 +00:00
use deno_runtime ::deno_tls ::rustls_pemfile ;
use deno_runtime ::deno_tls ::webpki_roots ;
2022-06-28 20:45:55 +00:00
use deno_runtime ::inspector_server ::InspectorServer ;
use deno_runtime ::permissions ::PermissionsOptions ;
2024-02-07 16:25:14 +00:00
use deno_terminal ::colors ;
2023-11-01 15:21:13 +00:00
use dotenvy ::from_filename ;
2023-02-20 18:14:06 +00:00
use once_cell ::sync ::Lazy ;
2023-05-01 20:42:05 +00:00
use once_cell ::sync ::OnceCell ;
2023-10-02 21:53:55 +00:00
use serde ::Deserialize ;
use serde ::Serialize ;
2023-03-22 14:15:53 +00:00
use std ::collections ::HashMap ;
2022-06-28 20:45:55 +00:00
use std ::env ;
2022-11-28 22:28:54 +00:00
use std ::io ::BufReader ;
2023-01-18 00:18:24 +00:00
use std ::io ::Cursor ;
2022-06-28 20:45:55 +00:00
use std ::net ::SocketAddr ;
2023-01-07 20:22:09 +00:00
use std ::num ::NonZeroUsize ;
2023-02-20 18:14:06 +00:00
use std ::path ::Path ;
2022-06-28 20:45:55 +00:00
use std ::path ::PathBuf ;
2022-11-02 15:32:30 +00:00
use std ::sync ::Arc ;
2023-05-01 20:42:05 +00:00
use thiserror ::Error ;
2022-06-28 20:45:55 +00:00
2023-01-25 20:13:40 +00:00
use crate ::file_fetcher ::FileFetcher ;
2022-11-28 22:28:54 +00:00
use crate ::util ::fs ::canonicalize_path_maybe_not_exists ;
2022-06-28 20:45:55 +00:00
use crate ::version ;
2024-01-16 00:15:39 +00:00
use deno_config ::glob ::PathOrPatternSet ;
2023-08-24 09:21:34 +00:00
use deno_config ::FmtConfig ;
use deno_config ::LintConfig ;
use deno_config ::TestConfig ;
2024-03-06 13:24:15 +00:00
pub fn npm_registry_url ( ) -> & 'static Url {
2023-10-25 18:39:00 +00:00
static NPM_REGISTRY_DEFAULT_URL : Lazy < Url > = Lazy ::new ( | | {
let env_var_name = " NPM_CONFIG_REGISTRY " ;
if let Ok ( registry_url ) = std ::env ::var ( env_var_name ) {
// ensure there is a trailing slash for the directory
let registry_url = format! ( " {} / " , registry_url . trim_end_matches ( '/' ) ) ;
match Url ::parse ( & registry_url ) {
Ok ( url ) = > {
return url ;
}
Err ( err ) = > {
log ::debug! (
" Invalid {} environment variable: {:#} " ,
env_var_name ,
err ,
) ;
}
2023-10-02 21:53:55 +00:00
}
}
2023-10-25 18:39:00 +00:00
Url ::parse ( " https://registry.npmjs.org " ) . unwrap ( )
} ) ;
2023-10-02 21:53:55 +00:00
& NPM_REGISTRY_DEFAULT_URL
}
2024-03-07 13:59:57 +00:00
pub static DENO_DISABLE_PEDANTIC_NODE_WARNINGS : Lazy < bool > = Lazy ::new ( | | {
std ::env ::var ( " DENO_DISABLE_PEDANTIC_NODE_WARNINGS " )
. ok ( )
. is_some ( )
} ) ;
2024-03-21 02:02:44 +00:00
static DENO_FUTURE : Lazy < bool > =
Lazy ::new ( | | std ::env ::var ( " DENO_FUTURE " ) . ok ( ) . is_some ( ) ) ;
2024-02-14 18:30:44 +00:00
pub fn jsr_url ( ) -> & 'static Url {
static JSR_URL : Lazy < Url > = Lazy ::new ( | | {
let env_var_name = " JSR_URL " ;
2023-12-12 14:45:20 +00:00
if let Ok ( registry_url ) = std ::env ::var ( env_var_name ) {
// ensure there is a trailing slash for the directory
let registry_url = format! ( " {} / " , registry_url . trim_end_matches ( '/' ) ) ;
match Url ::parse ( & registry_url ) {
Ok ( url ) = > {
return url ;
}
Err ( err ) = > {
log ::debug! (
" Invalid {} environment variable: {:#} " ,
env_var_name ,
err ,
) ;
}
}
}
2023-12-15 13:14:28 +00:00
Url ::parse ( " https://jsr.io/ " ) . unwrap ( )
2023-12-12 14:45:20 +00:00
} ) ;
2024-02-14 18:30:44 +00:00
& JSR_URL
2023-12-12 14:45:20 +00:00
}
2024-02-14 18:30:44 +00:00
pub fn jsr_api_url ( ) -> & 'static Url {
static JSR_API_URL : Lazy < Url > = Lazy ::new ( | | {
let mut jsr_api_url = jsr_url ( ) . clone ( ) ;
jsr_api_url . set_path ( " api/ " ) ;
jsr_api_url
2023-12-12 14:45:20 +00:00
} ) ;
2024-02-14 18:30:44 +00:00
& JSR_API_URL
2023-12-12 14:45:20 +00:00
}
2024-04-11 23:00:17 +00:00
pub fn ts_config_to_transpile_and_emit_options (
2023-08-24 09:21:34 +00:00
config : deno_config ::TsConfig ,
2024-04-11 23:00:17 +00:00
) -> ( deno_ast ::TranspileOptions , deno_ast ::EmitOptions ) {
2023-08-24 09:21:34 +00:00
let options : deno_config ::EmitConfigOptions =
serde_json ::from_value ( config . 0 ) . unwrap ( ) ;
let imports_not_used_as_values =
match options . imports_not_used_as_values . as_str ( ) {
" preserve " = > deno_ast ::ImportsNotUsedAsValues ::Preserve ,
" error " = > deno_ast ::ImportsNotUsedAsValues ::Error ,
_ = > deno_ast ::ImportsNotUsedAsValues ::Remove ,
} ;
2023-11-01 20:30:23 +00:00
let ( transform_jsx , jsx_automatic , jsx_development , precompile_jsx ) =
2023-08-24 09:21:34 +00:00
match options . jsx . as_str ( ) {
2023-11-01 20:30:23 +00:00
" react " = > ( true , false , false , false ) ,
" react-jsx " = > ( true , true , false , false ) ,
" react-jsxdev " = > ( true , true , true , false ) ,
" precompile " = > ( false , false , false , true ) ,
_ = > ( false , false , false , false ) ,
2023-08-24 09:21:34 +00:00
} ;
2024-04-11 23:00:17 +00:00
let source_map = if options . inline_source_map {
SourceMapOption ::Inline
} else if options . source_map {
SourceMapOption ::Separate
} else {
SourceMapOption ::None
} ;
(
deno_ast ::TranspileOptions {
use_ts_decorators : options . experimental_decorators ,
use_decorators_proposal : ! options . experimental_decorators ,
emit_metadata : options . emit_decorator_metadata ,
imports_not_used_as_values ,
jsx_automatic ,
jsx_development ,
jsx_factory : options . jsx_factory ,
jsx_fragment_factory : options . jsx_fragment_factory ,
jsx_import_source : options . jsx_import_source ,
precompile_jsx ,
2024-04-19 00:51:16 +00:00
precompile_jsx_skip_elements : None ,
2024-04-11 23:00:17 +00:00
transform_jsx ,
var_decl_imports : false ,
} ,
deno_ast ::EmitOptions {
inline_sources : options . inline_sources ,
keep_comments : false ,
source_map ,
} ,
)
2023-08-24 09:21:34 +00:00
}
2023-01-07 20:22:09 +00:00
2022-11-28 22:28:54 +00:00
/// Indicates how cached source files should be handled.
#[ derive(Debug, Clone, Eq, PartialEq) ]
pub enum CacheSetting {
/// Only the cached files should be used. Any files not in the cache will
/// error. This is the equivalent of `--cached-only` in the CLI.
Only ,
/// No cached source files should be used, and all files should be reloaded.
/// This is the equivalent of `--reload` in the CLI.
ReloadAll ,
/// Only some cached resources should be used. This is the equivalent of
2024-04-10 21:26:35 +00:00
/// `--reload=jsr:@std/http/file-server` or
/// `--reload=jsr:@std/http/file-server,jsr:@std/assert/assert-equals`.
2022-11-28 22:28:54 +00:00
ReloadSome ( Vec < String > ) ,
/// The usability of a cached value is determined by analyzing the cached
/// headers and other metadata associated with a cached response, reloading
/// any cached "non-fresh" cached responses.
RespectHeaders ,
/// The cached source files should be used for local modules. This is the
/// default behavior of the CLI.
Use ,
}
impl CacheSetting {
pub fn should_use_for_npm_package ( & self , package_name : & str ) -> bool {
match self {
CacheSetting ::ReloadAll = > false ,
CacheSetting ::ReloadSome ( list ) = > {
if list . iter ( ) . any ( | i | i = = " npm: " ) {
return false ;
}
2023-01-27 15:43:16 +00:00
let specifier = format! ( " npm: {package_name} " ) ;
2022-11-28 22:28:54 +00:00
if list . contains ( & specifier ) {
return false ;
}
true
}
_ = > true ,
}
}
}
2023-01-07 20:22:09 +00:00
#[ derive(Clone, Debug, Eq, PartialEq) ]
pub struct BenchOptions {
2024-01-08 17:18:42 +00:00
pub files : FilePatterns ,
2023-01-07 20:22:09 +00:00
pub filter : Option < String > ,
2023-02-12 17:40:45 +00:00
pub json : bool ,
2023-03-26 14:55:58 +00:00
pub no_run : bool ,
2023-01-07 20:22:09 +00:00
}
impl BenchOptions {
pub fn resolve (
maybe_bench_config : Option < BenchConfig > ,
maybe_bench_flags : Option < BenchFlags > ,
2024-01-08 17:18:42 +00:00
initial_cwd : & Path ,
2023-01-07 20:22:09 +00:00
) -> Result < Self , AnyError > {
let bench_flags = maybe_bench_flags . unwrap_or_default ( ) ;
Ok ( Self {
files : resolve_files (
maybe_bench_config . map ( | c | c . files ) ,
Some ( bench_flags . files ) ,
2024-01-08 17:18:42 +00:00
initial_cwd ,
2023-05-23 01:39:59 +00:00
) ? ,
2023-01-07 20:22:09 +00:00
filter : bench_flags . filter ,
2023-02-12 17:40:45 +00:00
json : bench_flags . json ,
2023-03-26 14:55:58 +00:00
no_run : bench_flags . no_run ,
2023-01-07 20:22:09 +00:00
} )
}
}
2024-01-18 20:57:30 +00:00
#[ derive(Clone, Debug) ]
2023-01-07 20:22:09 +00:00
pub struct FmtOptions {
pub check : bool ,
pub options : FmtOptionsConfig ,
2024-01-08 17:18:42 +00:00
pub files : FilePatterns ,
2023-01-07 20:22:09 +00:00
}
2024-03-26 15:52:20 +00:00
impl Default for FmtOptions {
fn default ( ) -> Self {
Self ::new_with_base ( PathBuf ::from ( " / " ) )
}
}
2023-01-07 20:22:09 +00:00
impl FmtOptions {
2024-01-18 20:57:30 +00:00
pub fn new_with_base ( base : PathBuf ) -> Self {
Self {
check : false ,
options : FmtOptionsConfig ::default ( ) ,
files : FilePatterns ::new_with_base ( base ) ,
}
}
2023-01-07 20:22:09 +00:00
pub fn resolve (
maybe_fmt_config : Option < FmtConfig > ,
2023-06-14 22:29:19 +00:00
maybe_fmt_flags : Option < FmtFlags > ,
2024-01-08 17:18:42 +00:00
initial_cwd : & Path ,
2023-01-07 20:22:09 +00:00
) -> Result < Self , AnyError > {
let ( maybe_config_options , maybe_config_files ) =
maybe_fmt_config . map ( | c | ( c . options , c . files ) ) . unzip ( ) ;
Ok ( Self {
check : maybe_fmt_flags . as_ref ( ) . map ( | f | f . check ) . unwrap_or ( false ) ,
options : resolve_fmt_options (
maybe_fmt_flags . as_ref ( ) ,
maybe_config_options ,
) ,
files : resolve_files (
maybe_config_files ,
maybe_fmt_flags . map ( | f | f . files ) ,
2024-01-08 17:18:42 +00:00
initial_cwd ,
2023-05-23 01:39:59 +00:00
) ? ,
2023-01-07 20:22:09 +00:00
} )
}
}
fn resolve_fmt_options (
fmt_flags : Option < & FmtFlags > ,
options : Option < FmtOptionsConfig > ,
) -> FmtOptionsConfig {
let mut options = options . unwrap_or_default ( ) ;
if let Some ( fmt_flags ) = fmt_flags {
if let Some ( use_tabs ) = fmt_flags . use_tabs {
options . use_tabs = Some ( use_tabs ) ;
}
if let Some ( line_width ) = fmt_flags . line_width {
options . line_width = Some ( line_width . get ( ) ) ;
}
if let Some ( indent_width ) = fmt_flags . indent_width {
options . indent_width = Some ( indent_width . get ( ) ) ;
}
if let Some ( single_quote ) = fmt_flags . single_quote {
options . single_quote = Some ( single_quote ) ;
}
if let Some ( prose_wrap ) = & fmt_flags . prose_wrap {
options . prose_wrap = Some ( match prose_wrap . as_str ( ) {
" always " = > ProseWrap ::Always ,
" never " = > ProseWrap ::Never ,
" preserve " = > ProseWrap ::Preserve ,
// validators in `flags.rs` makes other values unreachable
_ = > unreachable! ( ) ,
} ) ;
}
2023-01-24 20:07:00 +00:00
2023-01-25 20:06:00 +00:00
if let Some ( no_semis ) = & fmt_flags . no_semicolons {
options . semi_colons = Some ( ! no_semis ) ;
2023-01-24 20:07:00 +00:00
}
2023-01-07 20:22:09 +00:00
}
options
}
#[ derive(Clone) ]
pub struct TestOptions {
2024-01-08 17:18:42 +00:00
pub files : FilePatterns ,
2023-01-07 20:22:09 +00:00
pub doc : bool ,
pub no_run : bool ,
pub fail_fast : Option < NonZeroUsize > ,
pub allow_none : bool ,
pub filter : Option < String > ,
pub shuffle : Option < u64 > ,
pub concurrent_jobs : NonZeroUsize ,
2024-02-28 16:12:43 +00:00
pub trace_leaks : bool ,
2023-08-02 16:38:10 +00:00
pub reporter : TestReporterConfig ,
2023-08-03 02:05:34 +00:00
pub junit_path : Option < String > ,
2023-01-07 20:22:09 +00:00
}
impl TestOptions {
pub fn resolve (
maybe_test_config : Option < TestConfig > ,
maybe_test_flags : Option < TestFlags > ,
2024-01-08 17:18:42 +00:00
initial_cwd : & Path ,
2023-01-07 20:22:09 +00:00
) -> Result < Self , AnyError > {
let test_flags = maybe_test_flags . unwrap_or_default ( ) ;
Ok ( Self {
files : resolve_files (
maybe_test_config . map ( | c | c . files ) ,
Some ( test_flags . files ) ,
2024-01-08 17:18:42 +00:00
initial_cwd ,
2023-05-23 01:39:59 +00:00
) ? ,
2023-01-07 20:22:09 +00:00
allow_none : test_flags . allow_none ,
concurrent_jobs : test_flags
. concurrent_jobs
. unwrap_or_else ( | | NonZeroUsize ::new ( 1 ) . unwrap ( ) ) ,
doc : test_flags . doc ,
fail_fast : test_flags . fail_fast ,
filter : test_flags . filter ,
no_run : test_flags . no_run ,
shuffle : test_flags . shuffle ,
2024-02-28 16:12:43 +00:00
trace_leaks : test_flags . trace_leaks ,
2023-08-02 16:38:10 +00:00
reporter : test_flags . reporter ,
2023-08-03 02:05:34 +00:00
junit_path : test_flags . junit_path ,
2023-01-07 20:22:09 +00:00
} )
}
}
2023-03-09 19:18:00 +00:00
#[ derive(Clone, Default, Debug) ]
2023-01-07 20:22:09 +00:00
pub enum LintReporterKind {
2023-03-09 19:18:00 +00:00
#[ default ]
2023-01-07 20:22:09 +00:00
Pretty ,
Json ,
Compact ,
}
2024-01-18 20:57:30 +00:00
#[ derive(Clone, Debug) ]
2023-01-07 20:22:09 +00:00
pub struct LintOptions {
pub rules : LintRulesConfig ,
2024-01-08 17:18:42 +00:00
pub files : FilePatterns ,
2023-01-07 20:22:09 +00:00
pub reporter_kind : LintReporterKind ,
2024-03-21 21:18:59 +00:00
pub fix : bool ,
2023-01-07 20:22:09 +00:00
}
2024-03-26 15:52:20 +00:00
impl Default for LintOptions {
fn default ( ) -> Self {
Self ::new_with_base ( PathBuf ::from ( " / " ) )
}
}
2023-01-07 20:22:09 +00:00
impl LintOptions {
2024-01-18 20:57:30 +00:00
pub fn new_with_base ( base : PathBuf ) -> Self {
Self {
rules : Default ::default ( ) ,
files : FilePatterns ::new_with_base ( base ) ,
reporter_kind : Default ::default ( ) ,
2024-03-21 21:18:59 +00:00
fix : false ,
2024-01-18 20:57:30 +00:00
}
}
2023-01-07 20:22:09 +00:00
pub fn resolve (
maybe_lint_config : Option < LintConfig > ,
2023-06-14 22:29:19 +00:00
maybe_lint_flags : Option < LintFlags > ,
2024-01-08 17:18:42 +00:00
initial_cwd : & Path ,
2023-01-07 20:22:09 +00:00
) -> Result < Self , AnyError > {
2024-03-21 21:18:59 +00:00
let fix = maybe_lint_flags . as_ref ( ) . map ( | f | f . fix ) . unwrap_or ( false ) ;
2023-01-07 20:22:09 +00:00
let mut maybe_reporter_kind =
maybe_lint_flags . as_ref ( ) . and_then ( | lint_flags | {
if lint_flags . json {
Some ( LintReporterKind ::Json )
} else if lint_flags . compact {
Some ( LintReporterKind ::Compact )
} else {
None
}
} ) ;
if maybe_reporter_kind . is_none ( ) {
// Flag not set, so try to get lint reporter from the config file.
if let Some ( lint_config ) = & maybe_lint_config {
maybe_reporter_kind = match lint_config . report . as_deref ( ) {
Some ( " json " ) = > Some ( LintReporterKind ::Json ) ,
Some ( " compact " ) = > Some ( LintReporterKind ::Compact ) ,
Some ( " pretty " ) = > Some ( LintReporterKind ::Pretty ) ,
Some ( _ ) = > {
bail! ( " Invalid lint report type in config file " )
}
None = > None ,
}
}
}
let (
maybe_file_flags ,
maybe_rules_tags ,
maybe_rules_include ,
maybe_rules_exclude ,
) = maybe_lint_flags
. map ( | f | {
(
f . files ,
f . maybe_rules_tags ,
f . maybe_rules_include ,
f . maybe_rules_exclude ,
)
} )
. unwrap_or_default ( ) ;
let ( maybe_config_files , maybe_config_rules ) =
maybe_lint_config . map ( | c | ( c . files , c . rules ) ) . unzip ( ) ;
Ok ( Self {
reporter_kind : maybe_reporter_kind . unwrap_or_default ( ) ,
2024-01-08 17:18:42 +00:00
files : resolve_files (
maybe_config_files ,
Some ( maybe_file_flags ) ,
initial_cwd ,
) ? ,
2023-01-07 20:22:09 +00:00
rules : resolve_lint_rules_options (
maybe_config_rules ,
maybe_rules_tags ,
maybe_rules_include ,
maybe_rules_exclude ,
) ,
2024-03-21 21:18:59 +00:00
fix ,
2023-01-07 20:22:09 +00:00
} )
}
}
fn resolve_lint_rules_options (
maybe_lint_rules_config : Option < LintRulesConfig > ,
mut maybe_rules_tags : Option < Vec < String > > ,
mut maybe_rules_include : Option < Vec < String > > ,
mut maybe_rules_exclude : Option < Vec < String > > ,
) -> LintRulesConfig {
if let Some ( config_rules ) = maybe_lint_rules_config {
// Try to get configured rules. CLI flags take precedence
// over config file, i.e. if there's `rules.include` in config file
// and `--rules-include` CLI flag, only the flag value is taken into account.
if maybe_rules_include . is_none ( ) {
maybe_rules_include = config_rules . include ;
}
if maybe_rules_exclude . is_none ( ) {
maybe_rules_exclude = config_rules . exclude ;
}
if maybe_rules_tags . is_none ( ) {
maybe_rules_tags = config_rules . tags ;
}
}
LintRulesConfig {
exclude : maybe_rules_exclude ,
include : maybe_rules_include ,
tags : maybe_rules_tags ,
}
}
2023-02-20 18:14:06 +00:00
/// Discover `package.json` file. If `maybe_stop_at` is provided, we will stop
/// crawling up the directory tree at that path.
fn discover_package_json (
flags : & Flags ,
maybe_stop_at : Option < PathBuf > ,
2023-03-14 01:12:09 +00:00
current_dir : & Path ,
2023-02-20 18:14:06 +00:00
) -> Result < Option < PackageJson > , AnyError > {
// TODO(bartlomieju): discover for all subcommands, but print warnings that
// `package.json` is ignored in bundle/compile/etc.
2023-03-14 01:12:09 +00:00
if let Some ( package_json_dir ) = flags . package_json_search_dir ( current_dir ) {
2023-02-23 15:58:10 +00:00
return package_json ::discover_from ( & package_json_dir , maybe_stop_at ) ;
2023-02-20 18:14:06 +00:00
}
log ::debug! ( " No package.json file found " ) ;
Ok ( None )
}
2023-05-01 20:42:05 +00:00
struct CliRootCertStoreProvider {
cell : OnceCell < RootCertStore > ,
maybe_root_path : Option < PathBuf > ,
maybe_ca_stores : Option < Vec < String > > ,
maybe_ca_data : Option < CaData > ,
}
impl CliRootCertStoreProvider {
pub fn new (
maybe_root_path : Option < PathBuf > ,
maybe_ca_stores : Option < Vec < String > > ,
maybe_ca_data : Option < CaData > ,
) -> Self {
Self {
cell : Default ::default ( ) ,
maybe_root_path ,
maybe_ca_stores ,
maybe_ca_data ,
}
}
}
impl RootCertStoreProvider for CliRootCertStoreProvider {
fn get_or_try_init ( & self ) -> Result < & RootCertStore , AnyError > {
self
. cell
. get_or_try_init ( | | {
get_root_cert_store (
self . maybe_root_path . clone ( ) ,
self . maybe_ca_stores . clone ( ) ,
self . maybe_ca_data . clone ( ) ,
)
} )
. map_err ( | e | e . into ( ) )
}
}
#[ derive(Error, Debug, Clone) ]
pub enum RootCertStoreLoadError {
#[ error(
" Unknown certificate store \" {0} \" specified (allowed: \" system,mozilla \" ) "
) ]
UnknownStore ( String ) ,
#[ error( " Unable to add pem file to certificate store: {0} " ) ]
FailedAddPemFile ( String ) ,
#[ error( " Failed opening CA file: {0} " ) ]
CaFileOpenError ( String ) ,
}
2022-11-28 22:28:54 +00:00
/// Create and populate a root cert store based on the passed options and
/// environment.
pub fn get_root_cert_store (
maybe_root_path : Option < PathBuf > ,
maybe_ca_stores : Option < Vec < String > > ,
2023-01-18 00:18:24 +00:00
maybe_ca_data : Option < CaData > ,
2023-05-01 20:42:05 +00:00
) -> Result < RootCertStore , RootCertStoreLoadError > {
2022-11-28 22:28:54 +00:00
let mut root_cert_store = RootCertStore ::empty ( ) ;
let ca_stores : Vec < String > = maybe_ca_stores
. or_else ( | | {
let env_ca_store = env ::var ( " DENO_TLS_CA_STORE " ) . ok ( ) ? ;
Some (
env_ca_store
. split ( ',' )
. map ( | s | s . trim ( ) . to_string ( ) )
. filter ( | s | ! s . is_empty ( ) )
. collect ( ) ,
)
} )
. unwrap_or_else ( | | vec! [ " mozilla " . to_string ( ) ] ) ;
for store in ca_stores . iter ( ) {
match store . as_str ( ) {
" mozilla " = > {
2023-08-25 21:40:25 +00:00
root_cert_store . add_trust_anchors (
webpki_roots ::TLS_SERVER_ROOTS . iter ( ) . map ( | ta | {
2022-11-28 22:28:54 +00:00
rustls ::OwnedTrustAnchor ::from_subject_spki_name_constraints (
ta . subject ,
ta . spki ,
ta . name_constraints ,
)
} ) ,
) ;
}
" system " = > {
let roots = load_native_certs ( ) . expect ( " could not load platform certs " ) ;
for root in roots {
root_cert_store
. add ( & rustls ::Certificate ( root . 0 ) )
. expect ( " Failed to add platform cert to root cert store " ) ;
}
}
_ = > {
2023-05-01 20:42:05 +00:00
return Err ( RootCertStoreLoadError ::UnknownStore ( store . clone ( ) ) ) ;
2022-11-28 22:28:54 +00:00
}
}
}
2023-01-18 00:18:24 +00:00
let ca_data =
maybe_ca_data . or_else ( | | env ::var ( " DENO_CERT " ) . ok ( ) . map ( CaData ::File ) ) ;
if let Some ( ca_data ) = ca_data {
let result = match ca_data {
CaData ::File ( ca_file ) = > {
let ca_file = if let Some ( root ) = & maybe_root_path {
root . join ( & ca_file )
} else {
PathBuf ::from ( ca_file )
} ;
2023-05-01 20:42:05 +00:00
let certfile = std ::fs ::File ::open ( ca_file ) . map_err ( | err | {
RootCertStoreLoadError ::CaFileOpenError ( err . to_string ( ) )
} ) ? ;
2023-01-18 00:18:24 +00:00
let mut reader = BufReader ::new ( certfile ) ;
rustls_pemfile ::certs ( & mut reader )
}
CaData ::Bytes ( data ) = > {
let mut reader = BufReader ::new ( Cursor ::new ( data ) ) ;
rustls_pemfile ::certs ( & mut reader )
}
2022-11-28 22:28:54 +00:00
} ;
2023-01-18 00:18:24 +00:00
match result {
2022-11-28 22:28:54 +00:00
Ok ( certs ) = > {
root_cert_store . add_parsable_certificates ( & certs ) ;
}
Err ( e ) = > {
2023-05-01 20:42:05 +00:00
return Err ( RootCertStoreLoadError ::FailedAddPemFile ( e . to_string ( ) ) ) ;
2022-11-28 22:28:54 +00:00
}
}
}
Ok ( root_cert_store )
}
2023-10-02 21:53:55 +00:00
/// State provided to the process via an environment variable.
#[ derive(Clone, Debug, Serialize, Deserialize) ]
pub struct NpmProcessState {
2023-10-25 18:39:00 +00:00
pub kind : NpmProcessStateKind ,
2023-10-02 21:53:55 +00:00
pub local_node_modules_path : Option < String > ,
}
2023-10-25 18:39:00 +00:00
#[ derive(Clone, Debug, Serialize, Deserialize) ]
pub enum NpmProcessStateKind {
Snapshot ( deno_npm ::resolution ::SerializedNpmResolutionSnapshot ) ,
Byonm ,
}
2023-02-20 18:14:06 +00:00
const RESOLUTION_STATE_ENV_VAR_NAME : & str =
" DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE " ;
static NPM_PROCESS_STATE : Lazy < Option < NpmProcessState > > = Lazy ::new ( | | {
let state = std ::env ::var ( RESOLUTION_STATE_ENV_VAR_NAME ) . ok ( ) ? ;
let state : NpmProcessState = serde_json ::from_str ( & state ) . ok ( ) ? ;
// remove the environment variable so that sub processes
// that are spawned do not also use this.
std ::env ::remove_var ( RESOLUTION_STATE_ENV_VAR_NAME ) ;
Some ( state )
} ) ;
2022-06-30 00:41:48 +00:00
/// Overrides for the options below that when set will
/// use these values over the values derived from the
/// CLI flags or config file.
2023-10-02 21:53:55 +00:00
#[ derive(Default, Clone) ]
2022-06-30 00:41:48 +00:00
struct CliOptionOverrides {
import_map_specifier : Option < Option < ModuleSpecifier > > ,
}
2023-05-11 00:06:59 +00:00
/// Holds the resolved options of many sources used by subcommands
2022-06-28 20:45:55 +00:00
/// and provides some helper function for creating common objects.
2022-06-29 15:51:11 +00:00
pub struct CliOptions {
// the source of the options is a detail the rest of the
2022-06-28 20:45:55 +00:00
// application need not concern itself with, so keep these private
flags : Flags ,
2023-03-14 01:12:09 +00:00
initial_cwd : PathBuf ,
2023-02-23 01:16:16 +00:00
maybe_node_modules_folder : Option < PathBuf > ,
2023-08-07 01:56:56 +00:00
maybe_vendor_folder : Option < PathBuf > ,
2022-06-28 20:45:55 +00:00
maybe_config_file : Option < ConfigFile > ,
2023-02-20 18:14:06 +00:00
maybe_package_json : Option < PackageJson > ,
2022-11-02 15:32:30 +00:00
maybe_lockfile : Option < Arc < Mutex < Lockfile > > > ,
2022-06-30 00:41:48 +00:00
overrides : CliOptionOverrides ,
2023-11-17 01:28:38 +00:00
maybe_workspace_config : Option < WorkspaceConfig > ,
2024-01-18 23:30:49 +00:00
pub disable_deprecated_api_warning : bool ,
2024-01-26 15:41:16 +00:00
pub verbose_deprecated_api_warning : bool ,
2022-06-28 20:45:55 +00:00
}
2022-06-29 15:51:11 +00:00
impl CliOptions {
2022-11-02 15:32:30 +00:00
pub fn new (
flags : Flags ,
2023-02-23 01:16:16 +00:00
initial_cwd : PathBuf ,
2022-11-02 15:32:30 +00:00
maybe_config_file : Option < ConfigFile > ,
2023-05-23 01:28:36 +00:00
maybe_lockfile : Option < Arc < Mutex < Lockfile > > > ,
2023-02-20 18:14:06 +00:00
maybe_package_json : Option < PackageJson > ,
2024-02-29 03:54:16 +00:00
force_global_cache : bool ,
2023-02-23 01:16:16 +00:00
) -> Result < Self , AnyError > {
2022-06-28 20:45:55 +00:00
if let Some ( insecure_allowlist ) =
flags . unsafely_ignore_certificate_errors . as_ref ( )
{
let domains = if insecure_allowlist . is_empty ( ) {
" for all hostnames " . to_string ( )
} else {
format! ( " for: {} " , insecure_allowlist . join ( " , " ) )
} ;
let msg =
2023-01-27 15:43:16 +00:00
format! ( " DANGER: TLS certificate validation is disabled {domains} " ) ;
2022-07-01 15:50:16 +00:00
// use eprintln instead of log::warn so this always gets shown
2022-06-28 20:45:55 +00:00
eprintln! ( " {} " , colors ::yellow ( msg ) ) ;
}
2024-02-29 03:54:16 +00:00
let maybe_lockfile = maybe_lockfile . filter ( | _ | ! force_global_cache ) ;
2023-08-02 00:49:09 +00:00
let maybe_node_modules_folder = resolve_node_modules_folder (
2023-02-23 01:16:16 +00:00
& initial_cwd ,
& flags ,
maybe_config_file . as_ref ( ) ,
maybe_package_json . as_ref ( ) ,
)
. with_context ( | | " Resolving node_modules folder. " ) ? ;
2024-02-29 03:54:16 +00:00
let maybe_vendor_folder = if force_global_cache {
None
} else {
resolve_vendor_folder ( & initial_cwd , & flags , maybe_config_file . as_ref ( ) )
} ;
2023-11-17 01:28:38 +00:00
let maybe_workspace_config =
if let Some ( config_file ) = maybe_config_file . as_ref ( ) {
config_file . to_workspace_config ( ) ?
} else {
None
} ;
2023-11-01 15:21:13 +00:00
if let Some ( env_file_name ) = & flags . env_file {
if ( from_filename ( env_file_name ) ) . is_err ( ) {
2024-01-21 23:27:14 +00:00
log ::info! (
" {} The `--env` flag was used, but the dotenv file '{}' was not found. " ,
colors ::yellow ( " Warning " ) ,
env_file_name
) ;
2023-11-01 15:21:13 +00:00
}
}
2024-01-18 23:30:49 +00:00
let disable_deprecated_api_warning = flags . log_level
= = Some ( log ::Level ::Error )
| | std ::env ::var ( " DENO_NO_DEPRECATION_WARNINGS " ) . ok ( ) . is_some ( ) ;
2024-01-26 15:41:16 +00:00
let verbose_deprecated_api_warning =
std ::env ::var ( " DENO_VERBOSE_WARNINGS " ) . ok ( ) . is_some ( ) ;
2023-02-23 01:16:16 +00:00
Ok ( Self {
flags ,
2023-03-14 01:12:09 +00:00
initial_cwd ,
2022-06-28 20:45:55 +00:00
maybe_config_file ,
2022-11-02 15:32:30 +00:00
maybe_lockfile ,
2023-02-20 18:14:06 +00:00
maybe_package_json ,
2023-02-23 01:16:16 +00:00
maybe_node_modules_folder ,
2023-08-07 01:56:56 +00:00
maybe_vendor_folder ,
2022-06-30 00:41:48 +00:00
overrides : Default ::default ( ) ,
2023-11-17 01:28:38 +00:00
maybe_workspace_config ,
2024-01-18 23:30:49 +00:00
disable_deprecated_api_warning ,
2024-01-26 15:41:16 +00:00
verbose_deprecated_api_warning ,
2023-02-23 01:16:16 +00:00
} )
2022-07-01 15:50:16 +00:00
}
pub fn from_flags ( flags : Flags ) -> Result < Self , AnyError > {
2023-02-23 01:16:16 +00:00
let initial_cwd =
std ::env ::current_dir ( ) . with_context ( | | " Failed getting cwd. " ) ? ;
2024-02-27 08:27:21 +00:00
let additional_config_file_names =
if matches! ( flags . subcommand , DenoSubcommand ::Publish ( .. ) ) {
Some ( vec! [ " jsr.json " , " jsr.jsonc " ] )
} else {
None
} ;
2024-03-27 21:14:27 +00:00
let parse_options = deno_config ::ParseOptions {
include_task_comments : matches ! (
flags . subcommand ,
DenoSubcommand ::Task ( .. )
) ,
} ;
2023-08-24 09:21:34 +00:00
let maybe_config_file = ConfigFile ::discover (
& flags . config_flag ,
flags . config_path_args ( & initial_cwd ) ,
& initial_cwd ,
2024-02-27 08:27:21 +00:00
additional_config_file_names ,
2024-03-27 21:14:27 +00:00
& parse_options ,
2023-08-24 09:21:34 +00:00
) ? ;
2023-02-20 18:14:06 +00:00
let mut maybe_package_json = None ;
2023-08-24 09:21:34 +00:00
if flags . config_flag = = deno_config ::ConfigFlag ::Disabled
2023-02-24 19:23:07 +00:00
| | flags . no_npm
| | has_flag_env_var ( " DENO_NO_PACKAGE_JSON " )
{
2023-02-24 18:51:21 +00:00
log ::debug! ( " package.json auto-discovery is disabled " )
} else if let Some ( config_file ) = & maybe_config_file {
2023-02-20 18:14:06 +00:00
let specifier = config_file . specifier . clone ( ) ;
if specifier . scheme ( ) = = " file " {
let maybe_stop_at = specifier
. to_file_path ( )
. unwrap ( )
. parent ( )
. map ( | p | p . to_path_buf ( ) ) ;
2023-03-14 01:12:09 +00:00
maybe_package_json =
discover_package_json ( & flags , maybe_stop_at , & initial_cwd ) ? ;
2023-02-20 18:14:06 +00:00
}
} else {
2023-03-14 01:12:09 +00:00
maybe_package_json = discover_package_json ( & flags , None , & initial_cwd ) ? ;
2023-02-20 18:14:06 +00:00
}
2023-02-24 18:51:21 +00:00
2022-11-02 15:32:30 +00:00
let maybe_lock_file =
2023-01-23 22:41:02 +00:00
lockfile ::discover ( & flags , maybe_config_file . as_ref ( ) ) ? ;
2023-02-23 01:16:16 +00:00
Self ::new (
2023-02-20 18:14:06 +00:00
flags ,
2023-02-23 01:16:16 +00:00
initial_cwd ,
2023-02-20 18:14:06 +00:00
maybe_config_file ,
2023-05-23 01:28:36 +00:00
maybe_lock_file . map ( | l | Arc ::new ( Mutex ::new ( l ) ) ) ,
2023-02-20 18:14:06 +00:00
maybe_package_json ,
2024-02-29 03:54:16 +00:00
false ,
2023-02-23 01:16:16 +00:00
)
2022-06-28 20:45:55 +00:00
}
2023-03-14 01:12:09 +00:00
#[ inline(always) ]
pub fn initial_cwd ( & self ) -> & Path {
& self . initial_cwd
}
2022-06-28 20:45:55 +00:00
pub fn maybe_config_file_specifier ( & self ) -> Option < ModuleSpecifier > {
self . maybe_config_file . as_ref ( ) . map ( | f | f . specifier . clone ( ) )
}
pub fn ts_type_lib_window ( & self ) -> TsTypeLib {
2024-01-21 22:47:46 +00:00
TsTypeLib ::DenoWindow
2022-06-28 20:45:55 +00:00
}
pub fn ts_type_lib_worker ( & self ) -> TsTypeLib {
2024-01-21 22:47:46 +00:00
TsTypeLib ::DenoWorker
2022-06-28 20:45:55 +00:00
}
pub fn cache_setting ( & self ) -> CacheSetting {
if self . flags . cached_only {
CacheSetting ::Only
} else if ! self . flags . cache_blocklist . is_empty ( ) {
CacheSetting ::ReloadSome ( self . flags . cache_blocklist . clone ( ) )
} else if self . flags . reload {
CacheSetting ::ReloadAll
} else {
CacheSetting ::Use
}
}
2023-05-17 21:38:50 +00:00
pub fn npm_system_info ( & self ) -> NpmSystemInfo {
match self . sub_command ( ) {
DenoSubcommand ::Compile ( CompileFlags {
target : Some ( target ) ,
..
} ) = > {
// the values of NpmSystemInfo align with the possible values for the
// `arch` and `platform` fields of Node.js' `process` global:
// https://nodejs.org/api/process.html
match target . as_str ( ) {
" aarch64-apple-darwin " = > NpmSystemInfo {
os : " darwin " . to_string ( ) ,
cpu : " arm64 " . to_string ( ) ,
} ,
2024-02-23 18:18:39 +00:00
" aarch64-unknown-linux-gnu " = > NpmSystemInfo {
os : " linux " . to_string ( ) ,
cpu : " arm64 " . to_string ( ) ,
} ,
2023-05-17 21:38:50 +00:00
" x86_64-apple-darwin " = > NpmSystemInfo {
os : " darwin " . to_string ( ) ,
cpu : " x64 " . to_string ( ) ,
} ,
" x86_64-unknown-linux-gnu " = > NpmSystemInfo {
os : " linux " . to_string ( ) ,
cpu : " x64 " . to_string ( ) ,
} ,
" x86_64-pc-windows-msvc " = > NpmSystemInfo {
os : " win32 " . to_string ( ) ,
cpu : " x64 " . to_string ( ) ,
} ,
value = > {
2024-02-23 21:34:24 +00:00
log ::warn! (
concat! (
" Not implemented npm system info for target '{}'. Using current " ,
" system default. This may impact architecture specific dependencies. "
) ,
value ,
) ;
2023-05-17 21:38:50 +00:00
NpmSystemInfo ::default ( )
}
}
}
_ = > NpmSystemInfo ::default ( ) ,
}
}
2024-02-24 05:21:09 +00:00
/// Resolve the specifier for a specified import map.
///
/// This will NOT include the config file if it
/// happens to be an import map.
pub fn resolve_specified_import_map_specifier (
2022-06-28 20:45:55 +00:00
& self ,
) -> Result < Option < ModuleSpecifier > , AnyError > {
2022-06-30 00:41:48 +00:00
match self . overrides . import_map_specifier . clone ( ) {
2023-01-25 20:13:40 +00:00
Some ( maybe_path ) = > Ok ( maybe_path ) ,
2022-06-30 00:41:48 +00:00
None = > resolve_import_map_specifier (
self . flags . import_map_path . as_deref ( ) ,
self . maybe_config_file . as_ref ( ) ,
2023-03-14 01:12:09 +00:00
& self . initial_cwd ,
2022-06-30 00:41:48 +00:00
) ,
}
}
2023-01-25 20:13:40 +00:00
pub async fn resolve_import_map (
& self ,
file_fetcher : & FileFetcher ,
) -> Result < Option < ImportMap > , AnyError > {
2023-11-17 01:28:38 +00:00
if let Some ( workspace_config ) = self . maybe_workspace_config . as_ref ( ) {
2024-02-24 05:21:09 +00:00
let root_config_file = self . maybe_config_file . as_ref ( ) . unwrap ( ) ;
2023-11-17 01:28:38 +00:00
let base_import_map_config = ::import_map ::ext ::ImportMapConfig {
2024-02-24 05:21:09 +00:00
base_url : root_config_file . specifier . clone ( ) ,
import_map_value : root_config_file . to_import_map_value_from_imports ( ) ,
2023-11-17 01:28:38 +00:00
} ;
let children_configs = workspace_config
. members
. iter ( )
2024-02-24 05:21:09 +00:00
. map ( | member | ::import_map ::ext ::ImportMapConfig {
base_url : member . config_file . specifier . clone ( ) ,
import_map_value : member
. config_file
. to_import_map_value_from_imports ( ) ,
2023-11-17 01:28:38 +00:00
} )
. collect ( ) ;
2024-02-24 05:21:09 +00:00
let ( import_map_url , import_map ) =
::import_map ::ext ::create_synthetic_import_map (
base_import_map_config ,
children_configs ,
2023-11-17 01:28:38 +00:00
) ;
2024-02-24 05:21:09 +00:00
log ::debug! (
" Workspace config generated this import map {} " ,
serde_json ::to_string_pretty ( & import_map ) . unwrap ( )
) ;
let maybe_import_map_result =
import_map ::import_map_from_value ( import_map_url , import_map ) . map ( Some ) ;
return maybe_import_map_result ;
2023-11-17 01:28:38 +00:00
}
2024-02-24 05:21:09 +00:00
if self
. overrides
. import_map_specifier
. as_ref ( )
. map ( | s | s . is_none ( ) )
= = Some ( true )
{
// overrode to not use an import map
return Ok ( None ) ;
}
2024-01-24 22:44:06 +00:00
2024-02-24 05:21:09 +00:00
let import_map_specifier = self . resolve_specified_import_map_specifier ( ) ? ;
resolve_import_map (
import_map_specifier . as_ref ( ) ,
2023-02-20 18:14:06 +00:00
self . maybe_config_file ( ) . as_ref ( ) ,
2023-01-25 21:51:04 +00:00
file_fetcher ,
)
. await
2023-01-25 20:13:40 +00:00
}
2023-12-19 12:37:22 +00:00
pub fn node_ipc_fd ( & self ) -> Option < i64 > {
2023-12-13 10:14:16 +00:00
let maybe_node_channel_fd = std ::env ::var ( " DENO_CHANNEL_FD " ) . ok ( ) ;
if let Some ( node_channel_fd ) = maybe_node_channel_fd {
// Remove so that child processes don't inherit this environment variable.
std ::env ::remove_var ( " DENO_CHANNEL_FD " ) ;
2023-12-19 12:37:22 +00:00
node_channel_fd . parse ::< i64 > ( ) . ok ( )
2023-12-13 10:14:16 +00:00
} else {
None
}
}
2024-02-15 04:50:17 +00:00
pub fn enable_future_features ( & self ) -> bool {
2024-03-21 02:02:44 +00:00
* DENO_FUTURE
2024-02-15 04:50:17 +00:00
}
2023-03-22 14:15:53 +00:00
pub fn resolve_main_module ( & self ) -> Result < ModuleSpecifier , AnyError > {
match & self . flags . subcommand {
DenoSubcommand ::Bundle ( bundle_flags ) = > {
resolve_url_or_path ( & bundle_flags . source_file , self . initial_cwd ( ) )
. map_err ( AnyError ::from )
}
DenoSubcommand ::Compile ( compile_flags ) = > {
resolve_url_or_path ( & compile_flags . source_file , self . initial_cwd ( ) )
. map_err ( AnyError ::from )
}
DenoSubcommand ::Eval ( _ ) = > {
resolve_url_or_path ( " ./$deno$eval " , self . initial_cwd ( ) )
. map_err ( AnyError ::from )
}
DenoSubcommand ::Repl ( _ ) = > {
resolve_url_or_path ( " ./$deno$repl.ts " , self . initial_cwd ( ) )
. map_err ( AnyError ::from )
}
DenoSubcommand ::Run ( run_flags ) = > {
if run_flags . is_stdin ( ) {
std ::env ::current_dir ( )
. context ( " Unable to get CWD " )
. and_then ( | cwd | {
2023-03-23 16:45:43 +00:00
resolve_url_or_path ( " ./$deno$stdin.ts " , & cwd )
. map_err ( AnyError ::from )
2023-03-22 14:15:53 +00:00
} )
2023-06-15 17:09:37 +00:00
} else if run_flags . watch . is_some ( ) {
2023-03-22 14:15:53 +00:00
resolve_url_or_path ( & run_flags . script , self . initial_cwd ( ) )
. map_err ( AnyError ::from )
} else if NpmPackageReqReference ::from_str ( & run_flags . script ) . is_ok ( ) {
ModuleSpecifier ::parse ( & run_flags . script ) . map_err ( AnyError ::from )
} else {
resolve_url_or_path ( & run_flags . script , self . initial_cwd ( ) )
. map_err ( AnyError ::from )
}
}
_ = > {
bail! ( " No main module. " )
}
}
}
pub fn resolve_file_header_overrides (
& self ,
) -> HashMap < ModuleSpecifier , HashMap < String , String > > {
let maybe_main_specifier = self . resolve_main_module ( ) . ok ( ) ;
// TODO(Cre3per): This mapping moved to deno_ast with https://github.com/denoland/deno_ast/issues/133 and should be available in deno_ast >= 0.25.0 via `MediaType::from_path(...).as_media_type()`
let maybe_content_type =
self . flags . ext . as_ref ( ) . and_then ( | el | match el . as_str ( ) {
" ts " = > Some ( " text/typescript " ) ,
" tsx " = > Some ( " text/tsx " ) ,
" js " = > Some ( " text/javascript " ) ,
" jsx " = > Some ( " text/jsx " ) ,
_ = > None ,
} ) ;
if let ( Some ( main_specifier ) , Some ( content_type ) ) =
( maybe_main_specifier , maybe_content_type )
{
HashMap ::from ( [ (
main_specifier ,
HashMap ::from ( [ ( " content-type " . to_string ( ) , content_type . to_string ( ) ) ] ) ,
) ] )
} else {
HashMap ::default ( )
}
}
2023-10-02 21:53:55 +00:00
pub fn resolve_npm_resolution_snapshot (
2023-03-13 03:32:59 +00:00
& self ,
2023-04-13 14:47:45 +00:00
) -> Result < Option < ValidSerializedNpmResolutionSnapshot > , AnyError > {
2023-10-25 18:39:00 +00:00
if let Some ( NpmProcessStateKind ::Snapshot ( snapshot ) ) =
NPM_PROCESS_STATE . as_ref ( ) . map ( | s | & s . kind )
{
2023-02-20 18:14:06 +00:00
// TODO(bartlomieju): remove this clone
2023-10-25 18:39:00 +00:00
Ok ( Some ( snapshot . clone ( ) . into_valid ( ) ? ) )
2023-10-02 21:53:55 +00:00
} else {
Ok ( None )
2023-02-20 18:14:06 +00:00
}
}
// If the main module should be treated as being in an npm package.
// This is triggered via a secret environment variable which is used
// for functionality like child_process.fork. Users should NOT depend
// on this functionality.
pub fn is_npm_main ( & self ) -> bool {
2023-02-23 01:16:16 +00:00
NPM_PROCESS_STATE . is_some ( )
2023-02-20 18:14:06 +00:00
}
2022-06-30 00:41:48 +00:00
/// Overrides the import map specifier to use.
pub fn set_import_map_specifier ( & mut self , path : Option < ModuleSpecifier > ) {
self . overrides . import_map_specifier = Some ( path ) ;
2022-06-28 20:45:55 +00:00
}
2023-02-23 01:16:16 +00:00
pub fn has_node_modules_dir ( & self ) -> bool {
2024-04-05 14:34:51 +00:00
self . maybe_node_modules_folder . is_some ( )
2023-02-23 01:16:16 +00:00
}
2023-02-20 18:14:06 +00:00
2024-04-05 14:34:51 +00:00
pub fn node_modules_dir_path ( & self ) -> Option < & PathBuf > {
self . maybe_node_modules_folder . as_ref ( )
2022-12-16 22:41:51 +00:00
}
2023-10-02 21:53:55 +00:00
pub fn with_node_modules_dir_path ( & self , path : PathBuf ) -> Self {
Self {
flags : self . flags . clone ( ) ,
initial_cwd : self . initial_cwd . clone ( ) ,
maybe_node_modules_folder : Some ( path ) ,
maybe_vendor_folder : self . maybe_vendor_folder . clone ( ) ,
maybe_config_file : self . maybe_config_file . clone ( ) ,
maybe_package_json : self . maybe_package_json . clone ( ) ,
maybe_lockfile : self . maybe_lockfile . clone ( ) ,
2023-11-17 01:28:38 +00:00
maybe_workspace_config : self . maybe_workspace_config . clone ( ) ,
2023-10-02 21:53:55 +00:00
overrides : self . overrides . clone ( ) ,
2024-01-18 23:30:49 +00:00
disable_deprecated_api_warning : self . disable_deprecated_api_warning ,
2024-01-26 15:41:16 +00:00
verbose_deprecated_api_warning : self . verbose_deprecated_api_warning ,
2023-10-02 21:53:55 +00:00
}
}
2023-05-19 22:39:27 +00:00
pub fn node_modules_dir_enablement ( & self ) -> Option < bool > {
self . flags . node_modules_dir . or_else ( | | {
self
. maybe_config_file
. as_ref ( )
2024-02-24 05:21:09 +00:00
. and_then ( | c | c . json . node_modules_dir )
2023-05-19 22:39:27 +00:00
} )
}
2023-08-07 01:56:56 +00:00
pub fn vendor_dir_path ( & self ) -> Option < & PathBuf > {
self . maybe_vendor_folder . as_ref ( )
2023-08-02 00:49:09 +00:00
}
2023-05-01 20:42:05 +00:00
pub fn resolve_root_cert_store_provider (
& self ,
) -> Arc < dyn RootCertStoreProvider > {
Arc ::new ( CliRootCertStoreProvider ::new (
2022-06-28 20:45:55 +00:00
None ,
self . flags . ca_stores . clone ( ) ,
2023-01-18 00:18:24 +00:00
self . flags . ca_data . clone ( ) ,
2023-05-01 20:42:05 +00:00
) )
2022-06-28 20:45:55 +00:00
}
pub fn resolve_ts_config_for_emit (
& self ,
config_type : TsConfigType ,
2022-11-25 23:29:48 +00:00
) -> Result < TsConfigForEmit , AnyError > {
2024-01-24 13:16:23 +00:00
let result = deno_config ::get_ts_config_for_emit (
2022-11-25 23:29:48 +00:00
config_type ,
self . maybe_config_file . as_ref ( ) ,
2024-01-24 13:16:23 +00:00
) ;
match result {
Ok ( mut ts_config_for_emit ) = > {
if matches! ( self . flags . subcommand , DenoSubcommand ::Bundle ( .. ) ) {
// For backwards compatibility, force `experimentalDecorators` setting
// to true.
* ts_config_for_emit
. ts_config
. 0
. get_mut ( " experimentalDecorators " )
. unwrap ( ) = serde_json ::Value ::Bool ( true ) ;
}
Ok ( ts_config_for_emit )
}
Err ( err ) = > Err ( err ) ,
}
2022-06-28 20:45:55 +00:00
}
2024-04-11 23:17:10 +00:00
pub fn resolve_inspector_server (
& self ,
) -> Result < Option < InspectorServer > , AnyError > {
2022-12-12 14:33:30 +00:00
let maybe_inspect_host = self
. flags
. inspect
. or ( self . flags . inspect_brk )
. or ( self . flags . inspect_wait ) ;
2024-04-11 23:17:10 +00:00
let Some ( host ) = maybe_inspect_host else {
return Ok ( None ) ;
} ;
Ok ( Some ( InspectorServer ::new ( host , version ::get_user_agent ( ) ) ? ) )
2022-06-28 20:45:55 +00:00
}
2023-05-01 18:35:23 +00:00
pub fn maybe_lockfile ( & self ) -> Option < Arc < Mutex < Lockfile > > > {
2022-11-02 15:32:30 +00:00
self . maybe_lockfile . clone ( )
2022-06-28 20:45:55 +00:00
}
pub fn resolve_tasks_config (
& self ,
2024-03-27 21:14:27 +00:00
) -> Result < IndexMap < String , deno_config ::Task > , AnyError > {
2022-06-28 20:45:55 +00:00
if let Some ( config_file ) = & self . maybe_config_file {
config_file . resolve_tasks_config ( )
2023-02-23 03:45:35 +00:00
} else if self . maybe_package_json . is_some ( ) {
Ok ( Default ::default ( ) )
2022-06-28 20:45:55 +00:00
} else {
bail! ( " No config file found " )
}
}
2022-08-24 17:36:05 +00:00
/// Return the JSX import source configuration.
pub fn to_maybe_jsx_import_source_config (
& self ,
2023-07-27 16:15:39 +00:00
) -> Result < Option < JsxImportSourceConfig > , AnyError > {
match self . maybe_config_file . as_ref ( ) {
Some ( config ) = > config . to_maybe_jsx_import_source_config ( ) ,
None = > Ok ( None ) ,
}
2022-06-28 20:45:55 +00:00
}
/// Return any imports that should be brought into the scope of the module
/// graph.
2023-08-24 09:21:34 +00:00
pub fn to_maybe_imports (
& self ,
) -> Result < Vec < deno_graph ::ReferrerImports > , AnyError > {
2022-06-28 20:45:55 +00:00
if let Some ( config_file ) = & self . maybe_config_file {
2023-08-24 09:21:34 +00:00
config_file . to_maybe_imports ( ) . map ( | maybe_imports | {
maybe_imports
. into_iter ( )
. map ( | ( referrer , imports ) | deno_graph ::ReferrerImports {
referrer ,
imports ,
} )
. collect ( )
} )
2022-06-28 20:45:55 +00:00
} else {
2023-02-10 03:00:23 +00:00
Ok ( Vec ::new ( ) )
2022-06-28 20:45:55 +00:00
}
}
2023-02-20 18:14:06 +00:00
pub fn maybe_config_file ( & self ) -> & Option < ConfigFile > {
2023-01-07 20:22:09 +00:00
& self . maybe_config_file
}
2023-11-17 01:28:38 +00:00
pub fn maybe_workspace_config ( & self ) -> & Option < WorkspaceConfig > {
& self . maybe_workspace_config
}
2023-02-20 18:14:06 +00:00
pub fn maybe_package_json ( & self ) -> & Option < PackageJson > {
& self . maybe_package_json
}
2023-03-03 22:27:05 +00:00
pub fn maybe_package_json_deps ( & self ) -> Option < PackageJsonDeps > {
2023-02-23 03:45:35 +00:00
if matches! (
self . flags . subcommand ,
DenoSubcommand ::Task ( TaskFlags { task : None , .. } )
) {
// don't have any package json dependencies for deno task with no args
2023-03-03 22:27:05 +00:00
None
2023-02-20 18:14:06 +00:00
} else {
2023-03-03 22:27:05 +00:00
self
. maybe_package_json ( )
. as_ref ( )
. map ( package_json ::get_local_package_json_version_reqs )
2023-02-20 18:14:06 +00:00
}
}
2023-01-07 20:22:09 +00:00
pub fn resolve_fmt_options (
& self ,
fmt_flags : FmtFlags ,
) -> Result < FmtOptions , AnyError > {
let maybe_fmt_config = if let Some ( config_file ) = & self . maybe_config_file {
config_file . to_fmt_config ( ) ?
2022-06-28 20:45:55 +00:00
} else {
2023-01-07 20:22:09 +00:00
None
} ;
2024-01-08 17:18:42 +00:00
FmtOptions ::resolve ( maybe_fmt_config , Some ( fmt_flags ) , & self . initial_cwd )
2022-06-28 20:45:55 +00:00
}
2023-01-07 20:22:09 +00:00
pub fn resolve_lint_options (
& self ,
lint_flags : LintFlags ,
) -> Result < LintOptions , AnyError > {
let maybe_lint_config = if let Some ( config_file ) = & self . maybe_config_file {
config_file . to_lint_config ( ) ?
2022-07-18 19:12:19 +00:00
} else {
2023-01-07 20:22:09 +00:00
None
} ;
2024-01-08 17:18:42 +00:00
LintOptions ::resolve ( maybe_lint_config , Some ( lint_flags ) , & self . initial_cwd )
2022-07-18 19:12:19 +00:00
}
2024-01-08 17:18:42 +00:00
pub fn resolve_config_excludes ( & self ) -> Result < PathOrPatternSet , AnyError > {
2024-01-16 00:15:39 +00:00
let maybe_config_files = if let Some ( config_file ) = & self . maybe_config_file
2024-01-04 01:43:17 +00:00
{
2024-03-08 01:16:32 +00:00
Some ( config_file . to_files_config ( ) ? )
2024-01-04 01:43:17 +00:00
} else {
None
} ;
2024-01-16 00:15:39 +00:00
Ok ( maybe_config_files . map ( | f | f . exclude ) . unwrap_or_default ( ) )
2024-01-04 01:43:17 +00:00
}
2023-01-07 20:22:09 +00:00
pub fn resolve_test_options (
& self ,
test_flags : TestFlags ,
) -> Result < TestOptions , AnyError > {
let maybe_test_config = if let Some ( config_file ) = & self . maybe_config_file {
config_file . to_test_config ( ) ?
2022-12-10 01:30:47 +00:00
} else {
2023-01-07 20:22:09 +00:00
None
} ;
2024-01-08 17:18:42 +00:00
TestOptions ::resolve ( maybe_test_config , Some ( test_flags ) , & self . initial_cwd )
2022-12-10 01:30:47 +00:00
}
2023-01-07 20:22:09 +00:00
pub fn resolve_bench_options (
& self ,
bench_flags : BenchFlags ,
) -> Result < BenchOptions , AnyError > {
let maybe_bench_config = if let Some ( config_file ) = & self . maybe_config_file
{
config_file . to_bench_config ( ) ?
2022-06-28 20:45:55 +00:00
} else {
2023-01-07 20:22:09 +00:00
None
} ;
2024-01-08 17:18:42 +00:00
BenchOptions ::resolve (
maybe_bench_config ,
Some ( bench_flags ) ,
& self . initial_cwd ,
)
2022-06-28 20:45:55 +00:00
}
2024-02-20 21:29:57 +00:00
pub fn resolve_deno_graph_workspace_members (
& self ,
) -> Result < Vec < deno_graph ::WorkspaceMember > , AnyError > {
fn workspace_config_to_workspace_members (
workspace_config : & deno_config ::WorkspaceConfig ,
) -> Result < Vec < deno_graph ::WorkspaceMember > , AnyError > {
workspace_config
. members
. iter ( )
. map ( | member | {
config_to_workspace_member ( & member . config_file ) . with_context ( | | {
format! (
" Failed to resolve configuration for '{}' workspace member at '{}' " ,
member . member_name ,
member . config_file . specifier . as_str ( )
)
} )
} )
. collect ( )
}
fn config_to_workspace_member (
config : & ConfigFile ,
) -> Result < deno_graph ::WorkspaceMember , AnyError > {
let nv = deno_semver ::package ::PackageNv {
name : match & config . json . name {
Some ( name ) = > name . clone ( ) ,
None = > bail! ( " Missing 'name' field in config file. " ) ,
} ,
version : match & config . json . version {
Some ( name ) = > deno_semver ::Version ::parse_standard ( name ) ? ,
None = > bail! ( " Missing 'version' field in config file. " ) ,
} ,
} ;
Ok ( deno_graph ::WorkspaceMember {
base : config . specifier . join ( " ./ " ) . unwrap ( ) ,
nv ,
exports : config . to_exports_config ( ) ? . into_map ( ) ,
} )
}
let maybe_workspace_config = self . maybe_workspace_config ( ) ;
if let Some ( wc ) = maybe_workspace_config {
workspace_config_to_workspace_members ( wc )
} else {
Ok (
self
. maybe_config_file ( )
. as_ref ( )
. and_then ( | c | match config_to_workspace_member ( c ) {
Ok ( m ) = > Some ( vec! [ m ] ) ,
Err ( e ) = > {
log ::debug! ( " Deno config was not a package: {:#} " , e ) ;
None
}
} )
. unwrap_or_default ( ) ,
)
}
}
2022-06-28 20:45:55 +00:00
/// Vector of user script CLI arguments.
pub fn argv ( & self ) -> & Vec < String > {
& self . flags . argv
}
2023-01-18 00:18:24 +00:00
pub fn ca_data ( & self ) -> & Option < CaData > {
& self . flags . ca_data
2022-12-08 16:50:09 +00:00
}
pub fn ca_stores ( & self ) -> & Option < Vec < String > > {
& self . flags . ca_stores
}
2022-06-28 20:45:55 +00:00
pub fn check_js ( & self ) -> bool {
self
. maybe_config_file
. as_ref ( )
. map ( | cf | cf . get_check_js ( ) )
. unwrap_or ( false )
}
2022-08-12 19:21:17 +00:00
pub fn coverage_dir ( & self ) -> Option < String > {
2023-06-15 17:09:37 +00:00
match & self . flags . subcommand {
DenoSubcommand ::Test ( test ) = > test
2022-08-12 19:21:17 +00:00
. coverage_dir
. as_ref ( )
. map ( ToOwned ::to_owned )
2023-06-15 17:09:37 +00:00
. or_else ( | | env ::var ( " DENO_UNSTABLE_COVERAGE_DIR " ) . ok ( ) ) ,
_ = > None ,
2022-08-12 19:21:17 +00:00
}
2022-06-28 20:45:55 +00:00
}
2023-11-05 21:27:36 +00:00
pub fn enable_op_summary_metrics ( & self ) -> bool {
self . flags . enable_op_summary_metrics
| | matches! (
self . flags . subcommand ,
2023-12-17 10:11:07 +00:00
DenoSubcommand ::Test ( _ )
| DenoSubcommand ::Repl ( _ )
| DenoSubcommand ::Jupyter ( _ )
2023-11-05 21:27:36 +00:00
)
}
2022-06-28 20:45:55 +00:00
pub fn enable_testing_features ( & self ) -> bool {
self . flags . enable_testing_features
}
2023-03-22 14:15:53 +00:00
pub fn ext_flag ( & self ) -> & Option < String > {
& self . flags . ext
}
2023-10-31 00:25:58 +00:00
pub fn has_hmr ( & self ) -> bool {
if let DenoSubcommand ::Run ( RunFlags {
watch : Some ( WatchFlagsWithPaths { hmr , .. } ) ,
..
} ) = & self . flags . subcommand
{
* hmr
} else {
false
}
}
2022-07-19 15:58:18 +00:00
/// If the --inspect or --inspect-brk flags are used.
pub fn is_inspecting ( & self ) -> bool {
2022-12-12 14:33:30 +00:00
self . flags . inspect . is_some ( )
| | self . flags . inspect_brk . is_some ( )
| | self . flags . inspect_wait . is_some ( )
2022-07-19 15:58:18 +00:00
}
2022-06-28 20:45:55 +00:00
pub fn inspect_brk ( & self ) -> Option < SocketAddr > {
self . flags . inspect_brk
}
2022-12-12 14:33:30 +00:00
pub fn inspect_wait ( & self ) -> Option < SocketAddr > {
self . flags . inspect_wait
}
2022-06-28 20:45:55 +00:00
pub fn log_level ( & self ) -> Option < log ::Level > {
self . flags . log_level
}
2022-11-30 22:25:20 +00:00
pub fn is_quiet ( & self ) -> bool {
self
. log_level ( )
. map ( | l | l = = log ::Level ::Error )
. unwrap_or ( false )
}
2022-12-08 16:50:09 +00:00
pub fn location_flag ( & self ) -> & Option < Url > {
& self . flags . location
2022-06-28 20:45:55 +00:00
}
2023-05-25 18:27:45 +00:00
pub fn maybe_custom_root ( & self ) -> & Option < PathBuf > {
& self . flags . cache_path
2022-06-28 20:45:55 +00:00
}
2022-12-08 16:50:09 +00:00
pub fn no_prompt ( & self ) -> bool {
resolve_no_prompt ( & self . flags )
}
2022-06-28 20:45:55 +00:00
pub fn no_remote ( & self ) -> bool {
self . flags . no_remote
}
2022-09-07 13:33:51 +00:00
pub fn no_npm ( & self ) -> bool {
self . flags . no_npm
}
2024-01-22 21:31:12 +00:00
pub fn no_config ( & self ) -> bool {
self . flags . config_flag = = deno_config ::ConfigFlag ::Disabled
}
2022-06-28 20:45:55 +00:00
pub fn permissions_options ( & self ) -> PermissionsOptions {
2022-12-08 16:50:09 +00:00
PermissionsOptions {
2024-03-13 16:07:24 +00:00
allow_all : self . flags . allow_all ,
2022-12-08 16:50:09 +00:00
allow_env : self . flags . allow_env . clone ( ) ,
2023-08-03 11:19:19 +00:00
deny_env : self . flags . deny_env . clone ( ) ,
2022-12-08 16:50:09 +00:00
allow_hrtime : self . flags . allow_hrtime ,
2023-08-03 11:19:19 +00:00
deny_hrtime : self . flags . deny_hrtime ,
2022-12-08 16:50:09 +00:00
allow_net : self . flags . allow_net . clone ( ) ,
2023-08-03 11:19:19 +00:00
deny_net : self . flags . deny_net . clone ( ) ,
2024-03-14 23:53:46 +00:00
allow_ffi : convert_option_str_to_path_buf (
& self . flags . allow_ffi ,
self . initial_cwd ( ) ,
) ,
deny_ffi : convert_option_str_to_path_buf (
& self . flags . deny_ffi ,
self . initial_cwd ( ) ,
) ,
allow_read : convert_option_str_to_path_buf (
& self . flags . allow_read ,
self . initial_cwd ( ) ,
) ,
deny_read : convert_option_str_to_path_buf (
& self . flags . deny_read ,
self . initial_cwd ( ) ,
) ,
2022-12-08 16:50:09 +00:00
allow_run : self . flags . allow_run . clone ( ) ,
2023-08-03 11:19:19 +00:00
deny_run : self . flags . deny_run . clone ( ) ,
2022-12-08 16:50:09 +00:00
allow_sys : self . flags . allow_sys . clone ( ) ,
2023-08-03 11:19:19 +00:00
deny_sys : self . flags . deny_sys . clone ( ) ,
2024-03-14 23:53:46 +00:00
allow_write : convert_option_str_to_path_buf (
& self . flags . allow_write ,
self . initial_cwd ( ) ,
) ,
deny_write : convert_option_str_to_path_buf (
& self . flags . deny_write ,
self . initial_cwd ( ) ,
) ,
2022-12-08 16:50:09 +00:00
prompt : ! self . no_prompt ( ) ,
}
2022-06-28 20:45:55 +00:00
}
pub fn reload_flag ( & self ) -> bool {
self . flags . reload
}
pub fn seed ( & self ) -> Option < u64 > {
self . flags . seed
}
pub fn sub_command ( & self ) -> & DenoSubcommand {
& self . flags . subcommand
}
2023-11-10 17:41:24 +00:00
pub fn strace_ops ( & self ) -> & Option < Vec < String > > {
& self . flags . strace_ops
}
2023-12-06 21:36:06 +00:00
pub fn take_binary_npm_command_name ( & self ) -> Option < String > {
match self . sub_command ( ) {
DenoSubcommand ::Run ( flags ) = > {
const NPM_CMD_NAME_ENV_VAR_NAME : & str = " DENO_INTERNAL_NPM_CMD_NAME " ;
match std ::env ::var ( NPM_CMD_NAME_ENV_VAR_NAME ) {
Ok ( var ) = > {
// remove the env var so that child sub processes won't pick this up
std ::env ::remove_var ( NPM_CMD_NAME_ENV_VAR_NAME ) ;
Some ( var )
}
Err ( _ ) = > NpmPackageReqReference ::from_str ( & flags . script )
. ok ( )
. map ( | req_ref | npm_pkg_req_ref_to_binary_command ( & req_ref ) ) ,
}
}
_ = > None ,
}
}
2022-06-28 20:45:55 +00:00
pub fn type_check_mode ( & self ) -> TypeCheckMode {
self . flags . type_check_mode
}
2022-12-08 16:50:09 +00:00
pub fn unsafely_ignore_certificate_errors ( & self ) -> & Option < Vec < String > > {
& self . flags . unsafely_ignore_certificate_errors
2022-06-28 20:45:55 +00:00
}
2024-01-22 17:37:28 +00:00
pub fn legacy_unstable_flag ( & self ) -> bool {
self . flags . unstable_config . legacy_flag_enabled
2022-06-28 20:45:55 +00:00
}
2023-11-01 23:04:54 +00:00
pub fn unstable_bare_node_builtins ( & self ) -> bool {
2024-01-22 17:37:28 +00:00
self . flags . unstable_config . bare_node_builtins
2023-10-20 04:02:08 +00:00
| | self
. maybe_config_file ( )
. as_ref ( )
2023-12-07 00:03:18 +00:00
. map ( | c | c . has_unstable ( " bare-node-builtins " ) )
2023-10-20 04:02:08 +00:00
. unwrap_or ( false )
}
2024-04-02 22:43:03 +00:00
pub fn use_byonm ( & self ) -> bool {
2024-04-05 14:34:51 +00:00
if self . enable_future_features ( )
& & self . node_modules_dir_enablement ( ) . is_none ( )
& & self . maybe_package_json . is_some ( )
{
return true ;
}
2024-04-02 22:43:03 +00:00
2024-04-05 14:34:51 +00:00
// check if enabled via unstable
2024-01-22 17:37:28 +00:00
self . flags . unstable_config . byonm
2023-10-25 18:39:00 +00:00
| | NPM_PROCESS_STATE
. as_ref ( )
. map ( | s | matches! ( s . kind , NpmProcessStateKind ::Byonm ) )
. unwrap_or ( false )
| | self
. maybe_config_file ( )
. as_ref ( )
2023-12-07 00:03:18 +00:00
. map ( | c | c . has_unstable ( " byonm " ) )
. unwrap_or ( false )
}
pub fn unstable_sloppy_imports ( & self ) -> bool {
2024-01-22 17:37:28 +00:00
self . flags . unstable_config . sloppy_imports
2023-12-07 00:03:18 +00:00
| | self
. maybe_config_file ( )
. as_ref ( )
. map ( | c | c . has_unstable ( " sloppy-imports " ) )
2023-10-25 18:39:00 +00:00
. unwrap_or ( false )
}
2023-11-01 22:15:08 +00:00
pub fn unstable_features ( & self ) -> Vec < String > {
let mut from_config_file = self
. maybe_config_file ( )
. as_ref ( )
. map ( | c | c . json . unstable . clone ( ) )
. unwrap_or_default ( ) ;
2024-01-22 17:37:28 +00:00
from_config_file . extend_from_slice ( & self . flags . unstable_config . features ) ;
2023-11-01 22:15:08 +00:00
from_config_file
}
2022-12-08 16:50:09 +00:00
pub fn v8_flags ( & self ) -> & Vec < String > {
& self . flags . v8_flags
}
2024-04-17 14:19:55 +00:00
pub fn code_cache_enabled ( & self ) -> bool {
self . flags . code_cache_enabled
}
2023-06-15 17:09:37 +00:00
pub fn watch_paths ( & self ) -> Vec < PathBuf > {
2024-03-14 23:53:46 +00:00
let mut full_paths = Vec ::new ( ) ;
if let DenoSubcommand ::Run ( RunFlags {
2023-06-15 17:09:37 +00:00
watch : Some ( WatchFlagsWithPaths { paths , .. } ) ,
..
} ) = & self . flags . subcommand
{
2024-03-14 23:53:46 +00:00
full_paths . extend ( paths . iter ( ) . map ( | path | self . initial_cwd . join ( path ) ) ) ;
}
2023-06-15 17:09:37 +00:00
if let Ok ( Some ( import_map_path ) ) = self
2024-02-24 05:21:09 +00:00
. resolve_specified_import_map_specifier ( )
2023-06-15 17:09:37 +00:00
. map ( | ms | ms . and_then ( | ref s | s . to_file_path ( ) . ok ( ) ) )
{
2024-03-14 23:53:46 +00:00
full_paths . push ( import_map_path ) ;
2023-06-15 17:09:37 +00:00
}
if let Some ( specifier ) = self . maybe_config_file_specifier ( ) {
if specifier . scheme ( ) = = " file " {
if let Ok ( path ) = specifier . to_file_path ( ) {
2024-03-14 23:53:46 +00:00
full_paths . push ( path ) ;
2023-06-14 22:29:19 +00:00
}
}
}
2024-03-14 23:53:46 +00:00
full_paths
2022-06-28 20:45:55 +00:00
}
}
2023-02-23 01:16:16 +00:00
/// Resolves the path to use for a local node_modules folder.
2023-08-02 00:49:09 +00:00
fn resolve_node_modules_folder (
2023-02-23 01:16:16 +00:00
cwd : & Path ,
flags : & Flags ,
maybe_config_file : Option < & ConfigFile > ,
maybe_package_json : Option < & PackageJson > ,
) -> Result < Option < PathBuf > , AnyError > {
2023-05-18 22:10:44 +00:00
let use_node_modules_dir = flags
. node_modules_dir
2024-02-24 05:21:09 +00:00
. or_else ( | | maybe_config_file . and_then ( | c | c . json . node_modules_dir ) )
2023-08-07 01:56:56 +00:00
. or ( flags . vendor )
2024-02-24 05:21:09 +00:00
. or_else ( | | maybe_config_file . and_then ( | c | c . json . vendor ) ) ;
2023-05-18 22:10:44 +00:00
let path = if use_node_modules_dir = = Some ( false ) {
2023-02-23 01:16:16 +00:00
return Ok ( None ) ;
} else if let Some ( state ) = & * NPM_PROCESS_STATE {
return Ok ( state . local_node_modules_path . as_ref ( ) . map ( PathBuf ::from ) ) ;
} else if let Some ( package_json_path ) = maybe_package_json . map ( | c | & c . path ) {
// always auto-discover the local_node_modules_folder when a package.json exists
package_json_path . parent ( ) . unwrap ( ) . join ( " node_modules " )
2023-05-18 22:10:44 +00:00
} else if use_node_modules_dir . is_none ( ) {
2023-02-23 01:16:16 +00:00
return Ok ( None ) ;
} else if let Some ( config_path ) = maybe_config_file
. as_ref ( )
. and_then ( | c | c . specifier . to_file_path ( ) . ok ( ) )
{
config_path . parent ( ) . unwrap ( ) . join ( " node_modules " )
} else {
cwd . join ( " node_modules " )
} ;
Ok ( Some ( canonicalize_path_maybe_not_exists ( & path ) ? ) )
}
2023-08-07 01:56:56 +00:00
fn resolve_vendor_folder (
2023-08-02 00:49:09 +00:00
cwd : & Path ,
flags : & Flags ,
maybe_config_file : Option < & ConfigFile > ,
) -> Option < PathBuf > {
2023-08-07 01:56:56 +00:00
let use_vendor_dir = flags
. vendor
2024-02-24 05:21:09 +00:00
. or_else ( | | maybe_config_file . and_then ( | c | c . json . vendor ) )
2023-08-02 00:49:09 +00:00
. unwrap_or ( false ) ;
// Unlike the node_modules directory, there is no need to canonicalize
// this directory because it's just used as a cache and the resolved
// specifier is not based on the canonicalized path (unlike the modules
// in the node_modules folder).
2023-08-07 01:56:56 +00:00
if ! use_vendor_dir {
2023-08-02 00:49:09 +00:00
None
} else if let Some ( config_path ) = maybe_config_file
. as_ref ( )
. and_then ( | c | c . specifier . to_file_path ( ) . ok ( ) )
{
2023-08-07 01:56:56 +00:00
Some ( config_path . parent ( ) . unwrap ( ) . join ( " vendor " ) )
2023-08-02 00:49:09 +00:00
} else {
2023-08-07 01:56:56 +00:00
Some ( cwd . join ( " vendor " ) )
2023-08-02 00:49:09 +00:00
}
}
2022-06-28 20:45:55 +00:00
fn resolve_import_map_specifier (
maybe_import_map_path : Option < & str > ,
maybe_config_file : Option < & ConfigFile > ,
2023-03-14 01:12:09 +00:00
current_dir : & Path ,
2022-06-28 20:45:55 +00:00
) -> Result < Option < ModuleSpecifier > , AnyError > {
if let Some ( import_map_path ) = maybe_import_map_path {
if let Some ( config_file ) = & maybe_config_file {
2024-02-24 05:21:09 +00:00
if config_file . json . import_map . is_some ( ) {
2022-07-01 15:50:16 +00:00
log ::warn! ( " {} the configuration file \" {} \" contains an entry for \" importMap \" that is being ignored. " , colors ::yellow ( " Warning " ) , config_file . specifier ) ;
2022-06-28 20:45:55 +00:00
}
}
2023-03-14 01:12:09 +00:00
let specifier =
deno_core ::resolve_url_or_path ( import_map_path , current_dir )
2023-03-21 18:13:32 +00:00
. with_context ( | | {
format! ( " Bad URL ( \" {import_map_path} \" ) for import map. " )
} ) ? ;
2024-02-24 05:21:09 +00:00
Ok ( Some ( specifier ) )
2022-06-28 20:45:55 +00:00
} else if let Some ( config_file ) = & maybe_config_file {
2024-02-24 05:21:09 +00:00
// if the config file is an import map we prefer to use it, over `importMap` field
if config_file . is_an_import_map ( ) & & config_file . json . import_map . is_some ( ) {
log ::warn! ( " {} \" importMap \" setting is ignored when \" imports \" or \" scopes \" are specified in the config file. " , colors ::yellow ( " Warning " ) ) ;
2022-06-28 20:45:55 +00:00
}
2024-02-24 05:21:09 +00:00
Ok ( None )
} else {
Ok ( None )
2022-06-28 20:45:55 +00:00
}
}
2023-04-27 14:05:20 +00:00
pub struct StorageKeyResolver ( Option < Option < String > > ) ;
impl StorageKeyResolver {
pub fn from_options ( options : & CliOptions ) -> Self {
Self ( if let Some ( location ) = & options . flags . location {
// if a location is set, then the ascii serialization of the location is
// used, unless the origin is opaque, and then no storage origin is set, as
// we can't expect the origin to be reproducible
let storage_origin = location . origin ( ) ;
if storage_origin . is_tuple ( ) {
Some ( Some ( storage_origin . ascii_serialization ( ) ) )
} else {
Some ( None )
}
} else {
// otherwise we will use the path to the config file or None to
// fall back to using the main module's path
options
. maybe_config_file
. as_ref ( )
. map ( | config_file | Some ( config_file . specifier . to_string ( ) ) )
} )
}
2023-05-01 12:59:38 +00:00
/// Creates a storage key resolver that will always resolve to being empty.
pub fn empty ( ) -> Self {
Self ( Some ( None ) )
}
2023-04-27 14:05:20 +00:00
/// Resolves the storage key to use based on the current flags, config, or main module.
pub fn resolve_storage_key (
& self ,
main_module : & ModuleSpecifier ,
) -> Option < String > {
// use the stored value or fall back to using the path of the main module.
if let Some ( maybe_value ) = & self . 0 {
maybe_value . clone ( )
} else {
Some ( main_module . to_string ( ) )
}
}
}
2023-01-07 20:22:09 +00:00
/// Collect included and ignored files. CLI flags take precedence
/// over config file, i.e. if there's `files.ignore` in config file
/// and `--ignore` CLI flag, only the flag value is taken into account.
fn resolve_files (
2024-01-16 00:15:39 +00:00
maybe_files_config : Option < FilePatterns > ,
2023-01-07 20:22:09 +00:00
maybe_file_flags : Option < FileFlags > ,
2024-01-08 17:18:42 +00:00
initial_cwd : & Path ,
) -> Result < FilePatterns , AnyError > {
2024-01-18 20:57:30 +00:00
let mut maybe_files_config = maybe_files_config
. unwrap_or_else ( | | FilePatterns ::new_with_base ( initial_cwd . to_path_buf ( ) ) ) ;
2023-01-07 20:22:09 +00:00
if let Some ( file_flags ) = maybe_file_flags {
if ! file_flags . include . is_empty ( ) {
2024-01-16 00:15:39 +00:00
maybe_files_config . include =
2024-03-08 01:16:32 +00:00
Some ( PathOrPatternSet ::from_include_relative_path_or_patterns (
2024-01-16 00:15:39 +00:00
initial_cwd ,
& file_flags . include ,
) ? ) ;
2023-01-07 20:22:09 +00:00
}
if ! file_flags . ignore . is_empty ( ) {
2024-01-16 00:15:39 +00:00
maybe_files_config . exclude =
2024-03-08 01:16:32 +00:00
PathOrPatternSet ::from_exclude_relative_path_or_patterns (
2024-01-16 00:15:39 +00:00
initial_cwd ,
& file_flags . ignore ,
) ? ;
2023-01-07 20:22:09 +00:00
}
}
2024-01-18 20:57:30 +00:00
Ok ( maybe_files_config )
2023-01-07 20:22:09 +00:00
}
2022-12-08 16:50:09 +00:00
/// Resolves the no_prompt value based on the cli flags and environment.
pub fn resolve_no_prompt ( flags : & Flags ) -> bool {
2023-02-24 19:23:07 +00:00
flags . no_prompt | | has_flag_env_var ( " DENO_NO_PROMPT " )
}
2023-05-23 01:28:36 +00:00
pub fn has_flag_env_var ( name : & str ) -> bool {
2023-02-24 19:23:07 +00:00
let value = env ::var ( name ) ;
matches! ( value . as_ref ( ) . map ( | s | s . as_str ( ) ) , Ok ( " 1 " ) )
2022-12-08 16:50:09 +00:00
}
2023-05-11 00:06:59 +00:00
pub fn npm_pkg_req_ref_to_binary_command (
req_ref : & NpmPackageReqReference ,
) -> String {
2023-08-21 09:53:52 +00:00
let binary_name = req_ref . sub_path ( ) . unwrap_or ( req_ref . req ( ) . name . as_str ( ) ) ;
2023-05-11 00:06:59 +00:00
binary_name . to_string ( )
}
2024-03-14 23:53:46 +00:00
fn convert_option_str_to_path_buf (
flag : & Option < Vec < String > > ,
initial_cwd : & Path ,
) -> Option < Vec < PathBuf > > {
if let Some ( allow_ffi_paths ) = & flag {
let mut full_paths = Vec ::new ( ) ;
full_paths
. extend ( allow_ffi_paths . iter ( ) . map ( | path | initial_cwd . join ( path ) ) ) ;
Some ( full_paths )
} else {
None
}
}
2022-06-28 20:45:55 +00:00
#[ cfg(test) ]
mod test {
2024-01-08 17:18:42 +00:00
use crate ::util ::fs ::FileCollector ;
2022-06-28 20:45:55 +00:00
use super ::* ;
2023-05-23 01:39:59 +00:00
use pretty_assertions ::assert_eq ;
2022-06-28 20:45:55 +00:00
#[ test ]
fn resolve_import_map_flags_take_precedence ( ) {
let config_text = r #" {
" importMap " : " import_map.json "
} " #;
2023-03-14 11:37:15 +00:00
let cwd = & std ::env ::current_dir ( ) . unwrap ( ) ;
2022-06-28 20:45:55 +00:00
let config_specifier =
ModuleSpecifier ::parse ( " file:///deno/deno.jsonc " ) . unwrap ( ) ;
2024-03-27 21:14:27 +00:00
let config_file = ConfigFile ::new (
config_text ,
config_specifier ,
& deno_config ::ParseOptions ::default ( ) ,
)
. unwrap ( ) ;
2023-03-14 01:12:09 +00:00
let actual = resolve_import_map_specifier (
Some ( " import-map.json " ) ,
Some ( & config_file ) ,
cwd ,
) ;
let import_map_path = cwd . join ( " import-map.json " ) ;
2022-06-28 20:45:55 +00:00
let expected_specifier =
2022-12-17 22:20:15 +00:00
ModuleSpecifier ::from_file_path ( import_map_path ) . unwrap ( ) ;
2022-06-28 20:45:55 +00:00
assert! ( actual . is_ok ( ) ) ;
let actual = actual . unwrap ( ) ;
assert_eq! ( actual , Some ( expected_specifier ) ) ;
}
#[ test ]
fn resolve_import_map_none ( ) {
let config_text = r # "{}"# ;
let config_specifier =
ModuleSpecifier ::parse ( " file:///deno/deno.jsonc " ) . unwrap ( ) ;
2024-03-27 21:14:27 +00:00
let config_file = ConfigFile ::new (
config_text ,
config_specifier ,
& deno_config ::ParseOptions ::default ( ) ,
)
. unwrap ( ) ;
2023-03-14 01:12:09 +00:00
let actual = resolve_import_map_specifier (
None ,
Some ( & config_file ) ,
& PathBuf ::from ( " / " ) ,
) ;
2022-06-28 20:45:55 +00:00
assert! ( actual . is_ok ( ) ) ;
let actual = actual . unwrap ( ) ;
assert_eq! ( actual , None ) ;
}
#[ test ]
fn resolve_import_map_no_config ( ) {
2023-03-14 01:12:09 +00:00
let actual = resolve_import_map_specifier ( None , None , & PathBuf ::from ( " / " ) ) ;
2022-06-28 20:45:55 +00:00
assert! ( actual . is_ok ( ) ) ;
let actual = actual . unwrap ( ) ;
assert_eq! ( actual , None ) ;
}
2023-04-27 14:05:20 +00:00
#[ test ]
fn storage_key_resolver_test ( ) {
let resolver = StorageKeyResolver ( None ) ;
let specifier = ModuleSpecifier ::parse ( " file:///a.ts " ) . unwrap ( ) ;
assert_eq! (
resolver . resolve_storage_key ( & specifier ) ,
Some ( specifier . to_string ( ) )
) ;
let resolver = StorageKeyResolver ( Some ( None ) ) ;
assert_eq! ( resolver . resolve_storage_key ( & specifier ) , None ) ;
let resolver = StorageKeyResolver ( Some ( Some ( " value " . to_string ( ) ) ) ) ;
assert_eq! (
resolver . resolve_storage_key ( & specifier ) ,
Some ( " value " . to_string ( ) )
) ;
2023-05-01 12:59:38 +00:00
// test empty
let resolver = StorageKeyResolver ::empty ( ) ;
assert_eq! ( resolver . resolve_storage_key ( & specifier ) , None ) ;
2023-04-27 14:05:20 +00:00
}
2023-05-23 01:39:59 +00:00
#[ test ]
fn resolve_files_test ( ) {
use test_util ::TempDir ;
let temp_dir = TempDir ::new ( ) ;
temp_dir . create_dir_all ( " data " ) ;
temp_dir . create_dir_all ( " nested " ) ;
temp_dir . create_dir_all ( " nested/foo " ) ;
temp_dir . create_dir_all ( " nested/fizz " ) ;
temp_dir . create_dir_all ( " pages " ) ;
temp_dir . write ( " data/tes.ts " , " " ) ;
temp_dir . write ( " data/test1.js " , " " ) ;
temp_dir . write ( " data/test1.ts " , " " ) ;
temp_dir . write ( " data/test12.ts " , " " ) ;
temp_dir . write ( " nested/foo/foo.ts " , " " ) ;
temp_dir . write ( " nested/foo/bar.ts " , " " ) ;
temp_dir . write ( " nested/foo/fizz.ts " , " " ) ;
temp_dir . write ( " nested/foo/bazz.ts " , " " ) ;
temp_dir . write ( " nested/fizz/foo.ts " , " " ) ;
temp_dir . write ( " nested/fizz/bar.ts " , " " ) ;
temp_dir . write ( " nested/fizz/fizz.ts " , " " ) ;
temp_dir . write ( " nested/fizz/bazz.ts " , " " ) ;
temp_dir . write ( " pages/[id].ts " , " " ) ;
2023-06-10 15:09:45 +00:00
let temp_dir_path = temp_dir . path ( ) . as_path ( ) ;
2024-03-08 01:16:32 +00:00
let error = PathOrPatternSet ::from_include_relative_path_or_patterns (
2024-01-08 17:18:42 +00:00
temp_dir_path ,
2024-01-16 00:15:39 +00:00
& [ " data/**********.ts " . to_string ( ) ] ,
2023-05-23 17:35:12 +00:00
)
. unwrap_err ( ) ;
assert! ( error . to_string ( ) . starts_with ( " Failed to expand glob " ) ) ;
2023-05-23 01:39:59 +00:00
let resolved_files = resolve_files (
2024-01-16 00:15:39 +00:00
Some ( FilePatterns {
2024-01-18 20:57:30 +00:00
base : temp_dir_path . to_path_buf ( ) ,
2024-01-16 00:15:39 +00:00
include : Some (
2024-03-08 01:16:32 +00:00
PathOrPatternSet ::from_include_relative_path_or_patterns (
2024-01-16 00:15:39 +00:00
temp_dir_path ,
& [
" data/test1.?s " . to_string ( ) ,
" nested/foo/*.ts " . to_string ( ) ,
" nested/fizz/*.ts " . to_string ( ) ,
" pages/[id].ts " . to_string ( ) ,
] ,
)
. unwrap ( ) ,
) ,
2024-03-08 01:16:32 +00:00
exclude : PathOrPatternSet ::from_exclude_relative_path_or_patterns (
2024-01-16 00:15:39 +00:00
temp_dir_path ,
& [ " nested/**/*bazz.ts " . to_string ( ) ] ,
)
. unwrap ( ) ,
2023-05-23 01:39:59 +00:00
} ) ,
None ,
2024-01-08 17:18:42 +00:00
temp_dir_path ,
2023-05-23 01:39:59 +00:00
)
. unwrap ( ) ;
2024-03-08 01:16:32 +00:00
let mut files = FileCollector ::new ( | _ | true )
2024-01-08 17:18:42 +00:00
. ignore_git_folder ( )
. ignore_node_modules ( )
. collect_file_patterns ( resolved_files )
. unwrap ( ) ;
files . sort ( ) ;
2023-05-23 01:39:59 +00:00
assert_eq! (
2024-01-08 17:18:42 +00:00
files ,
2023-05-23 01:39:59 +00:00
vec! [
2024-01-08 17:18:42 +00:00
" data/test1.js " ,
" data/test1.ts " ,
" nested/fizz/bar.ts " ,
" nested/fizz/fizz.ts " ,
" nested/fizz/foo.ts " ,
" nested/foo/bar.ts " ,
" nested/foo/fizz.ts " ,
" nested/foo/foo.ts " ,
" pages/[id].ts " ,
2023-05-23 01:39:59 +00:00
]
2024-01-08 17:18:42 +00:00
. into_iter ( )
2024-02-24 05:21:09 +00:00
. map ( | p | deno_core ::normalize_path ( temp_dir_path . join ( p ) ) )
2024-01-08 17:18:42 +00:00
. collect ::< Vec < _ > > ( )
) ;
2023-05-23 01:39:59 +00:00
}
2023-12-12 14:45:20 +00:00
#[ test ]
2024-02-14 18:30:44 +00:00
fn jsr_urls ( ) {
let reg_url = jsr_url ( ) ;
2023-12-12 14:45:20 +00:00
assert! ( reg_url . as_str ( ) . ends_with ( '/' ) ) ;
2024-02-14 18:30:44 +00:00
let reg_api_url = jsr_api_url ( ) ;
2023-12-12 14:45:20 +00:00
assert! ( reg_api_url . as_str ( ) . ends_with ( '/' ) ) ;
}
2022-06-28 20:45:55 +00:00
}