1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

fix(npmrc): discovery in cwd when there is no package.json or deno.json

This commit is contained in:
Marvin Hagemeister 2024-10-25 15:20:18 +02:00
parent 0060e74779
commit 6a93485ae2
9 changed files with 66 additions and 5 deletions

View file

@ -518,6 +518,7 @@ fn resolve_lint_rules_options(
pub fn discover_npmrc_from_workspace(
workspace: &Workspace,
cwd: Option<&PathBuf>,
) -> Result<(Arc<ResolvedNpmRc>, Option<PathBuf>), AnyError> {
let root_folder = workspace.root_folder_configs();
discover_npmrc(
@ -529,6 +530,7 @@ pub fn discover_npmrc_from_workspace(
None
}
}),
cwd,
)
}
@ -540,6 +542,7 @@ pub fn discover_npmrc_from_workspace(
fn discover_npmrc(
maybe_package_json_path: Option<PathBuf>,
maybe_deno_json_path: Option<PathBuf>,
cwd: Option<&PathBuf>,
) -> Result<(Arc<ResolvedNpmRc>, Option<PathBuf>), AnyError> {
const NPMRC_NAME: &str = ".npmrc";
@ -583,7 +586,7 @@ fn discover_npmrc(
}
// 1. Try `.npmrc` next to `package.json`
if let Some(package_json_path) = maybe_package_json_path {
if let Some(package_json_path) = &maybe_package_json_path {
if let Some(package_json_dir) = package_json_path.parent() {
if let Some((source, path)) = try_to_read_npmrc(package_json_dir)? {
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
@ -592,7 +595,7 @@ fn discover_npmrc(
}
// 2. Try `.npmrc` next to `deno.json(c)`
if let Some(deno_json_path) = maybe_deno_json_path {
if let Some(deno_json_path) = &maybe_deno_json_path {
if let Some(deno_json_dir) = deno_json_path.parent() {
if let Some((source, path)) = try_to_read_npmrc(deno_json_dir)? {
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
@ -600,9 +603,18 @@ fn discover_npmrc(
}
}
// 3. Try `.npmrc` in cwd if no package.json or deno.json(c) was found
if let Some(cwd) = cwd {
if maybe_package_json_path.is_none() && maybe_deno_json_path.is_none() {
if let Some((source, path)) = try_to_read_npmrc(&cwd)? {
return try_to_parse_npmrc(source, &path).map(|r| (r, Some(path)));
}
}
}
// TODO(bartlomieju): update to read both files - one in the project root and one and
// home dir and then merge them.
// 3. Try `.npmrc` in the user's home directory
// 4. Try `.npmrc` in the user's home directory
if let Some(home_dir) = cache::home_dir() {
match try_to_read_npmrc(&home_dir) {
Ok(Some((source, path))) => {
@ -924,7 +936,8 @@ impl CliOptions {
log::warn!("{} {}", colors::yellow("Warning"), diagnostic);
}
let (npmrc, _) = discover_npmrc_from_workspace(&start_dir.workspace)?;
let (npmrc, _) =
discover_npmrc_from_workspace(&start_dir.workspace, Some(&initial_cwd))?;
let maybe_lock_file = CliLockfile::discover(&flags, &start_dir.workspace)?;

View file

@ -1342,7 +1342,7 @@ impl ConfigData {
}
// todo(dsherret): cache this so we don't load this so many times
let npmrc = discover_npmrc_from_workspace(&member_dir.workspace)
let npmrc = discover_npmrc_from_workspace(&member_dir.workspace, None)
.inspect(|(_, path)| {
if let Some(path) = path {
lsp_log!(" Resolved .npmrc: \"{}\"", path.display());

View file

@ -0,0 +1,7 @@
#!/usr/bin/env -S node
import process from "node:process";
for (const arg of process.argv.slice(2)) {
console.log(arg);
}

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/bin",
"version": "1.0.0",
"bin": "./cli.mjs"
}

View file

@ -0,0 +1,4 @@
@denotest:registry=http://localhost:4261/
//localhost:4261/:_authToken=private-reg-token
@denotest2:registry=http://localhost:4262/
//localhost:4262/:_authToken=private-reg-token2

View file

@ -0,0 +1,14 @@
{
"tempDir": true,
"tests": {
"run_script_with_npm_with_no_project": {
"args": "run -A main.js",
"output": "main.out"
},
"run_npm_with_no_project": {
"args": "run -A npm:@denotest/bin deno",
"output": "direct.out"
}
}
}

View file

@ -0,0 +1,3 @@
Download http://localhost:4261/@denotest%2fbin
Download http://localhost:4261/@denotest/bin/1.0.0.tgz
deno

View file

@ -0,0 +1,8 @@
import { getValue, setValue } from "npm:@denotest/basic";
import * as test from "npm:@denotest2/basic";
console.log(getValue());
setValue(42);
console.log(getValue());
console.log(test.getValue());

View file

@ -0,0 +1,7 @@
Download http://localhost:4261/@denotest%2fbasic
Download http://localhost:4262/@denotest2%2fbasic
Download http://localhost:4261/@denotest/basic/1.0.0.tgz
Download http://localhost:4262/@denotest2/basic/1.0.0.tgz
0
42
0