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

feat(npm): support packages with multiple command names (#15565)

This commit is contained in:
David Sherret 2022-08-23 22:01:21 -04:00 committed by GitHub
parent 4ef08a58df
commit 452df99222
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
84 changed files with 130 additions and 64240 deletions

View file

@ -35,6 +35,7 @@
"cli/tests/testdata/import_assertions/json_with_shebang.json",
"cli/tests/testdata/inline_js_source_map*",
"cli/tests/testdata/malformed_config/*",
"cli/tests/testdata/npm/registry/",
"cli/tests/testdata/test/markdown_windows.md",
"cli/tsc/*typescript.js",
"gh-pages",

View file

@ -106,6 +106,27 @@ pub async fn initialize_runtime(
Ok(())
}
pub async fn initialize_binary_command(
js_runtime: &mut JsRuntime,
binary_name: &str,
) -> Result<(), AnyError> {
// overwrite what's done in deno_std in order to set the binary arg name
let source_code = &format!(
r#"(async function initializeBinaryCommand(binaryName) {{
const process = Deno[Deno.internal].node.globalThis.process;
Object.defineProperty(process.argv, "0", {{
get: () => binaryName,
}});
}})('{}');"#,
binary_name,
);
let value =
js_runtime.execute_script(&located_script_name!(), source_code)?;
js_runtime.resolve_value(value).await?;
Ok(())
}
/// This function is an implementation of `defaultResolve` in
/// `lib/internal/modules/esm/resolve.js` from Node.
pub fn node_resolve(

View file

@ -184,6 +184,27 @@ fn deno_run_cjs_module() {
assert!(deno_dir.path().join("test_dir").exists());
}
itest!(deno_run_cowsay {
args: "run --unstable -A --quiet npm:cowsay@1.5.0 Hello",
output: "npm/deno_run_cowsay.out",
envs: env_vars_no_sync_download(),
http_server: true,
});
itest!(deno_run_cowsay_explicit {
args: "run --unstable -A --quiet npm:cowsay@1.5.0/cowsay Hello",
output: "npm/deno_run_cowsay.out",
envs: env_vars_no_sync_download(),
http_server: true,
});
itest!(deno_run_cowthink {
args: "run --unstable -A --quiet npm:cowsay@1.5.0/cowthink Hello",
output: "npm/deno_run_cowthink.out",
envs: env_vars_no_sync_download(),
http_server: true,
});
itest!(deno_run_non_existent {
args: "run --unstable npm:mkdirp@0.5.125",
output: "npm/deno_run_non_existent.out",

View file

@ -0,0 +1,8 @@
_______
< Hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

View file

@ -0,0 +1,8 @@
_______
( Hello )
-------
o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View file

@ -21,6 +21,7 @@ use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
use deno_runtime::BootstrapOptions;
use crate::args::DenoSubcommand;
use crate::checksum;
use crate::compat;
use crate::errors;
@ -103,7 +104,7 @@ impl CliMainWorker {
)?;
}
} else if self.is_main_cjs {
node::initialize_runtime(&mut self.worker.js_runtime).await?;
self.initialize_main_module_for_node().await?;
node::load_cjs_module_from_ext_node(
&mut self.worker.js_runtime,
&self.main_module.to_file_path().unwrap().to_string_lossy(),
@ -422,11 +423,31 @@ impl CliMainWorker {
id: ModuleId,
) -> Result<(), AnyError> {
if self.ps.npm_resolver.has_packages() {
node::initialize_runtime(&mut self.worker.js_runtime).await?;
self.initialize_main_module_for_node().await?;
}
self.worker.evaluate_module(id).await
}
async fn initialize_main_module_for_node(&mut self) -> Result<(), AnyError> {
node::initialize_runtime(&mut self.worker.js_runtime).await?;
if let DenoSubcommand::Run(flags) = self.ps.options.sub_command() {
if let Ok(pkg_ref) = NpmPackageReference::from_str(&flags.script) {
// if the user ran a binary command, we'll need to set process.argv[0]
// to be the name of the binary command instead of deno
let binary_name = pkg_ref
.sub_path
.as_deref()
.unwrap_or(pkg_ref.req.name.as_str());
node::initialize_binary_command(
&mut self.worker.js_runtime,
binary_name,
)
.await?;
}
}
Ok(())
}
async fn maybe_setup_coverage_collector(
&mut self,
) -> Result<Option<CoverageCollector>, AnyError> {