merge scripts, tasks and config folder and convert code to typescript
This commit is contained in:
59
scripts/generateJson/file.ts
Normal file
59
scripts/generateJson/file.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { outputFile } from "fs-extra";
|
||||
import * as path from "path";
|
||||
import { walk } from "walk";
|
||||
|
||||
export const relativePath = (...relativePath: string[]): string =>
|
||||
path.resolve(...relativePath);
|
||||
|
||||
export const radarPath = (...pathInSrc: string[]) =>
|
||||
relativePath("radar", ...pathInSrc);
|
||||
|
||||
export const stylesPath = (...pathInSrc: string[]) =>
|
||||
relativePath("styles", ...pathInSrc);
|
||||
|
||||
export const faviconPath = (...pathInSrc: string[]) =>
|
||||
relativePath("assets/favicon.ico", ...pathInSrc);
|
||||
|
||||
export const jsPath = (...pathInSrc: string[]) =>
|
||||
relativePath("js", ...pathInSrc);
|
||||
|
||||
export const buildPath = (...pathInDist: string[]) =>
|
||||
relativePath("build", ...pathInDist);
|
||||
|
||||
export const getAllMarkdownFiles = (folder: string) =>
|
||||
getAllFiles(folder, isMarkdownFile);
|
||||
|
||||
const getAllFiles = (
|
||||
folder: string,
|
||||
predicate: (s: string) => boolean
|
||||
): Promise<string[]> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const walker = walk(folder, { followLinks: false });
|
||||
const files: string[] = [];
|
||||
|
||||
walker.on("file", (root, fileStat, next) => {
|
||||
if (predicate(fileStat.name)) {
|
||||
files.push(path.resolve(root, fileStat.name));
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
walker.on("errors", (root, nodeStatsArray, next) => {
|
||||
nodeStatsArray.forEach(function (n) {
|
||||
console.error("[ERROR] " + n.name);
|
||||
if (n.error) {
|
||||
console.error(n.error.message || n.error.code + ": " + n.error.path);
|
||||
}
|
||||
});
|
||||
next();
|
||||
});
|
||||
|
||||
walker.on("end", () => {
|
||||
resolve(files.sort());
|
||||
});
|
||||
});
|
||||
|
||||
const isMarkdownFile = (name: string) => name.match(/\.md$/) !== null;
|
||||
|
||||
export const save = (data: string | Buffer | DataView, fileName: string) =>
|
||||
outputFile(buildPath(fileName), data);
|
||||
178
scripts/generateJson/radar.ts
Normal file
178
scripts/generateJson/radar.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import { readFile } from "fs-extra";
|
||||
import * as path from "path";
|
||||
import frontMatter from "front-matter";
|
||||
// @ts-ignore esModuleInterop is activated in tsconfig.scripts.json, but IDE typescript uses default typescript config
|
||||
import marked from "marked";
|
||||
import highlight from "highlight.js";
|
||||
|
||||
import { radarPath, getAllMarkdownFiles } from "./file";
|
||||
import { quadrants, rings } from "../../src/config";
|
||||
import { Item, Revision, ItemAttributes, Radar } from "../../src/model";
|
||||
|
||||
type FMAttributes = ItemAttributes;
|
||||
|
||||
marked.setOptions({
|
||||
highlight: (code) => highlight.highlightAuto(code).value,
|
||||
});
|
||||
|
||||
export const createRadar = async (): Promise<Radar> => {
|
||||
const fileNames = await getAllMarkdownFiles(radarPath());
|
||||
const revisions = await createRevisionsFromFiles(fileNames);
|
||||
const allReleases = getAllReleases(revisions);
|
||||
const items = createItems(revisions);
|
||||
const flaggedItems = flagItem(items, allReleases);
|
||||
|
||||
return {
|
||||
items: flaggedItems,
|
||||
releases: allReleases,
|
||||
};
|
||||
};
|
||||
|
||||
const checkAttributes = (fileName: string, attributes: FMAttributes) => {
|
||||
if (attributes.ring && !rings.includes(attributes.ring)) {
|
||||
throw new Error(
|
||||
`Error: ${fileName} has an illegal value for 'ring' - must be one of ${rings}`
|
||||
);
|
||||
}
|
||||
|
||||
if (attributes.quadrant && !quadrants.includes(attributes.quadrant)) {
|
||||
throw new Error(
|
||||
`Error: ${fileName} has an illegal value for 'quadrant' - must be one of ${quadrants}`
|
||||
);
|
||||
}
|
||||
|
||||
return attributes;
|
||||
};
|
||||
|
||||
const createRevisionsFromFiles = (fileNames: string[]) =>
|
||||
Promise.all(
|
||||
fileNames.map((fileName) => {
|
||||
return new Promise<Revision>((resolve, reject) => {
|
||||
readFile(fileName, "utf8", (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
const fm = frontMatter<FMAttributes>(data);
|
||||
// add target attribute to external links
|
||||
// todo: check path
|
||||
let html = marked(fm.body.replace(/\]\(\//g, "](/techradar/"));
|
||||
html = html.replace(
|
||||
/a href="http/g,
|
||||
'a target="_blank" rel="noopener noreferrer" href="http'
|
||||
);
|
||||
|
||||
resolve({
|
||||
...itemInfoFromFilename(fileName),
|
||||
...checkAttributes(fileName, fm.attributes),
|
||||
fileName,
|
||||
body: html,
|
||||
} as Revision);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const itemInfoFromFilename = (fileName: string) => {
|
||||
const [release, name] = fileName.split(path.sep).slice(-2);
|
||||
return {
|
||||
name: path.basename(name, ".md"),
|
||||
release,
|
||||
};
|
||||
};
|
||||
|
||||
const getAllReleases = (revisions: Revision[]) =>
|
||||
revisions
|
||||
.reduce<string[]>((allReleases, { release }) => {
|
||||
if (!allReleases.includes(release)) {
|
||||
return [...allReleases, release];
|
||||
}
|
||||
return allReleases;
|
||||
}, [])
|
||||
.sort();
|
||||
|
||||
const createItems = (revisions: Revision[]) => {
|
||||
const itemMap = revisions.reduce<{ [name: string]: Item }>(
|
||||
(items, revision) => {
|
||||
return {
|
||||
...items,
|
||||
[revision.name]: addRevisionToItem(items[revision.name], revision),
|
||||
};
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
return Object.values(itemMap)
|
||||
.map((item) => ({ ...item, ["title"]: item.title || item.name }))
|
||||
.sort((x, y) => (x.name > y.name ? 1 : -1));
|
||||
};
|
||||
|
||||
const ignoreEmptyRevisionBody = (revision: Revision, item: Item) => {
|
||||
if (!revision.body || revision.body.trim() === "") {
|
||||
return item.body;
|
||||
}
|
||||
return revision.body;
|
||||
};
|
||||
|
||||
const addRevisionToItem = (
|
||||
item: Item = {
|
||||
flag: "default",
|
||||
featured: true,
|
||||
revisions: [],
|
||||
name: "",
|
||||
title: "",
|
||||
ring: "trial",
|
||||
quadrant: "",
|
||||
body: "",
|
||||
info: "",
|
||||
},
|
||||
revision: Revision
|
||||
): Item => {
|
||||
let newItem: Item = {
|
||||
...item,
|
||||
...revision,
|
||||
body: ignoreEmptyRevisionBody(revision, item),
|
||||
};
|
||||
|
||||
if (revisionCreatesNewHistoryEntry(revision)) {
|
||||
newItem = {
|
||||
...newItem,
|
||||
revisions: [revision, ...newItem.revisions],
|
||||
};
|
||||
}
|
||||
|
||||
return newItem;
|
||||
};
|
||||
|
||||
const revisionCreatesNewHistoryEntry = (revision: Revision) => {
|
||||
return revision.body.trim() !== "" || typeof revision.ring !== "undefined";
|
||||
};
|
||||
|
||||
const flagItem = (items: Item[], allReleases: string[]) =>
|
||||
items.map(
|
||||
(item) =>
|
||||
({
|
||||
...item,
|
||||
flag: getItemFlag(item, allReleases),
|
||||
} as Item),
|
||||
[]
|
||||
);
|
||||
|
||||
const isInLastRelease = (item: Item, allReleases: string[]) =>
|
||||
item.revisions[0].release === allReleases[allReleases.length - 1];
|
||||
|
||||
const isNewItem = (item: Item, allReleases: string[]) =>
|
||||
item.revisions.length === 1 && isInLastRelease(item, allReleases);
|
||||
|
||||
const hasItemChanged = (item: Item, allReleases: string[]) =>
|
||||
item.revisions.length > 1 && isInLastRelease(item, allReleases);
|
||||
|
||||
const getItemFlag = (item: Item, allReleases: string[]): string => {
|
||||
if (isNewItem(item, allReleases)) {
|
||||
return "new";
|
||||
}
|
||||
if (hasItemChanged(item, allReleases)) {
|
||||
return "changed";
|
||||
}
|
||||
return "default";
|
||||
};
|
||||
Reference in New Issue
Block a user