Skip to content

Commit

Permalink
Switch to ESM, added default dir handling
Browse files Browse the repository at this point in the history
Signed-off-by: Dj Walker-Morgan <[email protected]>
  • Loading branch information
djw-m committed Sep 19, 2023
1 parent cc4ba15 commit 5cb7557
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
43 changes: 24 additions & 19 deletions tools/automation/generators/advisoryindex/advisoryindex.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const fs = require('fs');
const matter = require('gray-matter');
const MarkdownIt = require('markdown-it');
const njk = require('nunjucks');
const { basename, join } = require('path');
const parseArgs = require('minimist');
const { addAbortListener } = require('events');
import fs from "fs";
import matter from "gray-matter";
import MarkdownIt from "markdown-it";
import njk from "nunjucks";
import path from 'path';
import parseArgs from 'minimist';
import { fileURLToPath } from 'url';

// Modules hack for dirname via https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

var argv = parseArgs(process.argv.slice(2));

if (argv.root == undefined) {
console.log("Need --root");
process.exit(1);
}
let securityRoot = argv.root;

const securityRoot = argv.root;
if (securityRoot == undefined) {
securityRoot=path.normalize(path.join(__dirname,"..","..","..","..","advocacy_docs","security"));
console.log(`Using ${securityRoot} as working directory`);
}

let seccount = 10;

Expand All @@ -23,14 +27,14 @@ if (argv.count != undefined) {

const md = new MarkdownIt();
// We are going to process the advisories in
const advisoriesDir = join(securityRoot, "advisories");
const advisoriesDir = path.join(securityRoot, "advisories");
// To produce an index file named
const advisoriesIndex = join(advisoriesDir, "index.mdx");
const advisoriesIndex = path.join(advisoriesDir, "index.mdx");
// And another similar but shorted one named
const securityIndex = join(securityRoot, "index.mdx");
const securityIndex = path.join(securityRoot, "index.mdx");

// Using templates in a directory called
const templatesDir = join(securityRoot, "templates");
const templatesDir = path.join(securityRoot, "templates");

function parseMarkdownFile(filePath) {
const fileContent = fs.readFileSync(filePath, 'utf8');
Expand Down Expand Up @@ -58,6 +62,7 @@ function parseMarkdownFile(filePath) {
}
}
} else if (block.type == "heading_open") {
let value="";
if (currentSectionArray.length!=0) {
value=currentSectionArray;
currentSectionArray = [];
Expand All @@ -75,9 +80,9 @@ function parseMarkdownFile(filePath) {
// add the parsedMatter data to the docmap
docMap["frontmatter"]=parsedMatter.data;

const path = basename(filePath, ".mdx");
const cvepath = path.basename(filePath, ".mdx");

docMap["filename"] = path;
docMap["filename"] = cvepath;

return docMap;
}
Expand Down Expand Up @@ -113,7 +118,7 @@ let namespace = {};
let allDocMap = {};

cvelist.forEach(cve => {
const docMap = parseMarkdownFile(join(advisoriesDir, cve + '.mdx'));
const docMap = parseMarkdownFile(path.join(advisoriesDir, cve + '.mdx'));
// make sure the cve id isn't a link
docMap['vulnerability_details']['cve_id'] = cleanCVE(docMap['vulnerability_details']['cve_id']);
// trim the cve id off the front of the title
Expand Down
1 change: 1 addition & 0 deletions tools/automation/generators/advisoryindex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "advisoryindex.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down

1 comment on commit 5cb7557

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.