From 2d1a0eff3274f97b785757ee4bc2e1bb25ef8788 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 9 Dec 2022 13:01:45 +0100 Subject: [PATCH] feat(rome_cli): Add JS runtime and package manager information to `rage` (#4021) --- crates/rome_cli/src/commands/rage.rs | 3 ++ .../snapshots/main_commands_rage/rage_ok.snap | 3 ++ .../with_configuration.snap | 3 ++ .../with_malformed_configuration.snap | 3 ++ .../main_commands_rage/with_server_logs.snap | 3 ++ npm/rome/bin/rome | 32 +++++++++++++++++-- 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/rome_cli/src/commands/rage.rs b/crates/rome_cli/src/commands/rage.rs index a88aeb454b9..dea2ef57cb9 100644 --- a/crates/rome_cli/src/commands/rage.rs +++ b/crates/rome_cli/src/commands/rage.rs @@ -30,6 +30,9 @@ pub(crate) fn rage(mut session: CliSession) -> Result<(), Termination> { {EnvVarOs("ROME_LOG_DIR")} {EnvVarOs("NO_COLOR")} {EnvVarOs("TERM")} + {EnvVarOs("JS_RUNTIME_VERSION")} + {EnvVarOs("JS_RUNTIME_NAME")} + {EnvVarOs("NODE_PACKAGE_MANAGER")} {RageConfiguration(&session.app.fs)} {WorkspaceRage(session.app.workspace.deref())} diff --git a/crates/rome_cli/tests/snapshots/main_commands_rage/rage_ok.snap b/crates/rome_cli/tests/snapshots/main_commands_rage/rage_ok.snap index bd95848cc66..7cf2cf4f5b5 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_rage/rage_ok.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_rage/rage_ok.snap @@ -17,6 +17,9 @@ Environment: ROME_LOG_DIR: **PLACEHOLDER** NO_COLOR: **PLACEHOLDER** TERM: **PLACEHOLDER** + JS_RUNTIME_VERSION: unset + JS_RUNTIME_NAME: unset + NODE_PACKAGE_MANAGER: unset Rome Configuration: Status: unset diff --git a/crates/rome_cli/tests/snapshots/main_commands_rage/with_configuration.snap b/crates/rome_cli/tests/snapshots/main_commands_rage/with_configuration.snap index 120c8cab8f0..efc3f7e1c6a 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_rage/with_configuration.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_rage/with_configuration.snap @@ -27,6 +27,9 @@ Environment: ROME_LOG_DIR: **PLACEHOLDER** NO_COLOR: **PLACEHOLDER** TERM: **PLACEHOLDER** + JS_RUNTIME_VERSION: unset + JS_RUNTIME_NAME: unset + NODE_PACKAGE_MANAGER: unset Rome Configuration: Status: loaded diff --git a/crates/rome_cli/tests/snapshots/main_commands_rage/with_malformed_configuration.snap b/crates/rome_cli/tests/snapshots/main_commands_rage/with_malformed_configuration.snap index 714734cde99..05c3d0d5a78 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_rage/with_malformed_configuration.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_rage/with_malformed_configuration.snap @@ -27,6 +27,9 @@ Environment: ROME_LOG_DIR: **PLACEHOLDER** NO_COLOR: **PLACEHOLDER** TERM: **PLACEHOLDER** + JS_RUNTIME_VERSION: unset + JS_RUNTIME_NAME: unset + NODE_PACKAGE_MANAGER: unset Rome Configuration: Status: Failed to load diff --git a/crates/rome_cli/tests/snapshots/main_commands_rage/with_server_logs.snap b/crates/rome_cli/tests/snapshots/main_commands_rage/with_server_logs.snap index 317d8a1610e..a72ef6e453f 100644 --- a/crates/rome_cli/tests/snapshots/main_commands_rage/with_server_logs.snap +++ b/crates/rome_cli/tests/snapshots/main_commands_rage/with_server_logs.snap @@ -17,6 +17,9 @@ Environment: ROME_LOG_DIR: **PLACEHOLDER** NO_COLOR: **PLACEHOLDER** TERM: **PLACEHOLDER** + JS_RUNTIME_VERSION: unset + JS_RUNTIME_NAME: unset + NODE_PACKAGE_MANAGER: unset Rome Configuration: Status: unset diff --git a/npm/rome/bin/rome b/npm/rome/bin/rome index bc9d65f5b83..af50af92430 100644 --- a/npm/rome/bin/rome +++ b/npm/rome/bin/rome @@ -1,5 +1,5 @@ #!/usr/bin/env node -const { platform, arch } = process; +const { platform, arch, env, version, release } = process; const PLATFORMS = { win32: { @@ -21,7 +21,16 @@ if (binPath) { const result = require("child_process").spawnSync( require.resolve(binPath), process.argv.slice(2), - { shell: false, stdio: "inherit" }, + { + shell: false, + stdio: "inherit", + env: { + ...env, + JS_RUNTIME_VERSION: version, + JS_RUNTIME_NAME: release.name, + NODE_PACKAGE_MANAGER: detectPackageManager(), + }, + }, ); if (result.error) { @@ -37,3 +46,22 @@ if (binPath) { ); process.exitCode = 1; } + +/** + * NPM, Yarn, and other package manager set the `npm_config_user_agent`. It has the following format: + * + * ``` + * "npm/8.3.0 node/v16.13.2 win32 x64 workspaces/false + * ``` + * + * @returns The package manager string (`npm/8.3.0`) or null if the user agent string isn't set. + */ +function detectPackageManager() { + const userAgent = env.npm_config_user_agent; + + if (userAgent == null) { + return null; + } + + return userAgent.split(" ")[0]; +}