From 4a250b2f25d3e9fea31a1effb10d3fe07c3564ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 7 Sep 2022 15:33:51 +0200 Subject: 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. --- cli/npm/mod.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'cli/npm/mod.rs') 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, 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, ) -> 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::>() + .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 } -- cgit v1.2.3