feat: cleanup
This commit is contained in:
committed by
Mathias Schopmans
parent
1583363ab8
commit
ab005e03b3
@@ -1,66 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
import { spawn } from "child_process";
|
||||
import { randomBytes } from "crypto";
|
||||
import * as fs from "fs-extra";
|
||||
|
||||
import * as paths from "./paths";
|
||||
|
||||
// Do this as the first thing so that any code reading it knows the right env.
|
||||
process.env.BABEL_ENV = "production";
|
||||
process.env.NODE_ENV = "production";
|
||||
|
||||
// Makes the script crash on unhandled rejections instead of silently
|
||||
// ignoring them. In the future, promise rejections that are not handled will
|
||||
// terminate the Node.js process with a non-zero exit code.
|
||||
process.on("unhandledRejection", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
fs.removeSync(paths.templateNodeModules);
|
||||
fs.ensureSymlinkSync(paths.appNodeModules, paths.templateNodeModules);
|
||||
|
||||
const runCommand = (command: string) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const executedCommand = spawn(command, {
|
||||
stdio: "inherit",
|
||||
shell: true,
|
||||
env: {
|
||||
REACT_APP_RADAR_NAME: "AOE Technology Radar",
|
||||
REACT_APP_RADAR_TITLE_FORMAT: "%TECHNOLOGY_NAME% | %APP_TITLE%",
|
||||
REACT_APP_BUILDHASH: randomBytes(10).toString("hex"),
|
||||
GENERATE_SOURCEMAP: "false",
|
||||
...process.env,
|
||||
},
|
||||
});
|
||||
|
||||
executedCommand.on("error", (error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
executedCommand.on("exit", (code) => {
|
||||
if (code === 0) {
|
||||
resolve(code);
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const buildTemplate = () => {
|
||||
const packageManager = fs.existsSync(paths.appYarnLock) ? "yarn" : "npm";
|
||||
|
||||
fs.emptyDirSync(paths.templateBuild);
|
||||
process.chdir(paths.template);
|
||||
|
||||
return runCommand(`${packageManager} run build`);
|
||||
};
|
||||
|
||||
buildTemplate().then(() => {
|
||||
fs.copySync(paths.templateBuild, paths.appBuild);
|
||||
fs.ensureDirSync(paths.appPublic);
|
||||
fs.copySync(paths.appPublic, paths.appBuild);
|
||||
console.log(`${paths.appBuild} was created and can be deployed.`);
|
||||
});
|
||||
@@ -1,105 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
import {
|
||||
copyFileSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
} from "fs";
|
||||
import { JSDOM } from "jsdom";
|
||||
import XmlSitemap from "xml-sitemap";
|
||||
|
||||
import { publicUrl, setTitle } from "../src/config";
|
||||
import { createRadar } from "./generateJson/radar";
|
||||
|
||||
// Do this as the first thing so that any code reading it knows the right env.
|
||||
process.env.BABEL_ENV = "production";
|
||||
process.env.NODE_ENV = "production";
|
||||
|
||||
// Makes the script crash on unhandled rejections instead of silently
|
||||
// ignoring them. In the future, promise rejections that are not handled will
|
||||
// terminate the Node.js process with a non-zero exit code.
|
||||
process.on("unhandledRejection", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
const createStaticFiles = async () => {
|
||||
console.log("starting static");
|
||||
const radar = await createRadar();
|
||||
|
||||
copyFileSync("build/index.html", "build/overview.html");
|
||||
copyFileSync("build/index.html", "build/help-and-about-tech-radar.html");
|
||||
const rawConf = readFileSync("build/config.json", "utf-8");
|
||||
const config = JSON.parse(rawConf);
|
||||
|
||||
Object.keys(config.quadrants).forEach((quadrant) => {
|
||||
const destFolder = `build/${quadrant}`;
|
||||
copyFileSync("build/index.html", `${destFolder}.html`);
|
||||
if (!existsSync(destFolder)) {
|
||||
mkdirSync(destFolder);
|
||||
}
|
||||
});
|
||||
|
||||
const sitemap = new XmlSitemap();
|
||||
const sitemapOptions = {
|
||||
lastmod: "now",
|
||||
changefreq: "weekly",
|
||||
};
|
||||
|
||||
sitemap.add(`${publicUrl}index.html`, sitemapOptions);
|
||||
|
||||
radar.items.forEach((item) => {
|
||||
const targetPath = `build/${item.quadrant}/${item.name}.html`;
|
||||
copyFileSync("build/index.html", targetPath);
|
||||
|
||||
JSDOM.fromFile(targetPath).then((dom) => {
|
||||
const document = dom.window.document;
|
||||
const rootEl = document.getElementById("root");
|
||||
|
||||
setTitle(document, item.title);
|
||||
|
||||
if (rootEl) {
|
||||
const textNode = document.createElement("div");
|
||||
const bodyFragment = JSDOM.fragment(item.body);
|
||||
textNode.appendChild(bodyFragment);
|
||||
|
||||
const headlineNode = document.createElement("h1");
|
||||
const titleText = document.createTextNode(item.title);
|
||||
headlineNode.appendChild(titleText);
|
||||
|
||||
rootEl.appendChild(headlineNode);
|
||||
rootEl.appendChild(textNode);
|
||||
|
||||
// remove the <noscript> element as page has already been hydrated with static content
|
||||
const noscriptEl = document.getElementsByTagName("noscript");
|
||||
if (noscriptEl[0]) {
|
||||
noscriptEl[0].remove();
|
||||
}
|
||||
} else {
|
||||
console.warn(
|
||||
'Element with ID "root" not found. Static site content will be empty.'
|
||||
);
|
||||
}
|
||||
|
||||
writeFileSync(targetPath, dom.serialize());
|
||||
});
|
||||
|
||||
sitemap.add(
|
||||
`${publicUrl}${item.quadrant}/${item.name}.html`,
|
||||
sitemapOptions
|
||||
);
|
||||
});
|
||||
|
||||
writeFileSync("build/sitemap.xml", sitemap.xml);
|
||||
};
|
||||
|
||||
createStaticFiles()
|
||||
.then(() => {
|
||||
console.log(`created static files.`);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err && err.message) {
|
||||
console.error(err.message);
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
import * as fs from "fs-extra";
|
||||
|
||||
import { radarPath } from "./generateJson/file";
|
||||
import * as paths from "./paths";
|
||||
|
||||
// Do this as the first thing so that any code reading it knows the right env.
|
||||
process.env.BABEL_ENV = "production";
|
||||
process.env.NODE_ENV = "production";
|
||||
|
||||
// Makes the script crash on unhandled rejections instead of silently
|
||||
// ignoring them. In the future, promise rejections that are not handled will
|
||||
// terminate the Node.js process with a non-zero exit code.
|
||||
process.on("unhandledRejection", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
fs.removeSync(paths.templateNodeModules);
|
||||
fs.ensureSymlinkSync(paths.appNodeModules, paths.templateNodeModules);
|
||||
|
||||
try {
|
||||
fs.statSync(radarPath());
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`${radarPath()} not found. Please create ${radarPath()} and add your markdown files to build the techradar.`
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const generateJson = async () => {
|
||||
const { createRadar } = require("./generateJson/radar");
|
||||
const { save } = require("./generateJson/file");
|
||||
|
||||
const radar = await createRadar();
|
||||
await save(JSON.stringify(radar), paths.radarJson);
|
||||
};
|
||||
|
||||
generateJson()
|
||||
.then(() => {
|
||||
console.log(`${paths.appRdJson} created.`);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err && err.message) {
|
||||
console.error(err.message);
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,59 +0,0 @@
|
||||
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);
|
||||
@@ -1,205 +0,0 @@
|
||||
import frontMatter from "front-matter";
|
||||
import { readFileSync } from "fs";
|
||||
import { readFile } from "fs-extra";
|
||||
import highlight from "highlight.js";
|
||||
import { marked } from "marked";
|
||||
import * as path from "path";
|
||||
|
||||
import { ConfigData, publicUrl } from "../../src/config";
|
||||
import {
|
||||
FlagType,
|
||||
Item,
|
||||
ItemAttributes,
|
||||
Radar,
|
||||
Revision,
|
||||
} from "../../src/model";
|
||||
import { appBuild } from "../paths";
|
||||
import { getAllMarkdownFiles, radarPath } from "./file";
|
||||
|
||||
marked.setOptions({
|
||||
highlight: (code: any) => highlight.highlightAuto(code).value,
|
||||
});
|
||||
|
||||
export const createRadar = async (): Promise<Radar> => {
|
||||
const fileNames = await getAllMarkdownFiles(radarPath());
|
||||
const revisions: (Revision | undefined)[] = await createRevisionsFromFiles(
|
||||
fileNames
|
||||
);
|
||||
const filterdRevisions: Revision[] = revisions.filter(
|
||||
(r) => r !== undefined
|
||||
) as Revision[];
|
||||
const allReleases = getAllReleases(filterdRevisions);
|
||||
const items = createItems(filterdRevisions);
|
||||
const flaggedItems = flagItem(items, allReleases);
|
||||
|
||||
items.forEach((item) => checkAttributes(item.name, item));
|
||||
|
||||
return {
|
||||
items: flaggedItems,
|
||||
releases: allReleases,
|
||||
};
|
||||
};
|
||||
|
||||
const checkAttributes = (fileName: string, attributes: ItemAttributes) => {
|
||||
const rawConf = readFileSync(path.resolve(appBuild, "config.json"), "utf-8");
|
||||
const config = JSON.parse(rawConf) as ConfigData;
|
||||
|
||||
if (!config.rings.includes(attributes.ring)) {
|
||||
throw new Error(
|
||||
`Error: ${fileName} has an illegal value for 'ring' - must be one of ${config.rings}`
|
||||
);
|
||||
}
|
||||
|
||||
const quadrants = Object.keys(config.quadrants);
|
||||
if (!quadrants.includes(attributes.quadrant)) {
|
||||
throw new Error(
|
||||
`Error: ${fileName} has an illegal value for 'quadrant' - must be one of ${quadrants}`
|
||||
);
|
||||
}
|
||||
|
||||
if (config.tags) {
|
||||
for (let tag of config.tags) {
|
||||
if (attributes.tags && attributes.tags.includes(tag)) {
|
||||
return attributes;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
} else {
|
||||
return attributes;
|
||||
}
|
||||
};
|
||||
|
||||
const createRevisionsFromFiles = (fileNames: string[]) => {
|
||||
return Promise.all(
|
||||
fileNames.map((fileName) =>
|
||||
readFile(fileName, "utf8").then((data) => {
|
||||
const fm = frontMatter<ItemAttributes>(data);
|
||||
let html = marked(fm.body.replace(/\]\(\//g, `](${publicUrl}`));
|
||||
html = html.replace(
|
||||
/a href="http/g,
|
||||
'a target="_blank" rel="noopener noreferrer" href="http'
|
||||
);
|
||||
const attributes = checkAttributes(fileName, fm.attributes);
|
||||
if (attributes) {
|
||||
return {
|
||||
...itemInfoFromFilename(fileName),
|
||||
...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: FlagType.default,
|
||||
featured: true,
|
||||
revisions: [],
|
||||
name: "",
|
||||
title: "",
|
||||
ring: "trial",
|
||||
quadrant: "",
|
||||
body: "",
|
||||
info: "",
|
||||
angleFraction: Math.random(),
|
||||
radiusFraction: Math.random(),
|
||||
},
|
||||
revision: Revision
|
||||
): Item => {
|
||||
let newItem: Item = {
|
||||
...item,
|
||||
...revision,
|
||||
body: ignoreEmptyRevisionBody(revision, item),
|
||||
};
|
||||
|
||||
if (revisionCreatesNewHistoryEntry(revision, item)) {
|
||||
newItem = {
|
||||
...newItem,
|
||||
revisions: [revision, ...newItem.revisions],
|
||||
};
|
||||
}
|
||||
|
||||
return newItem;
|
||||
};
|
||||
|
||||
const revisionCreatesNewHistoryEntry = (revision: Revision, item: Item) => {
|
||||
return (
|
||||
revision.body.trim() !== "" ||
|
||||
(typeof revision.ring !== "undefined" && revision.ring !== item.ring) ||
|
||||
(typeof revision.quadrant !== "undefined" &&
|
||||
revision.quadrant !== item.quadrant)
|
||||
);
|
||||
};
|
||||
|
||||
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 FlagType.new;
|
||||
}
|
||||
if (hasItemChanged(item, allReleases)) {
|
||||
return FlagType.changed;
|
||||
}
|
||||
return FlagType.default;
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
import { realpathSync } from "fs";
|
||||
import { resolve } from "path";
|
||||
|
||||
export const radarJson = "rd.json";
|
||||
const appDirectory = realpathSync(process.cwd());
|
||||
const resolveApp = (relativePath = "") => resolve(appDirectory, relativePath);
|
||||
const templateDirectory = realpathSync(__dirname);
|
||||
const resolveTemplate = (relativePath = "") =>
|
||||
resolve(templateDirectory, "../..", relativePath);
|
||||
|
||||
export const template = resolveTemplate(); // this repository
|
||||
export const templateBuild = resolveTemplate("build"); // build folder in this repository
|
||||
export const templateNodeModules = resolveTemplate("node_modules"); // node_modules folder in this repository
|
||||
export const appRdJson = resolveApp(`build/${radarJson}`); // build/rd.json in project
|
||||
export const appBuild = resolveApp("build"); // build folder in project
|
||||
export const appPublic = resolveApp("public"); // public folder in project
|
||||
export const appYarnLock = resolveApp("yarn.lock"); // yarn.lock in project
|
||||
export const appNodeModules = resolveApp("node_modules"); // node_modules folder in project
|
||||
1
scripts/types.d.ts
vendored
1
scripts/types.d.ts
vendored
@@ -1 +0,0 @@
|
||||
declare module "xml-sitemap";
|
||||
Reference in New Issue
Block a user