Skip to content

Commit

Permalink
Merge pull request #428 from NodeSecure/migrate-ts
Browse files Browse the repository at this point in the history
Migrate codebase to TypeScript
  • Loading branch information
fraxken authored Dec 26, 2024
2 parents c72043d + 3ae728a commit b85849b
Show file tree
Hide file tree
Showing 34 changed files with 389 additions and 269 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This package is available in the Node Package Repository and can be easily insta
$ git clone https://github.com/NodeSecure/report.git
$ cd report
$ npm i
$ npm run build
$ npm link
```

Expand Down Expand Up @@ -72,7 +73,7 @@ This uses the official NodeSecure [runtime configuration](https://github.com/Nod
{
"version": "1.0.0",
"i18n": "english",
"strategy": "npm",
"strategy": "github-advisory",
"report": {
"title": "NodeSecure Security Report",
"logoUrl": "https://avatars.githubusercontent.com/u/85318671?s=200&v=4",
Expand Down
15 changes: 11 additions & 4 deletions bin/commands/execute.js → bin/commands/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ import * as reporting from "../../src/reporting/index.js";
const kReadConfigOptions = {
createIfDoesNotExist: false,
createMode: "report"
};
} as const;

export async function execute(options = {}) {
export interface ExecuteOptions {
debug?: boolean;
}

export async function execute(options: ExecuteOptions = {}) {
const { debug: debugMode } = options;

if (debugMode) {
Expand All @@ -36,7 +40,10 @@ export async function execute(options = {}) {
const config = configResult.unwrap();
const { report } = config;

if (report.reporters.length === 0) {
if (!report) {
throw new Error("A valid configuration is required");
}
if (!report.reporters || report.reporters.length === 0) {
throw new Error("At least one reporter must be selected (either 'HTML' or 'PDF')");
}

Expand Down Expand Up @@ -75,7 +82,7 @@ function init() {
);
}

function debug(obj) {
function debug(obj: any) {
const filePath = path.join(CONSTANTS.DIRS.REPORTS, `debug-pkg-repo.txt`);
writeFileSync(filePath, inspect(obj, { showHidden: true, depth: null }), "utf8");
}
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/index.js → bin/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as commands from "./commands/index.js";
console.log(kleur.grey().bold(`\n > Executing nreport at: ${kleur.yellow().bold(process.cwd())}\n`));

const { version } = JSON.parse(
fs.readFileSync(new URL("../package.json", import.meta.url))
fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8")
);
const cli = sade("nreport").version(version);

Expand Down
26 changes: 5 additions & 21 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
// Import Node.js Dependencies
import path from "node:path";
import { fileURLToPath } from "node:url";
import { typescriptConfig, globals } from "@openally/config.eslint";

// Import Third-party Dependencies
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const compat = new FlatCompat({
baseDirectory: __dirname
});

export default [{
ignores: ["**/node_modules/", "**/tmp/", "**/dist/", "**/coverage/", "**/fixtures/", "**/public/lib"]
}, ...compat.extends("@nodesecure/eslint-config"), {
export default typescriptConfig({
languageOptions: {
sourceType: "module",

parserOptions: {
requireConfigFile: false
globals: {
...globals.browser
}
}
}];
});
32 changes: 17 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
"name": "@nodesecure/report",
"version": "3.0.0",
"description": "NodeSecure HTML & PDF graphic security report",
"main": "./bin/index.js",
"main": "./dist/src/index.js",
"type": "module",
"bin": {
"nreport": "./bin/index.js"
"nreport": "./dist/bin/index.js"
},
"exports": {
".": {
"import": "./src/index.js"
"import": "./dist/src/index.js"
}
},
"scripts": {
"lint": "eslint src test",
"test-only": "glob -c \"node --test-reporter=spec --test\" \"./test/**/*.spec.js\"",
"build": "tsc && npm run build:views && npm run build:public",
"build:views": "rimraf dist/views && cp -r views dist/views",
"build:public": "rimraf dist/public && cp -r public dist/public",
"lint": "eslint src test bin scripts",
"test-only": "glob -c \"tsx --test-reporter=spec --test\" \"./test/**/*.spec.ts\"",
"test": "c8 --all --src ./src -r html npm run test-only",
"preview:light": "node --no-warnings ./scripts/preview.js --theme light",
"preview:dark": "node --no-warnings ./scripts/preview.js --theme dark"
"preview:light": "tsx --no-warnings ./scripts/preview.js --theme light",
"preview:dark": "tsx --no-warnings ./scripts/preview.js --theme dark",
"prepublishOnly": "npm run build"
},
"files": [
"bin",
"public",
"src",
"views"
"dist"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -53,19 +54,20 @@
"@topcli/spinner": "^2.1.2",
"esbuild": "^0.24.0",
"filenamify": "^6.0.0",
"frequency-set": "^1.0.2",
"kleur": "^4.1.5",
"puppeteer": "23.6.0",
"sade": "^1.8.1",
"zup": "0.0.2"
},
"devDependencies": {
"@nodesecure/eslint-config": "2.0.0-beta.0",
"@openally/config.eslint": "^1.1.0",
"@openally/config.typescript": "^1.0.3",
"@types/node": "^22.2.0",
"c8": "^10.1.2",
"eslint": "^9.8.0",
"glob": "^11.0.0",
"open": "^10.1.0"
"open": "^10.1.0",
"tsx": "^4.19.2",
"typescript": "^5.7.2"
},
"engines": {
"node": ">=20"
Expand Down
1 change: 0 additions & 1 deletion public/lib/md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ function ii(a, b, c, d, x, s, t) {
}

function md51(s) {
txt = '';
var n = s.length,
state = [1732584193, -271733879, -1732584194, 271733878], i;
for (i = 64; i <= s.length; i += 64) {
Expand Down
2 changes: 2 additions & 0 deletions public/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-undef */

// Import Internal Dependencies
import { md5 } from "../lib/md5.js";

Expand Down
25 changes: 13 additions & 12 deletions scripts/preview.js → scripts/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,40 @@ const payload = (await import(
payload.report_theme = theme;

const config = {
theme,
theme: theme as ("light" | "dark"),
includeTransitiveInternal: false,
reporters: [ "html" ],
reporters: ["html" as const],
npm: {
organizationPrefix: "@nodesecure",
packages: [ "@nodesecure/js-x-ray" ]
packages: ["@nodesecure/js-x-ray"]
},
git: {
organizationUrl: "https://github.com/NodeSecure",
repositories: []
},
charts: [
{
name: "Extensions",
name: "Extensions" as const,
display: true,
interpolation: "d3.interpolateRainbow",
type: "bar"
type: "bar" as const
},
{
name: "Licenses",
name: "Licenses" as const,
display: true,
interpolation: "d3.interpolateCool",
type: "bar"
type: "bar" as const
},
{
name: "Warnings",
name: "Warnings" as const,
display: true,
type: "horizontalBar",
type: "horizontalBar" as const,
interpolation: "d3.interpolateInferno"
},
{
name: "Flags",
name: "Flags" as const,
display: true,
type: "horizontalBar",
type: "horizontalBar" as const,
interpolation: "d3.interpolateSinebow"
}
],
Expand All @@ -86,7 +86,8 @@ const config = {
};

const HTMLReport = new HTMLTemplateGenerator(
payload, config
payload,
config
).render({ asset_location: "./dist" });

const previewLocation = path.join(kPreviewDir, "preview.html");
Expand Down
Loading

0 comments on commit b85849b

Please sign in to comment.