diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-09-07 15:33:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-07 15:33:51 +0200 |
commit | 4a250b2f25d3e9fea31a1effb10d3fe07c3564ae (patch) | |
tree | c15442571cf271af1014df061f1aa7f1984e0e89 /cli/npm/mod.rs | |
parent | 3b1204eb2d9c5cdf21bb92f7c8923869477f0969 (diff) |
feat: add --no-npm flag to disable npm: imports (#15673)
This commit adds "--no-npm" flag, it's similar to "--no-remote"
flag. This flag makes Deno error out if "npm:" specifier is encountered.
Diffstat (limited to 'cli/npm/mod.rs')
-rw-r--r-- | cli/npm/mod.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/cli/npm/mod.rs b/cli/npm/mod.rs index 8272974bc..d0a57c5bc 100644 --- a/cli/npm/mod.rs +++ b/cli/npm/mod.rs @@ -14,8 +14,8 @@ use std::sync::Arc; use deno_ast::ModuleSpecifier; use deno_core::anyhow::bail; use deno_core::anyhow::Context; +use deno_core::error::custom_error; use deno_core::error::AnyError; - use deno_core::futures; use deno_core::url::Url; use deno_runtime::deno_node::DenoDirNpmResolver; @@ -77,6 +77,7 @@ pub struct GlobalNpmPackageResolver { resolution: Arc<NpmResolution>, registry_url: Url, unstable: bool, + no_npm: bool, } impl GlobalNpmPackageResolver { @@ -85,12 +86,14 @@ impl GlobalNpmPackageResolver { reload: bool, cache_setting: CacheSetting, unstable: bool, + no_npm: bool, ) -> Self { Self::from_cache( NpmCache::from_deno_dir(dir, cache_setting.clone()), reload, cache_setting, unstable, + no_npm, ) } @@ -99,6 +102,7 @@ impl GlobalNpmPackageResolver { reload: bool, cache_setting: CacheSetting, unstable: bool, + no_npm: bool, ) -> Self { let api = NpmRegistryApi::new(cache.clone(), reload, cache_setting); let registry_url = api.base_url().to_owned(); @@ -109,6 +113,7 @@ impl GlobalNpmPackageResolver { resolution, registry_url, unstable, + no_npm, } } @@ -122,11 +127,28 @@ impl GlobalNpmPackageResolver { &self, packages: Vec<NpmPackageReq>, ) -> Result<(), AnyError> { - if !self.unstable && !packages.is_empty() { + assert!(!packages.is_empty()); + + if !self.unstable { bail!( "Unstable use of npm specifiers. The --unstable flag must be provided." ) } + + if self.no_npm { + let fmt_reqs = packages + .iter() + .map(|p| format!("\"{}\"", p)) + .collect::<Vec<_>>() + .join(", "); + return Err(custom_error( + "NoNpm", + format!( + "Following npm specifiers were requested: {}; but --no-npm is specified.", + fmt_reqs + ), + )); + } self.resolution.add_package_reqs(packages).await } |