Skip to content

Commit d880192

Browse files
authored
JS-implementation of getting runtime + version (CycloneDX#2081)
* Full js implementation for getting runtime + version Signed-off-by: Roland Asmann <[email protected]> * Linting Signed-off-by: Roland Asmann <[email protected]> * Extracted getting of runtime-information into utils, using it in envcontext as well Signed-off-by: Roland Asmann <[email protected]> --------- Signed-off-by: Roland Asmann <[email protected]>
1 parent 7f5d803 commit d880192

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

bin/cdxgen.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ import {
2929
commandsExecuted,
3030
DEBUG_MODE,
3131
dirNameStr,
32+
getRuntimeInformation,
3233
getTmpDir,
3334
isMac,
3435
isSecureMode,
3536
isWin,
3637
remoteHostsAccessed,
3738
safeExistsSync,
38-
safeSpawnSync,
3939
} from "../lib/helpers/utils.js";
4040
import { validateBom } from "../lib/helpers/validator.js";
4141
import { postProcess } from "../lib/stages/postgen/postgen.js";
@@ -405,21 +405,9 @@ function version() {
405405
);
406406
const packageJson = JSON.parse(packageJsonAsString);
407407

408-
var version = `\x1b[1mCycloneDX Generator ${packageJson.version}\x1b[0m`;
408+
const runtimeInfo = getRuntimeInformation();
409409

410-
if (process.execPath.endsWith("/node")) {
411-
const result = safeSpawnSync(process.execPath, ["-v"], {
412-
encoding: "utf-8",
413-
shell: isWin,
414-
});
415-
const nodeVersion =
416-
result.status !== 0 || result.error
417-
? "(security doesn't allow retrieval')"
418-
: result.stdout.trim();
419-
version = `${version}\nNode: ${process.execPath}, version: ${nodeVersion}`;
420-
}
421-
422-
return version;
410+
return `\x1b[1mCycloneDX Generator ${packageJson.version}\x1b[0m\nRuntime: ${runtimeInfo.runtime}, Version: ${runtimeInfo.version}`;
423411
}
424412

425413
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 nodeInfo;
239236
}
240237

241238
/**

lib/helpers/utils.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13675,11 +13675,7 @@ export async function getPipFrozenTree(
1367513675

1367613676
// FIX: Create a set of explicit dependencies from requirements.txt to identify root packages.
1367713677
const explicitDeps = new Set();
13678-
if (
13679-
reqOrSetupFile &&
13680-
reqOrSetupFile.endsWith(".txt") &&
13681-
safeExistsSync(reqOrSetupFile)
13682-
) {
13678+
if (reqOrSetupFile?.endsWith(".txt") && safeExistsSync(reqOrSetupFile)) {
1368313679
const reqData = readFileSync(reqOrSetupFile, { encoding: "utf-8" });
1368413680
// We only need the package names, so we pass `false` to avoid fetching full metadata.
1368513681
const tempPkgList = await parseReqFile(reqData, false);
@@ -15899,3 +15895,31 @@ function collectAllLdConfs(basePath, ldConf, allLdConfDirs, libPaths) {
1589915895
}
1590015896
}
1590115897
}
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)