From 5e4c0ebdf90631240110283aacf2c348eefe3146 Mon Sep 17 00:00:00 2001 From: Aryan Rajoria Date: Fri, 6 Sep 2024 16:56:43 +0530 Subject: [PATCH] debug add nvm and rearrange repotests Signed-off-by: Aryan Rajoria --- .github/workflows/repotests.yml | 40 ++++++++++----------- pregen.js | 63 ++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 32 deletions(-) diff --git a/.github/workflows/repotests.yml b/.github/workflows/repotests.yml index c440f8da2..415bcf083 100644 --- a/.github/workflows/repotests.yml +++ b/.github/workflows/repotests.yml @@ -248,6 +248,26 @@ jobs: run: | curl -s "https://get.sdkman.io" | bash if: runner.os != 'Windows' + - name: repotests react-app + run: | + FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs8 repotests/react-app -o bomresults/react-app.json + node bin/evinse.js -i bomresults/react-app.json -o bomresults/react-app.evinse.json -l javascript --with-data-flow -p repotests/react-app + shell: bash + - name: repotests basic-ftp + run: | + FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs10 repotests/basic-ftp -o bomresults/basic-ftp.json + node bin/evinse.js -i bomresults/basic-ftp.json -o bomresults/basic-ftp.evinse.json -l javascript --with-data-flow -p repotests/basic-ftp + shell: bash + - name: repotests llama-node + run: | + FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs16 repotests/llama-node -o bomresults/llama-node.json + node bin/evinse.js -i bomresults/llama-node.json -o bomresults/llama-node.evinse.json -l javascript --with-data-flow -p repotests/llama-node + shell: bash + - name: repotests RSSHub + run: | + FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs22 repotests/RSSHub -o bomresults/RSSHub.json + node bin/evinse.js -i bomresults/RSSHub.json -o bomresults/RSSHub.evinse.json -l javascript --with-data-flow -p repotests/RSSHub + shell: bash - name: repotests java-sec-code run: | bin/cdxgen.js -p -t java repotests/java-sec-code -o bomresults/bom-java-sec-code-1.json --include-formulation --include-crypto @@ -309,26 +329,6 @@ jobs: FETCH_LICENSE=false bin/cdxgen.js -p -t js repotests/meetingsdk-vuejs-sample -o bomresults/bom-vue.json node bin/evinse.js -i bomresults/bom-vue.json -o bomresults/bom-vue.evinse.json -l javascript --with-data-flow -p repotests/meetingsdk-vuejs-sample shell: bash - - name: repotests react-app - run: | - FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs8 repotests/react-app -o bomresults/react-app.json - node bin/evinse.js -i bomresults/react-app.json -o bomresults/react-app.evinse.json -l javascript --with-data-flow -p repotests/react-app - shell: bash - - name: repotests basic-ftp - run: | - FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs10 repotests/basic-ftp -o bomresults/basic-ftp.json - node bin/evinse.js -i bomresults/basic-ftp.json -o bomresults/basic-ftp.evinse.json -l javascript --with-data-flow -p repotests/basic-ftp - shell: bash - - name: repotests llama-node - run: | - FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs16 repotests/llama-node -o bomresults/llama-node.json - node bin/evinse.js -i bomresults/llama-node.json -o bomresults/llama-node.evinse.json -l javascript --with-data-flow -p repotests/llama-node - shell: bash - - name: repotests RSSHub - run: | - FETCH_LICENSE=false bin/cdxgen.js -p -t nodejs22 repotests/RSSHub -o bomresults/RSSHub.json - node bin/evinse.js -i bomresults/RSSHub.json -o bomresults/RSSHub.evinse.json -l javascript --with-data-flow -p repotests/RSSHub - shell: bash - name: repotests sveltejs-examples run: | CDXGEN_DEBUG_MODE=debug ASTGEN_IGNORE_DIRS="" FETCH_LICENSE=false bin/cdxgen.js -p -t js repotests/sveltejs-examples -o bomresults/bom-svelte.json diff --git a/pregen.js b/pregen.js index 8d1ed0403..4bd41fd04 100644 --- a/pregen.js +++ b/pregen.js @@ -1,4 +1,4 @@ -import { spawnSync } from "node:child_process"; +import { spawn, spawnSync } from "node:child_process"; import { mkdtempSync, readdirSync } from "node:fs"; import { arch, platform, tmpdir } from "node:os"; import { delimiter, join } from "node:path"; @@ -119,10 +119,12 @@ export function prepareNodeEnv(filePath, options) { // custom logic to find nvmNodePath let nvmNodePath; const possibleNodeDir = join(process.env.NVM_DIR, "versions", "node"); - - // debug in circleci - console.log(readdirSync(process.env.NVM_DIR), { withFileTypes: true }) - console.log(createDirectoryTree(process.env.NVM_DIR, 3, 0)) + + if (!tryLoadNvmAndInstallTool(nodeVersion)) { + console.log( + `Could not install Nodejs${nodeVersion}. There is a problem with loading nvm from ${process.env.NVM_DIR}`, + ); + } const nodeVersionArray = readdirSync(possibleNodeDir, { withFileTypes: true, @@ -159,6 +161,39 @@ export function prepareNodeEnv(filePath, options) { } } +/** + * If NVM_DIR is in path, however nvm command is not loaded. + * it is possible that required nodeVersion is not installed. + * This function loads nvm and install the nodeVersion + * + * @param {String} nodeVersion required version number + * + * @returns {Boolean} true if successful, otherwise false + */ +export function tryLoadNvmAndInstallTool(nodeVersion) { + const NVM_DIR = process.env.NVM_DIR; + + const command = ` + if [ -f ${NVM_DIR}/nvm.sh ]; then + . ${NVM_DIR}/nvm.sh + nvm install ${nodeVersion} + else + echo "NVM script not found at ${NVM_DIR}/nvm.sh" + exit 1 + fi + `; + + const spawnedShell = spawnSync(process.env.SHELL || "bash", ["-c", command], { + encoding: "utf-8", + shell: process.env.SHELL || true, + }); + if (spawnedShell.status === 0) { + return true; + } + + return false; +} + /** * This method installs and create package-lock.json * @@ -206,10 +241,10 @@ export function doNpmInstall(filePath, nvmNodePath) { /** * Function to retrieve directories in nvm - * @param {*} directoryPath - * @param {*} maxDepth - * @param {*} currentDepth - * @returns + * @param {*} directoryPath + * @param {*} maxDepth + * @param {*} currentDepth + * @returns */ function createDirectoryTree(directoryPath, maxDepth = 3, currentDepth = 0) { if (currentDepth > maxDepth) { @@ -220,10 +255,14 @@ function createDirectoryTree(directoryPath, maxDepth = 3, currentDepth = 0) { const entries = readdirSync(directoryPath, { withFileTypes: true }); const tree = {}; - entries.forEach(entry => { + entries.forEach((entry) => { if (entry.isDirectory()) { const subDirPath = join(directoryPath, entry.name); - tree[entry.name] = createDirectoryTree(subDirPath, maxDepth, currentDepth + 1); + tree[entry.name] = createDirectoryTree( + subDirPath, + maxDepth, + currentDepth + 1, + ); } }); @@ -232,4 +271,4 @@ function createDirectoryTree(directoryPath, maxDepth = 3, currentDepth = 0) { console.error(`Error reading directory ${directoryPath}:`, err); return null; } -} \ No newline at end of file +}