pidgin/nest

closing merged branch
remove-old-protocols
2019-04-16, Gary Kramlich
280560865eb6
closing merged branch
const fs = require("fs");
const path = require("path");
const front = require("front-matter");
const paths = fs
.readFileSync(path.join(__dirname, "paths.txt"), "utf8")
.split("\r\n");
const covered = new Set();
function getFront(path) {
try {
const data = fs.readFileSync(path, "utf8");
const { attributes } = front(data);
return attributes;
} catch (error) {
console.log(error);
}
}
const mdRegex = /\.md$/;
function getMdPaths(input) {
let output = [];
let items = fs.readdirSync(input).map(i => path.join(input, i));
while (items.length) {
const item = items.pop();
const stat = fs.statSync(item);
if (stat.isDirectory()) {
const nueveau = fs.readdirSync(item).map(i => path.join(item, i));
items.push(...nueveau);
} else if (stat.isFile() && mdRegex.test(item)) {
output.push(item);
}
}
return output.sort();
}
const filepath = (__dirname.replace(/\/tools(\/)?$/, "/hugo/content"))
getMdPaths(path.join(__dirname, "../hugo/content/"))
.map(path => {
let replaces = getFront(path).replaces;
if (!replaces) {
replaces = [];
} else if (typeof replaces === "string") {
replaces = [replaces];
} else if (!Array.isArray(replaces)) {
throw new Error(
`expected replaces of ${path} to be falsey, string or an array`
);
}
return {
replaces,
path,
relative: path
.replace(filepath, "")
.replace(/\.md$/, "")
.replace(/_index(\.\w\w)?$/, "")
};
})
.forEach(page => page.replaces.forEach(path => covered.add(path)));
const countOfCovered = paths.reduce(
(acc, path) => acc + (covered.has(path) ? 1 : 0),
0
);
console.log(
`Currently there are ${countOfCovered} of ${paths.length} pages covered`
);