merge scripts, tasks and config folder and convert code to typescript
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
import * as fs from "fs-extra";
|
||||
import { spawn } from "child_process";
|
||||
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";
|
||||
@@ -13,13 +15,9 @@ process.on("unhandledRejection", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
const fs = require("fs-extra");
|
||||
const paths = require("../config/paths");
|
||||
const childProcess = require("child_process");
|
||||
|
||||
const runCommand = (command, args) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const executedCommand = childProcess.spawn(command, args, {
|
||||
const runCommand = (command: string) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const executedCommand = spawn(command, {
|
||||
stdio: "inherit",
|
||||
shell: true,
|
||||
});
|
||||
@@ -30,13 +28,15 @@ const runCommand = (command, args) => {
|
||||
|
||||
executedCommand.on("exit", (code) => {
|
||||
if (code === 0) {
|
||||
resolve();
|
||||
resolve(code);
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
};
|
||||
|
||||
const buildTemplate = () => {
|
||||
const packageManager = fs.existsSync(paths.appYarnLock) ? "yarn" : "npx";
|
||||
@@ -44,10 +44,7 @@ const buildTemplate = () => {
|
||||
fs.emptyDirSync(paths.templateBuild);
|
||||
process.chdir(paths.template);
|
||||
|
||||
return runCommand(`${packageManager} build`).catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
return runCommand(`${packageManager} build`);
|
||||
};
|
||||
|
||||
if (fs.existsSync(paths.appRdJson)) {
|
||||
44
scripts/createStaticFiles.ts
Normal file
44
scripts/createStaticFiles.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { copyFileSync, mkdirSync, existsSync } from "fs";
|
||||
import { createRadar } from "./generateJson/radar";
|
||||
import { quadrants } from "../src/config";
|
||||
|
||||
// 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;
|
||||
});
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
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");
|
||||
|
||||
quadrants.forEach((quadrant) => {
|
||||
const destFolder = `build/${quadrant}`;
|
||||
copyFileSync("build/index.html", `${destFolder}.html`);
|
||||
if (!existsSync(destFolder)) {
|
||||
mkdirSync(destFolder);
|
||||
}
|
||||
});
|
||||
radar.items.forEach((item) => {
|
||||
copyFileSync(
|
||||
"build/index.html",
|
||||
`build/${item.quadrant}/${item.name}.html`
|
||||
);
|
||||
});
|
||||
|
||||
console.log("created static files.");
|
||||
} catch (e) {
|
||||
console.error("error:", e);
|
||||
}
|
||||
})();
|
||||
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
"use strict";
|
||||
import * as paths from "./paths";
|
||||
import { createRadar } from "./generateJson/radar";
|
||||
import { save } from "./generateJson/file";
|
||||
|
||||
// Do this as the first thing so that any code reading it knows the right env.
|
||||
process.env.BABEL_ENV = "production";
|
||||
@@ -13,10 +15,17 @@ process.on("unhandledRejection", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
const paths = require("../config/paths");
|
||||
const generateJson = async () => {
|
||||
try {
|
||||
const radar = await createRadar();
|
||||
|
||||
require("../bin/tasks/radarjson")
|
||||
.radarJsonGenerator()
|
||||
await save(JSON.stringify(radar), paths.radarJson);
|
||||
} catch (e) {
|
||||
console.error("error:", e);
|
||||
}
|
||||
};
|
||||
|
||||
generateJson()
|
||||
.then(() => {
|
||||
console.log(`${paths.appRdJson} created.`);
|
||||
})
|
||||
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";
|
||||
};
|
||||
15
scripts/paths.ts
Normal file
15
scripts/paths.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { resolve } from "path";
|
||||
import { realpathSync } from "fs";
|
||||
|
||||
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();
|
||||
export const templateBuild = resolveTemplate("build");
|
||||
export const appRdJson = resolveApp(`build/${radarJson}`);
|
||||
export const appBuild = resolveApp("build");
|
||||
export const appYarnLock = resolveApp("yarn.lock");
|
||||
Reference in New Issue
Block a user