--- a/tools/migrate-tracker.js Tue May 28 03:31:05 2019 +0100
+++ b/tools/migrate-tracker.js Tue May 28 03:56:55 2019 +0100
@@ -1,81 +1,149 @@
-const fs = require("fs");
-const path = require("path");
-const front = require("front-matter");
+ * Tracks the progress of migrating content + * from the the old site to the new site +/***************************************************************************** + *****************************************************************************/ +const fs = require('fs') +const path = require('path') +const front = require('front-matter') +/***************************************************************************** + *****************************************************************************/ +const filepath = __dirname.replace(/\/tools(\/)?$/, '/hugo/content') +const obsoleteRegex = /\s#(\s+)?obsolete(\s+)?$/i +/***************************************************************************** + *****************************************************************************/ - .readFileSync(path.join(__dirname, "paths.txt"), "utf8")
-const obsoleteRegex = /\s#(\s+)?obsolete(\s+)?$/i;
-const migrating = paths.filter(path => !obsoleteRegex.test("# obsolete"));
-const covered = new Set();
-function getFront(path) {
- const data = fs.readFileSync(path, "utf8");
- const { attributes } = front(data);
+ .readFileSync(path.join(__dirname, 'paths.txt'), 'utf8') + if (obsoleteRegex.test(path)) { + obsoleted.push(path.replace(obsoleteRegex, ''))
-const mdRegex = /\.md$/;
-function getMdPaths(input) {
- let items = fs.readdirSync(input).map(i => path.join(input, i));
- 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)) {
-const filepath = (__dirname.replace(/\/tools(\/)?$/, "/hugo/content"))
-getMdPaths(path.join(__dirname, "../hugo/content/"))
+/** Information on new content */ +const nestPages = getMdPaths(path.join(__dirname, '../hugo/content/')) + // discard translated content + .filter(path => !/\.\w+\.md$/.test(path)) - let replaces = getFront(path).replaces;
- } else if (typeof replaces === "string") {
- } else if (!Array.isArray(replaces)) {
- `expected replaces of ${path} to be falsey, string or an array`
+ const frontMatter = getFrontMatter(path) + const replaces = castReplacesDataToArray(frontMatter.replaces)
- .replace(/_index(\.\w\w)?$/, "")
+ // convert to realative file path + // remove file extention + .replace(/\/_index(\.\w\w)?$/, ''), - .forEach(page => page.replaces.forEach(path => covered.add(path)));
+/***************************************************************************** + *****************************************************************************/ +const covered = new Set() +nestPages.forEach(page => page.replaces.forEach(path => covered.add(path))) const countOfCovered = migrating.reduce(
(acc, path) => acc + (covered.has(path) ? 1 : 0),
+const percentComplete = ((countOfCovered / migrating.length) * 100).toPrecision( +const countOfObsolete = paths.length - migrating.length + tracking ${paths.length} pages + ${countOfCovered}/${migrating.length} (${percentComplete}%) migrated + ${countOfObsolete} obsoleted +/***************************************************************************** + * Generate redirection mapping + *****************************************************************************/ +// TODO: Generate redirection instructions +/***************************************************************************** + *****************************************************************************/ + * Retrieves front matter from markdown files + * @param {string} path path to mardown file +function getFrontMatter(path) { + const data = fs.readFileSync(path, 'utf8') + const { attributes } = front(data) -const percentComplete = (countOfCovered / migrating.length * 100).toPrecision(2);
-const countOfObsolete = paths.length - migrating.length;
- `tracking ${paths.length} pages, ${countOfCovered}/${migrating.length} (${percentComplete}%) migrated, ${countOfObsolete} obsoleted`
+ * creates a list of all markdown files in a directory + * @param {string} directory directory to search for markdown files +function getMdPaths(directory) { + let items = fs.readdirSync(directory).map(i => path.join(directory, i)) + const item = items.pop() + const stat = fs.statSync(item) + if (stat.isDirectory()) { + const nueveau = fs.readdirSync(item).map(i => path.join(item, i)) + } else if (stat.isFile() && mdRegex.test(item)) { + * @param {void|string|string[]} replaces +function castReplacesDataToArray(replaces) { + } else if (typeof replaces === 'string') { + } else if (Array.isArray(replaces)) { + `expected replaces of ${path} to be falsey, string or an array` --- a/tools/pre-prod.js Tue May 28 03:31:05 2019 +0100
+++ b/tools/pre-prod.js Tue May 28 03:56:55 2019 +0100
@@ -1,8 +1,21 @@
-const { html: beautify_html } = require('js-beautify')
+ * Runs after Hugo has built the site + * * Cleans, Minifies and Optimises hugo output +/***************************************************************************** + *****************************************************************************/ const path = require('path')
const SVGO = require('svgo')
+const { html: beautify_html } = require('js-beautify') +/***************************************************************************** + *****************************************************************************/ const target = path.join(__dirname, '../public/')
@@ -11,63 +24,96 @@
const jsonRegex = /\.json$/
const svgRegex = /\.svg$/
-getFilePaths(target, tidyPath)
+const htmlBeautifyOptions = { max_preserve_newlines: 0 } +/***************************************************************************** + *****************************************************************************/ +getFilePaths(target).forEach(path => { + if (path.match(xmlRegex)) { + } else if (path.match(jsonRegex)) { + } else if (path.match(svgRegex)) { +/***************************************************************************** + *****************************************************************************/ + * Format XML and HTML, removing hugo template artefacts, + * like whitespace chasms +function tidyXML(path) { + const data = fs.readFileSync(path, 'utf8') + const formatted = beautify_html(data, htmlBeautifyOptions) + fs.writeFile(path, formatted, err => { + console.log(`Error Tidiyng ${path}`, err) + console.log(`Tidied path: ${path.replace(target, '')}`) -function getFilePaths(input, cb) {
- let items = readDir(input)
+function minifyJSON(path) { + const data = fs.readFileSync(path, 'utf8') + const formatted = JSON.stringify(JSON.parse(data)) + fs.writeFile(path, formatted, err => { + console.log(`Error minifying ${path}`, err) + console.log(`Minified path: ${path.replace(target, '')}`) +function optimiseSVG(path) { + const data = fs.readFileSync(path, 'utf8') + fs.writeFileSync(path, d.data) + console.log(`Optimised: ${path.replace(target, '')}`) + .catch(err => console.log(`Error Optimising SVG ${path}`, err)) +/***************************************************************************** + *****************************************************************************/ +function getFilePaths(input) { + const items = readDir(input) const stat = fs.statSync(item)
if (stat.isDirectory()) {
- const nueveau = readDir(item)
+ items.push(...readDir(item)) } else if (stat.isFile()) {
return fs.readdirSync(item).map(i => path.join(item, i))
-function tidyPath(path) {
- if (path.match(xmlRegex)) {
- const data = fs.readFileSync(path, 'utf8')
- max_preserve_newlines: 0,
- console.log(`Error Tidiyng ${path}`, err)
- console.log(`Tidied path: ${path.replace(target, '')}`)
- } else if (path.match(jsonRegex)) {
- const data = fs.readFileSync(path, 'utf8')
- fs.writeFile(path, JSON.stringify(JSON.parse(data)), err => {
- console.log(`Error minifying ${path}`, err)
- console.log(`Minified path: ${path.replace(target, '')}`)
- } else if (path.match(svgRegex)) {
- const data = fs.readFileSync(path, 'utf8')
- fs.writeFileSync(path, d.data)
- console.log(`Optimised: ${path.replace(target, '')}`)
- .catch(err => console.log(`Error Optimising SVG ${path}`, err))