Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(unstable): deno add subcommand #22520

Merged
merged 34 commits into from Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
be4e4bc
wip
bartlomieju Feb 21, 2024
c32b358
something is working
bartlomieju Feb 21, 2024
00a3699
Merge branch 'main' into deno_add
bartlomieju Feb 21, 2024
c8aa094
more resilient
bartlomieju Feb 21, 2024
5879ffb
remove deno.json
bartlomieju Feb 21, 2024
60b742e
Merge branch 'main' into deno_add
bartlomieju Feb 26, 2024
c7f18e0
cleanup
bartlomieju Feb 26, 2024
29d1a16
Merge branch 'main' into deno_add
bartlomieju Feb 27, 2024
0e7825d
make retrieval concurrent
bartlomieju Feb 27, 2024
657ce8c
improve the cli
bartlomieju Feb 27, 2024
9ea21a9
Merge branch 'main' into deno_add
bartlomieju Feb 28, 2024
bc31f4b
need to do semver resolution still and handle npm
bartlomieju Feb 28, 2024
aff61a3
handle npm
bartlomieju Feb 28, 2024
4f1d987
start writing tests
bartlomieju Feb 28, 2024
d51a6dc
add tests
bartlomieju Feb 28, 2024
0cadde8
lint
bartlomieju Feb 28, 2024
ba1a163
fix validation
bartlomieju Feb 28, 2024
231bfbe
adjust test so we can cache it
bartlomieju Feb 28, 2024
3b64bd2
add a todo
bartlomieju Feb 28, 2024
4e71e9d
update example in flags.rs
bartlomieju Feb 29, 2024
ba55df4
use assert_matches_json
bartlomieju Feb 29, 2024
c81e785
move add to the top of the file
bartlomieju Feb 29, 2024
9642410
use log::info instead of eprintln
bartlomieju Feb 29, 2024
6809f9d
lint, rename version to version_req
bartlomieju Feb 29, 2024
2840239
wip
bartlomieju Feb 29, 2024
602f743
Merge branch 'main' into deno_add
bartlomieju Feb 29, 2024
4f66610
wip
bartlomieju Feb 29, 2024
d79f757
use PackageSearchApi
bartlomieju Feb 29, 2024
f2788a9
revert test server change
bartlomieju Feb 29, 2024
ec49ef6
decode %2F as well
bartlomieju Feb 29, 2024
5e436aa
lint
bartlomieju Feb 29, 2024
a9ac65e
Merge branch 'main' into deno_add
bartlomieju Feb 29, 2024
7bee45e
revert unrelated changes
bartlomieju Feb 29, 2024
69b0082
Merge branch 'main' into deno_add
bartlomieju Feb 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 68 additions & 3 deletions cli/args/flags.rs
Expand Up @@ -35,6 +35,11 @@ pub struct FileFlags {
pub include: Vec<String>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct AddFlags {
pub packages: Vec<String>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct BenchFlags {
pub files: FileFlags,
Expand Down Expand Up @@ -307,6 +312,7 @@ pub struct PublishFlags {

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DenoSubcommand {
Add(AddFlags),
Bench(BenchFlags),
Bundle(BundleFlags),
Cache(CacheFlags),
Expand Down Expand Up @@ -760,9 +766,9 @@ impl Flags {
| Test(_) | Bench(_) | Repl(_) | Compile(_) | Publish(_) => {
std::env::current_dir().ok()
}
Bundle(_) | Completions(_) | Doc(_) | Fmt(_) | Init(_) | Install(_)
| Uninstall(_) | Jupyter(_) | Lsp | Lint(_) | Types | Upgrade(_)
| Vendor(_) => None,
Add(_) | Bundle(_) | Completions(_) | Doc(_) | Fmt(_) | Init(_)
| Install(_) | Uninstall(_) | Jupyter(_) | Lsp | Lint(_) | Types
| Upgrade(_) | Vendor(_) => None,
}
}

Expand Down Expand Up @@ -923,6 +929,7 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::error::Result<Flags> {

if let Some((subcommand, mut m)) = matches.remove_subcommand() {
match subcommand.as_str() {
"add" => add_parse(&mut flags, &mut m),
"bench" => bench_parse(&mut flags, &mut m),
"bundle" => bundle_parse(&mut flags, &mut m),
"cache" => cache_parse(&mut flags, &mut m),
Expand Down Expand Up @@ -1078,6 +1085,7 @@ fn clap_root() -> Command {
.subcommand(run_subcommand())
.defer(|cmd| {
cmd
.subcommand(add_subcommand())
.subcommand(bench_subcommand())
.subcommand(bundle_subcommand())
.subcommand(cache_subcommand())
Expand Down Expand Up @@ -1107,6 +1115,30 @@ fn clap_root() -> Command {
.after_help(ENV_VARIABLES_HELP)
}

fn add_subcommand() -> Command {
Command::new("add")
.about("Add dependencies")
.long_about(
"Add dependencies to the configuration file.

deno add @std/path

You can add multiple dependencies at once:

deno add @std/path @std/assert
",
)
.defer(|cmd| {
cmd.arg(
Arg::new("packages")
.help("List of packages to add")
.required(true)
.num_args(1..)
.action(ArgAction::Append),
)
})
}

fn bench_subcommand() -> Command {
Command::new("bench")
.about("Run benchmarks")
Expand Down Expand Up @@ -3218,6 +3250,11 @@ fn unsafely_ignore_certificate_errors_arg() -> Arg {
.value_parser(flags_net::validator)
}

fn add_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let packages = matches.remove_many::<String>("packages").unwrap().collect();
flags.subcommand = DenoSubcommand::Add(AddFlags { packages });
}

fn bench_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.type_check_mode = TypeCheckMode::Local;

Expand Down Expand Up @@ -8599,4 +8636,32 @@ mod tests {
}
);
}

#[test]
fn add_subcommand() {
let r = flags_from_vec(svec!["deno", "add"]);
r.unwrap_err();

let r = flags_from_vec(svec!["deno", "add", "@david/which"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Add(AddFlags {
packages: svec!["@david/which"],
}),
..Flags::default()
}
);

let r = flags_from_vec(svec!["deno", "add", "@david/which", "@luca/hello"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Add(AddFlags {
packages: svec!["@david/which", "@luca/hello"],
}),
..Flags::default()
}
);
}
}
4 changes: 2 additions & 2 deletions cli/lsp/mod.rs
Expand Up @@ -21,7 +21,7 @@ mod completions;
mod config;
mod diagnostics;
mod documents;
mod jsr;
pub mod jsr;
pub mod language_server;
mod logging;
mod lsp_custom;
Expand All @@ -32,7 +32,7 @@ mod performance;
mod refactor;
mod registries;
mod repl;
mod search;
pub mod search;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we should move these out of the lsp module to somewhere more common since we're going to be using this for more than the lsp now (cc @nayeemrmn)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're just using CliJsrSearchApi::versions() in jsr_find_package_and_select_version. I think we ought to be using JsrResolver::req_to_nv() there or something.. we can modify it to be usable with both fetch or cached-only logic. I'll look into it in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nayeemrmn sounds like a good idea. Could you also improve deno add to handle npm: specifiers? For now I left it erroring (there are tests that verify it).

mod semantic_tokens;
mod testing;
mod text;
Expand Down
3 changes: 3 additions & 0 deletions cli/main.rs
Expand Up @@ -88,6 +88,9 @@ fn spawn_subcommand<F: Future<Output = T> + 'static, T: SubcommandOutput>(

async fn run_subcommand(flags: Flags) -> Result<i32, AnyError> {
let handle = match flags.subcommand.clone() {
DenoSubcommand::Add(add_flags) => spawn_subcommand(async {
tools::registry::add(flags, add_flags).await
}),
DenoSubcommand::Bench(bench_flags) => spawn_subcommand(async {
if bench_flags.watch.is_some() {
tools::bench::run_benchmarks_with_watch(flags, bench_flags).await
Expand Down
2 changes: 2 additions & 0 deletions cli/tools/registry/mod.rs
Expand Up @@ -50,13 +50,15 @@ mod auth;
mod diagnostics;
mod graph;
mod paths;
mod pm;
mod provenance;
mod publish_order;
mod tar;
mod unfurl;

use auth::get_auth_method;
use auth::AuthMethod;
pub use pm::add;
use publish_order::PublishOrderGraph;
pub use unfurl::deno_json_deps;
use unfurl::SpecifierUnfurler;
Expand Down