Skip to content

Commit fde9381

Browse files
authored
feat(unstable): add support for npm specifier cli arguments for 'deno cache' (#16141)
This commit adds support for npm specifier in "deno cache" subcommand. ``` $ deno cache --unstable npm:vite npm:chalk https://deno.land/std/http/file_server.ts ``` Besides downloading requested npm package(s), it will also download necessary code from "std/node/".
1 parent 5b097fd commit fde9381

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

cli/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ async fn load_and_type_check(
393393

394394
for file in files {
395395
let specifier = resolve_url_or_path(file)?;
396+
397+
// TODO(bartlomieju): in the future (after all relevant deno subcommands
398+
// have support for npm: specifiers), it would be good to unify this code
399+
// in `ProcState::prepare_module_load`.
400+
if let Ok(package_ref) = NpmPackageReference::from_specifier(&specifier) {
401+
ps.npm_resolver
402+
.add_package_reqs(vec![package_ref.req.clone()])
403+
.await?;
404+
ps.prepare_node_std_graph().await?;
405+
continue;
406+
}
407+
396408
ps.prepare_module_load(
397409
vec![specifier],
398410
false,

cli/tests/integration/npm_tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ itest!(error_version_after_subpath {
203203
exit_code: 1,
204204
});
205205

206+
itest!(deno_cache {
207+
args: "cache --unstable --reload npm:chalk npm:mkdirp",
208+
output: "npm/deno_cache.out",
209+
envs: env_vars(),
210+
http_server: true,
211+
});
212+
206213
#[test]
207214
fn parallel_downloading() {
208215
let (out, _err) = util::run_and_collect_output_with_args(

cli/tests/testdata/npm/deno_cache.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Download http://localhost:4545/npm/registry/chalk
2+
Download http://localhost:4545/npm/registry/chalk/chalk-5.0.1.tgz
3+
Download http://localhost:4545/npm/registry/mkdirp
4+
Download http://localhost:4545/npm/registry/chalk/chalk-5.0.1.tgz
5+
Download http://localhost:4545/npm/registry/mkdirp/mkdirp-1.0.4.tgz

0 commit comments

Comments
 (0)