fix(add): create deno.json when running deno add jsr:<pkg> (#26275)

Fixes https://github.com/denoland/deno/issues/26119.

Originally I wanted to put them in package.json if there's no deno.json,
but on second thought it makes more sense to just create a deno.json
This commit is contained in:
Nathan Whitaker 2024-10-15 09:38:42 -07:00 committed by GitHub
parent 533a9b1086
commit 797405fc61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 4 deletions

View File

@ -400,14 +400,17 @@ impl std::fmt::Display for AddCommandName {
fn load_configs(
flags: &Arc<Flags>,
has_jsr_specifiers: impl FnOnce() -> bool,
) -> Result<(CliFactory, Option<NpmConfig>, Option<DenoConfig>), AnyError> {
let cli_factory = CliFactory::from_flags(flags.clone());
let options = cli_factory.cli_options()?;
let npm_config = NpmConfig::from_options(options)?;
let (cli_factory, deno_config) = match DenoConfig::from_options(options)? {
Some(config) => (cli_factory, Some(config)),
None if npm_config.is_some() => (cli_factory, None),
None => {
None if npm_config.is_some() && !has_jsr_specifiers() => {
(cli_factory, None)
}
_ => {
let factory = create_deno_json(flags, options)?;
let options = factory.cli_options()?.clone();
(
@ -427,7 +430,9 @@ pub async fn add(
add_flags: AddFlags,
cmd_name: AddCommandName,
) -> Result<(), AnyError> {
let (cli_factory, npm_config, deno_config) = load_configs(&flags)?;
let (cli_factory, npm_config, deno_config) = load_configs(&flags, || {
add_flags.packages.iter().any(|s| s.starts_with("jsr:"))
})?;
let mut npm_config = ConfigUpdater::maybe_new(npm_config).await?;
let mut deno_config = ConfigUpdater::maybe_new(deno_config).await?;
@ -764,7 +769,7 @@ pub async fn remove(
flags: Arc<Flags>,
remove_flags: RemoveFlags,
) -> Result<(), AnyError> {
let (_, npm_config, deno_config) = load_configs(&flags)?;
let (_, npm_config, deno_config) = load_configs(&flags, || false)?;
let mut configs = [
ConfigUpdater::maybe_new(npm_config).await?,

View File

@ -0,0 +1,16 @@
{
"tempDir": true,
"steps": [
{
"args": "add jsr:@denotest/add",
"output": "add.out"
},
{
"args": [
"eval",
"console.log(Deno.readTextFileSync('./deno.json').trim())"
],
"output": "deno.json.out"
}
]
}

View File

@ -0,0 +1,3 @@
Created deno.json configuration file.
Add jsr:@denotest/add@1.0.0
Download [WILDCARD]

View File

@ -0,0 +1,5 @@
{
"imports": {
"@denotest/add": "jsr:@denotest/add@^1.0.0"
}
}

View File

@ -0,0 +1 @@
{}