Skip to content

Commit

Permalink
refactor: project structure PR (#1382)
Browse files Browse the repository at this point in the history
* refactoring

Signed-off-by: Aryan Rajoria <[email protected]>

* lint

Signed-off-by: Aryan Rajoria <[email protected]>

* fix for tests - utils.test.js display.test.js postegen.test.js

Signed-off-by: Aryan Rajoria <[email protected]>

* fix exports

Signed-off-by: Aryan Rajoria <[email protected]>

* fix files

Signed-off-by: Aryan Rajoria <[email protected]>

* add imports to deno and jsr

Signed-off-by: Aryan Rajoria <[email protected]>

* fix exports

Signed-off-by: Aryan Rajoria <[email protected]>

* add requested change

Signed-off-by: Aryan Rajoria <[email protected]>

* change from folder names to **

Signed-off-by: Aryan Rajoria <[email protected]>

* lint

Signed-off-by: Aryan Rajoria <[email protected]>

* add files to exclude in jsr and deno

Signed-off-by: Aryan Rajoria <[email protected]>

* fix * to .*

Signed-off-by: Aryan Rajoria <[email protected]>

---------

Signed-off-by: Aryan Rajoria <[email protected]>
  • Loading branch information
aryan-rajoria authored Sep 19, 2024
1 parent 1e9123f commit ddaa6be
Show file tree
Hide file tree
Showing 31 changed files with 82 additions and 51 deletions.
20 changes: 10 additions & 10 deletions bin/cdxgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { findUpSync } from "find-up";
import globalAgent from "global-agent";
import { load as _load } from "js-yaml";
import jws from "jws";
import { createBom, submitBom } from "../lib/cli/index.js";
import {
printCallStack,
printDependencyTree,
Expand All @@ -20,12 +21,11 @@ import {
printSponsorBanner,
printSummary,
printTable,
} from "../display.js";
import { createBom, submitBom } from "../index.js";
import { postProcess } from "../postgen.js";
import { prepareEnv } from "../pregen.js";
import { ATOM_DB } from "../utils.js";
import { validateBom } from "../validator.js";
} from "../lib/helpers/display.js";
import { ATOM_DB, dirNameStr } from "../lib/helpers/utils.js";
import { validateBom } from "../lib/helpers/validator.js";
import { postProcess } from "../lib/stages/postgen/postgen.js";
import { prepareEnv } from "../lib/stages/pregen/pregen.js";

// Support for config files
const configPath = findUpSync([
Expand All @@ -51,7 +51,7 @@ let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
const dirName = import.meta ? dirname(fileURLToPath(url)) : __dirname;
const dirName = dirNameStr;

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
Expand Down Expand Up @@ -493,7 +493,7 @@ const checkPermissions = (filePath) => {
printSponsorBanner(options);
// Start SBOM server
if (options.server) {
const serverModule = await import("../server.js");
const serverModule = await import("../lib/server/server.js");
return serverModule.start(options);
}
// Check if cdxgen has the required permissions
Expand Down Expand Up @@ -666,7 +666,7 @@ const checkPermissions = (filePath) => {
if (!options.evinseOutput) {
options.evinseOutput = options.output;
}
const evinserModule = await import("../evinser.js");
const evinserModule = await import("../lib/evinser/evinser.js");
options.projectType = options.projectType || ["java"];
const evinseOptions = {
_: args._,
Expand Down Expand Up @@ -719,7 +719,7 @@ const checkPermissions = (filePath) => {
}
// Protobuf serialization
if (options.exportProto) {
const protobomModule = await import("../protobom.js");
const protobomModule = await import("../lib/helpers/protobom.js");
protobomModule.writeBinary(bomNSData.bomJson, options.protoBinFile);
}
if (options.print && bomNSData.bomJson && bomNSData.bomJson.components) {
Expand Down
12 changes: 8 additions & 4 deletions bin/evinse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import { load as _load } from "js-yaml";
// Evinse (Evinse Verification Is Nearly SBOM Evidence)
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import {
analyzeProject,
createEvinseFile,
prepareDB,
} from "../lib/evinser/evinser.js";
import {
printCallStack,
printOccurrences,
printReachables,
printServices,
} from "../display.js";
import { analyzeProject, createEvinseFile, prepareDB } from "../evinser.js";
import { ATOM_DB } from "../utils.js";
import { validateBom } from "../validator.js";
} from "../lib/helpers/display.js";
import { ATOM_DB } from "../lib/helpers/utils.js";
import { validateBom } from "../lib/helpers/validator.js";

// Support for config files
const configPath = findUpSync([
Expand Down
6 changes: 3 additions & 3 deletions bin/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import process from "node:process";
import repl from "node:repl";
import jsonata from "jsonata";

import { createBom } from "../lib/cli/index.js";
import {
printCallStack,
printDependencyTree,
Expand All @@ -17,9 +18,8 @@ import {
printSummary,
printTable,
printVulnerabilities,
} from "../display.js";
import { createBom } from "../index.js";
import { validateBom } from "../validator.js";
} from "../lib/helpers/display.js";
import { validateBom } from "../lib/helpers/validator.js";

const options = {
useColors: true,
Expand Down
3 changes: 2 additions & 1 deletion bin/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { URL, fileURLToPath } from "node:url";
import jws from "jws";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { dirNameStr } from "../lib/helpers/utils.js";

let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
const dirName = import.meta ? dirname(fileURLToPath(url)) : __dirname;
const dirName = dirNameStr;

const args = yargs(hideBin(process.argv))
.option("input", {
Expand Down
16 changes: 12 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"name": "@cyclonedx/cdxgen",
"version": "10.10.0",
"exports": "./index.js",
"exports": "./lib/cli/index.js",
"compilerOptions": {
"allowJs": true,
"lib": ["deno.window"],
"exclude": ["*.test.js", "./types/**/*"],
"strict": true
},
"include": ["*.js", "data/", "bin/"],
"exclude": ["test/", "docs/", "contrib/", "ci/", "tools_config/"],
"include": ["*.js", "data/", "bin/", "lib/**"],
"exclude": [
"test/",
"docs/",
"contrib/",
"ci/",
"tools_config/",
".github/",
".*"
],
"lint": {
"include": ["bin/", "*.js"],
"include": ["bin/", "*.js", "lib/**"],
"exclude": ["test/", "data/", "contrib/"],
"rules": {
"tags": ["recommended", "no-self-compare"],
Expand Down
14 changes: 11 additions & 3 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@cyclonedx/cdxgen",
"version": "10.10.0",
"exports": "./index.js",
"include": ["*.js", "bin/**", "data/**", "types/**"],
"exclude": ["test/", "docs/", "contrib/", "ci/", "tools_config/"]
"exports": "./lib/cli/index.js",
"include": ["*.js", "lib/**", "bin/**", "data/**", "types/**"],
"exclude": [
"test/",
"docs/",
"contrib/",
"ci/",
"tools_config/",
".github/",
".*"
]
}
15 changes: 8 additions & 7 deletions index.js → lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
getOriginUrl,
gitTreeHashes,
listFiles,
} from "./envcontext.js";
} from "../helpers/envcontext.js";
import {
CARGO_CMD,
CLJ_CMD,
Expand All @@ -52,6 +52,7 @@ import {
convertJarNSToPackages,
convertOSQueryResults,
determineSbtVersion,
dirNameStr,
encodeForPurl,
executeGradleProperties,
executeParallelGradleProperties,
Expand Down Expand Up @@ -143,34 +144,34 @@ import {
readZipEntry,
recomputeScope,
splitOutputByGradleProjects,
} from "./utils.js";
} from "../helpers/utils.js";
let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
const dirName = import.meta ? dirname(fileURLToPath(url)) : __dirname;
const dirName = dirNameStr;

const selfPJson = JSON.parse(
readFileSync(join(dirName, "package.json"), "utf-8"),
);
const _version = selfPJson.version;
import { gte, lte } from "semver";
import { findJSImportsExports } from "./analyzer.js";
import { findJSImportsExports } from "../helpers/analyzer.js";
import { collectOSCryptoLibs } from "../helpers/cbomutils.js";
import {
executeOsQuery,
getBinaryBom,
getCargoAuditableInfo,
getDotnetSlices,
getOSPackages,
} from "./binary.js";
import { collectOSCryptoLibs } from "./cbomutils.js";
} from "../managers/binary.js";
import {
addSkippedSrcFiles,
exportArchive,
exportImage,
getPkgPathList,
parseImageName,
} from "./docker.js";
} from "../managers/docker.js";

const isWin = _platform() === "win32";

Expand Down
6 changes: 3 additions & 3 deletions evinser.js → lib/evinser/evinser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import path from "node:path";
import process from "node:process";
import { PackageURL } from "packageurl-js";
import { Op } from "sequelize";
import { findCryptoAlgos } from "./cbomutils.js";
import * as db from "./db.js";
import { findCryptoAlgos } from "../helpers/cbomutils.js";
import * as db from "../helpers/db.js";
import {
DEBUG_MODE,
collectGradleDependencies,
Expand All @@ -15,7 +15,7 @@ import {
getGradleCommand,
getMavenCommand,
getTimestamp,
} from "./utils.js";
} from "../helpers/utils.js";
const DB_NAME = "evinser.db";
const typePurlsCache = {};

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cbomutils.js → lib/helpers/cbomutils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { executeOsQuery } from "./binary.js";
import { executeOsQuery } from "../managers/binary.js";
import { convertOSQueryResults, dirNameStr } from "./utils.js";
const cbomosDbQueries = JSON.parse(
readFileSync(join(dirNameStr, "data", "cbomosdb-queries.json"), "utf-8"),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions utils.js → lib/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ import {
import toml from "toml";
import { IriValidationStrategy, validateIri } from "validate-iri";
import { xml2js } from "xml-js";
import { getTreeWithPlugin } from "./piptree.js";
import { getTreeWithPlugin } from "../managers/piptree.js";

let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
export const dirNameStr = import.meta ? dirname(fileURLToPath(url)) : __dirname;
// TODO: verify if this is a good method (Prabhu)
// this is due to dirNameStr being "cdxgen/lib/helpers" which causes errors
export const dirNameStr = import.meta
? dirname(dirname(dirname(fileURLToPath(url))))
: __dirname;
export const isWin = platform() === "win32";
export const isMac = platform() === "darwin";
export let ATOM_DB = join(homedir(), ".local", "share", ".atomdb");
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions validator.js → lib/helpers/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { dirname, join } from "node:path";
import Ajv from "ajv";
import addFormats from "ajv-formats";
import { PackageURL } from "packageurl-js";
import { DEBUG_MODE, isPartialTree } from "./utils.js";
import { DEBUG_MODE, dirNameStr, isPartialTree } from "./utils.js";

import { URL, fileURLToPath } from "node:url";
let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
const dirName = import.meta ? dirname(fileURLToPath(url)) : __dirname;
const dirName = dirNameStr;

/**
* Validate the generated bom using jsonschema
Expand Down
5 changes: 3 additions & 2 deletions binary.js → lib/managers/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import {
DEBUG_MODE,
TIMEOUT_MS,
adjustLicenseInformation,
dirNameStr,
findLicenseId,
isSpdxLicenseExpression,
} from "./utils.js";
} from "../helpers/utils.js";

import { URL, fileURLToPath } from "node:url";

let url = import.meta.url;
if (!url.startsWith("file://")) {
url = new URL(`file://${import.meta.url}`).toString();
}
const dirName = import.meta ? dirname(fileURLToPath(url)) : __dirname;
const dirName = dirNameStr;

const isWin = _platform() === "win32";

Expand Down
2 changes: 1 addition & 1 deletion docker.js → lib/managers/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { parse } from "node:url";
import { globSync } from "glob";
import got from "got";
import { x } from "tar";
import { DEBUG_MODE, getAllFiles } from "./utils.js";
import { DEBUG_MODE, getAllFiles } from "../helpers/utils.js";

export const isWin = _platform() === "win32";
export const DOCKER_HUB_REGISTRY = "docker.io";
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions server.js → lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import process from "node:process";
import url from "node:url";
import bodyParser from "body-parser";
import connect from "connect";
import { createBom, submitBom } from "./index.js";
import { postProcess } from "./postgen.js";
import { createBom, submitBom } from "../cli/index.js";
import { postProcess } from "../stages/postgen/postgen.js";

import compression from "compression";

Expand Down
6 changes: 5 additions & 1 deletion postgen.js → lib/stages/postgen/postgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import process from "node:process";
import { PackageURL } from "packageurl-js";
import { DEBUG_MODE, dirNameStr, hasAnyProjectType } from "./utils.js";
import {
DEBUG_MODE,
dirNameStr,
hasAnyProjectType,
} from "../../helpers/utils.js";

/**
* Filter and enhance BOM post generation.
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions pregen.js → lib/stages/pregen/pregen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
installSdkmanTool,
isNvmAvailable,
isSdkmanAvailable,
} from "./envcontext.js";
import { DEBUG_MODE, hasAnyProjectType } from "./utils.js";
} from "../../helpers/envcontext.js";
import { DEBUG_MODE, hasAnyProjectType } from "../../helpers/utils.js";

/**
* Method to prepare the build environment for BOM generation purposes.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
],
"type": "module",
"exports": "./index.js",
"exports": "./lib/cli/index.js",
"types": "./types/index.d.ts",
"bin": {
"cdxgen": "bin/cdxgen.js",
Expand All @@ -48,7 +48,7 @@
"cdx-verify": "bin/verify.js"
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --inject-globals false docker.test.js utils.test.js display.test.js postgen.test.js",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --inject-globals false lib/managers/docker.test.js lib/helpers/utils.test.js lib/helpers/display.test.js lib/stages/postgen/postgen.test.js",
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --inject-globals false",
"lint:check": "biome check",
"lint": "biome check --fix",
Expand Down Expand Up @@ -115,7 +115,7 @@
"sequelize": "^6.37.3",
"sqlite3": "^5.1.7"
},
"files": ["*.js", "bin/", "data/", "types/"],
"files": ["*.js", "lib/**", "bin/", "data/", "types/"],
"devDependencies": {
"@biomejs/biome": "1.8.3",
"jest": "^29.7.0",
Expand Down

0 comments on commit ddaa6be

Please sign in to comment.