diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-07-09 20:06:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-10 03:06:08 +0000 |
commit | ce7dc2be92499f15b4b0315bfca3ee9d61fc3c5e (patch) | |
tree | f2463a8026d6f68d288c04b8671ce26f310de9fe /cli/npm/managed/mod.rs | |
parent | eb46296e974c686896486350bb00bf428a84e9fd (diff) |
feat(node): Support executing npm package lifecycle scripts (preinstall/install/postinstall) (#24487)
Adds support for running npm package lifecycle scripts, opted into via a
new `--allow-scripts` flag.
With this PR, when running `deno cache` (or `DENO_FUTURE=1 deno
install`) you can specify the `--allow-scripts=pkg1,pkg2` flag to run
lifecycle scripts attached to the given packages.
Note at the moment this only works when `nodeModulesDir` is true (using
the local resolver).
When a package with un-run lifecycle scripts is encountered, we emit a
warning suggesting things may not work and to try running lifecycle
scripts. Additionally, if a package script implicitly requires
`node-gyp` and it's not found on the system, we emit a warning.
Extra things in this PR:
- Extracted out bits of `task.rs` into a separate module for reuse
- Added a couple fields to `process.config` in order to support
`node-gyp` (it relies on a few variables being there)
- Drive by fix to downloading new npm packages to test registry
---
TODO:
- [x] validation for allow-scripts args (make sure it looks like an npm
package)
- [x] make allow-scripts matching smarter
- [ ] figure out what issues this closes
---
Review notes:
- This adds a bunch of deps to our test registry due to using
`node-gyp`, so it's pretty noisy
Diffstat (limited to 'cli/npm/managed/mod.rs')
-rw-r--r-- | cli/npm/managed/mod.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/cli/npm/managed/mod.rs b/cli/npm/managed/mod.rs index 6022396d6..76645d1d6 100644 --- a/cli/npm/managed/mod.rs +++ b/cli/npm/managed/mod.rs @@ -29,6 +29,7 @@ use deno_semver::package::PackageReq; use resolution::AddPkgReqsResult; use crate::args::CliLockfile; +use crate::args::LifecycleScriptsConfig; use crate::args::NpmProcessState; use crate::args::NpmProcessStateKind; use crate::args::PackageJsonInstallDepsProvider; @@ -70,6 +71,7 @@ pub struct CliNpmResolverManagedCreateOptions { pub npm_system_info: NpmSystemInfo, pub package_json_deps_provider: Arc<PackageJsonInstallDepsProvider>, pub npmrc: Arc<ResolvedNpmRc>, + pub lifecycle_scripts: LifecycleScriptsConfig, } pub async fn create_managed_npm_resolver_for_lsp( @@ -98,6 +100,7 @@ pub async fn create_managed_npm_resolver_for_lsp( options.maybe_node_modules_path, options.npm_system_info, snapshot, + options.lifecycle_scripts, ) }) .await @@ -122,6 +125,7 @@ pub async fn create_managed_npm_resolver( options.maybe_node_modules_path, options.npm_system_info, snapshot, + options.lifecycle_scripts, )) } @@ -138,6 +142,7 @@ fn create_inner( node_modules_dir_path: Option<PathBuf>, npm_system_info: NpmSystemInfo, snapshot: Option<ValidSerializedNpmResolutionSnapshot>, + lifecycle_scripts: LifecycleScriptsConfig, ) -> Arc<dyn CliNpmResolver> { let resolution = Arc::new(NpmResolution::from_serialized( npm_api.clone(), @@ -160,6 +165,7 @@ fn create_inner( tarball_cache.clone(), node_modules_dir_path, npm_system_info.clone(), + lifecycle_scripts.clone(), ); Arc::new(ManagedCliNpmResolver::new( fs, @@ -172,6 +178,7 @@ fn create_inner( tarball_cache, text_only_progress_bar, npm_system_info, + lifecycle_scripts, )) } @@ -258,6 +265,7 @@ pub struct ManagedCliNpmResolver { text_only_progress_bar: ProgressBar, npm_system_info: NpmSystemInfo, top_level_install_flag: AtomicFlag, + lifecycle_scripts: LifecycleScriptsConfig, } impl std::fmt::Debug for ManagedCliNpmResolver { @@ -281,6 +289,7 @@ impl ManagedCliNpmResolver { tarball_cache: Arc<TarballCache>, text_only_progress_bar: ProgressBar, npm_system_info: NpmSystemInfo, + lifecycle_scripts: LifecycleScriptsConfig, ) -> Self { Self { fs, @@ -294,6 +303,7 @@ impl ManagedCliNpmResolver { tarball_cache, npm_system_info, top_level_install_flag: Default::default(), + lifecycle_scripts, } } @@ -578,6 +588,7 @@ impl CliNpmResolver for ManagedCliNpmResolver { self.tarball_cache.clone(), self.root_node_modules_path().map(ToOwned::to_owned), self.npm_system_info.clone(), + self.lifecycle_scripts.clone(), ), self.maybe_lockfile.clone(), self.npm_api.clone(), @@ -587,6 +598,7 @@ impl CliNpmResolver for ManagedCliNpmResolver { self.tarball_cache.clone(), self.text_only_progress_bar.clone(), self.npm_system_info.clone(), + self.lifecycle_scripts.clone(), )) } |