Skip to content

Commit 78e6dd1

Browse files
committed
Extracted getting of runtime-information into utils, using it in envcontext as well
Signed-off-by: Roland Asmann <[email protected]>
1 parent 2862cfd commit 78e6dd1

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

bin/cdxgen.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
commandsExecuted,
3030
DEBUG_MODE,
3131
dirNameStr,
32+
getRuntimeInformation,
3233
getTmpDir,
3334
isMac,
3435
isSecureMode,
@@ -404,27 +405,9 @@ function version() {
404405
);
405406
const packageJson = JSON.parse(packageJsonAsString);
406407

407-
let runtime;
408-
let runtimeVersion;
408+
const runtimeInfo = getRuntimeInformation();
409409

410-
if (typeof globalThis.Deno !== "undefined" && globalThis.Deno.version?.deno) {
411-
runtime = "Deno";
412-
runtimeVersion = globalThis.Deno.version.deno;
413-
} else if (typeof globalThis.Bun !== "undefined" && globalThis.Bun.version) {
414-
runtime = "Bun";
415-
runtimeVersion = globalThis.Bun.version;
416-
} else if (
417-
typeof globalThis.process !== "undefined" &&
418-
globalThis.process.versions?.node
419-
) {
420-
runtime = "Node.js";
421-
runtimeVersion = globalThis.process.versions.node;
422-
} else {
423-
runtime = "Unknown";
424-
runtimeVersion = "N/A";
425-
}
426-
427-
return `\x1b[1mCycloneDX Generator ${packageJson.version}\x1b[0m\nRuntime: ${runtime}, Version: ${runtimeVersion}`;
410+
return `\x1b[1mCycloneDX Generator ${packageJson.version}\x1b[0m\nRuntime: ${runtimeInfo.runtime}, Version: ${runtimeInfo.version}`;
428411
}
429412

430413
if (process.env.GLOBAL_AGENT_HTTP_PROXY || process.env.HTTP_PROXY) {

lib/helpers/envcontext.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {
1313
GO_CMD,
1414
getJavaCommand,
1515
getPythonCommand,
16+
getRuntimeInformation,
1617
getTmpDir,
1718
isMac,
1819
isWin,
1920
MAX_BUFFER,
20-
NODE_CMD,
2121
NPM_CMD,
2222
RUBY_CMD,
2323
RUSTC_CMD,
@@ -222,20 +222,17 @@ export function collectPythonInfo(dir) {
222222
* @returns Object containing node details
223223
*/
224224
export function collectNodeInfo(dir) {
225-
const versionDesc = getCommandOutput(NODE_CMD, dir, ["--version"]);
226-
let moduleDesc = getCommandOutput(NPM_CMD, dir, ["--version"]);
225+
const runtimeInfo = getRuntimeInformation();
226+
const nodeInfo = {
227+
type: "platform",
228+
name: runtimeInfo.runtime,
229+
version: runtimeInfo.version,
230+
};
231+
const moduleDesc = getCommandOutput(NPM_CMD, dir, ["--version"]);
227232
if (moduleDesc) {
228-
moduleDesc = `npm: ${moduleDesc}`;
233+
nodeInfo.description = `npm: ${moduleDesc}`;
229234
}
230-
if (versionDesc) {
231-
return {
232-
type: "platform",
233-
name: "node",
234-
version: versionDesc.trim(),
235-
description: moduleDesc,
236-
};
237-
}
238-
return undefined;
235+
return runtimeInfo;
239236
}
240237

241238
/**

lib/helpers/utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15895,3 +15895,31 @@ function collectAllLdConfs(basePath, ldConf, allLdConfDirs, libPaths) {
1589515895
}
1589615896
}
1589715897
}
15898+
15899+
/**
15900+
* Get information about the runtime.
15901+
*
15902+
* @returns {Object} Object containing the name and version of the runtime
15903+
*/
15904+
export function getRuntimeInformation() {
15905+
const runtimeInfo = {};
15906+
15907+
if (typeof globalThis.Deno !== "undefined" && globalThis.Deno.version?.deno) {
15908+
runtimeInfo.runtime = "Deno";
15909+
runtimeInfo.version = globalThis.Deno.version.deno;
15910+
} else if (typeof globalThis.Bun !== "undefined" && globalThis.Bun.version) {
15911+
runtimeInfo.runtime = "Bun";
15912+
runtimeInfo.version = globalThis.Bun.version;
15913+
} else if (
15914+
typeof globalThis.process !== "undefined" &&
15915+
globalThis.process.versions?.node
15916+
) {
15917+
runtimeInfo.runtime = "Node.js";
15918+
runtimeInfo.version = globalThis.process.versions.node;
15919+
} else {
15920+
runtimeInfo.runtime = "Unknown";
15921+
runtimeInfo.version = "N/A";
15922+
}
15923+
15924+
return runtimeInfo;
15925+
}

0 commit comments

Comments
 (0)