From 3f3b246ffe9494eaa73eb1e91f72fd1729c392eb Mon Sep 17 00:00:00 2001 From: killa Date: Sat, 21 Sep 2024 22:52:27 +0800 Subject: [PATCH] chore: use XTRANSIT_NODE_EXE to get exe (#55) * chore: use XTRANSIT_NODE_EXE to get exe --- commands/check_process_status.js | 2 +- commands/xprofctl.js | 2 +- common/exe.js | 3 +++ lib/agent.js | 3 +++ lib/handler.js | 8 +------- test/config.test.js | 5 ++++- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/commands/check_process_status.js b/commands/check_process_status.js index b7dc3cf..a25f0a2 100644 --- a/commands/check_process_status.js +++ b/commands/check_process_status.js @@ -43,7 +43,7 @@ async function checkInstalled(nodeExe, cwd) { } async function checkStatus() { - const nodeExe = process.env.XTRANSIT_NODE_EXE || await getNodeExe(pid); + const nodeExe = await getNodeExe(pid); status.nodeVersion = await execute(`${nodeExe} -v`); status.alinodeVersion = await execute(`${nodeExe} -V`); diff --git a/commands/xprofctl.js b/commands/xprofctl.js index f605d83..9b6919c 100644 --- a/commands/xprofctl.js +++ b/commands/xprofctl.js @@ -54,7 +54,7 @@ async function takeAction() { // hanle coredump if (command === 'generate_coredump') { data.type = 'core'; - const nodepath = process.env.XTRANSIT_NODE_EXE || await realpath(await getNodeExe(process.pid, false)); + const nodepath = await realpath(await getNodeExe(process.pid, false)); data.executable_path = nodepath; data.node_version = process.versions.node; data.alinode_version = process.versions.alinode; diff --git a/common/exe.js b/common/exe.js index 5f843f4..6b4a416 100644 --- a/common/exe.js +++ b/common/exe.js @@ -11,6 +11,9 @@ const utils = require('../common/utils'); const platform = os.platform(); module.exports = async (pid, stringify = true) => { + if (process.env.XTRANSIT_NODE_EXE) { + return process.env.XTRANSIT_NODE_EXE; + } let nodeExe = 'node'; /* istanbul ignore next */ diff --git a/lib/agent.js b/lib/agent.js index f802f7e..bced2c3 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -47,6 +47,9 @@ class XtransitAgent extends EventEmitter { this.filterProcessEnvName = config.filterProcessEnvName; this.customXprofilerLogs = config.customXprofilerLogs; this.nodeExe = config.nodeExe; + if (this.nodeExe) { + process.env.XTRANSIT_NODE_EXE = this.nodeExe; + } // global var this.conn = null; diff --git a/lib/handler.js b/lib/handler.js index 5860ef3..20cfeeb 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -97,13 +97,7 @@ module.exports = async function(message, options) { }; // exec command - let nodeExe; - if (this.config && this.config.nodeExe) { - nodeExe = this.config.nodeExe; - execOptions.env.XTRANSIT_NODE_EXE = this.config.nodeExe; - } else { - nodeExe = await getNodeExe(process.pid, false); - } + const nodeExe = await getNodeExe(process.pid, false); this.logger.debug(`[execute command] ${nodeExe} ${args.join(' ')}`); const aExecFile = options && options.execFile || execFile; const { stdout, stderr: error } = await aExecFile(nodeExe, args, execOptions); diff --git a/test/config.test.js b/test/config.test.js index 6919f7f..da49389 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -20,7 +20,10 @@ function getError(config) { } describe('get config', function() { - afterEach(mm.restore); + afterEach(() => { + mm.restore(); + delete process.env.XTRANSIT_NODE_EXE; + }); it('should throw error', function() { expect(getError({})).to.be('config.server must be passed in!');