pidgin/nest

e97179cd277f
Create a short code fore repository and use it.

Due to the impending migration of all of our repos, I figured a short code
was a good way to make that migration have as little impact as possible on
nest as we now have one variable to flip. There's some other nastiness
that we'll have to deal with, but this gets the brunt of it.
/**
* Tracks the progress of migrating content
* from the the old site to the new site
*/
/*****************************************************************************
* Imports
*****************************************************************************/
const fs = require('fs')
const path = require('path')
const front = require('front-matter')
/*****************************************************************************
* Set Up
*****************************************************************************/
const filepath = __dirname.replace(/\/tools(\/)?$/, '/hugo/content')
const obsoleteRegex = /\s#(\s+)?obsolete(\s+)?$/i
const mdRegex = /\.md$/
const migrating = []
const obsoleted = []
/*****************************************************************************
* Execution
*****************************************************************************/
const paths = fs
.readFileSync(path.join(__dirname, 'paths.txt'), 'utf8')
.split('\r\n')
// Sift each URL
paths.forEach(path => {
if (obsoleteRegex.test(path)) {
obsoleted.push(path.replace(obsoleteRegex, ''))
} else {
migrating.push(path)
}
})
/** Information on new content */
const nestPages = getMdPaths(path.join(__dirname, '../hugo/content/'))
// discard translated content
.filter(path => !/\.\w+\.md$/.test(path))
.map(path => {
const frontMatter = getFrontMatter(path)
const replaces = castReplacesDataToArray(frontMatter.replaces)
return {
replaces,
path,
pathOnSite: path
// convert to relative file path
.replace(filepath, '')
// remove file extension
.replace(/\.md$/, '')
// remove _index
.replace(/\/_index(\.\w\w)?$/, ''),
}
})
/*****************************************************************************
* Generate Stats
*****************************************************************************/
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),
0
)
const percentComplete = ((countOfCovered / migrating.length) * 100).toPrecision(
2
)
const countOfObsolete = paths.length - migrating.length
console.log(`
STATS:
tracking ${paths.length} pages
${countOfCovered}/${migrating.length} (${percentComplete}%) migrated
${countOfObsolete} obsoleted
`)
/*****************************************************************************
* Generate redirection mapping
*****************************************************************************/
// TODO: Generate redirection instructions
/*****************************************************************************
* Helpers
*****************************************************************************/
/**
* Retrieves front matter from markdown files
* @param {string} path path to markdown file
*/
function getFrontMatter(path) {
try {
const data = fs.readFileSync(path, 'utf8')
const { attributes } = front(data)
return attributes
} catch (error) {
console.log(error)
}
}
/**
* creates a list of all markdown files in a directory
* @param {string} directory directory to search for markdown files
*/
function getMdPaths(directory) {
let output = []
let items = fs.readdirSync(directory).map(i => path.join(directory, i))
while (items.length) {
const item = items.pop()
const stat = fs.statSync(item)
if (stat.isDirectory()) {
items.push(...fs.readdirSync(item).map(i => path.join(item, i)))
} else if (stat.isFile() && mdRegex.test(item)) {
output.push(item)
}
}
return output.sort()
}
/**
*
* @param {void|string|string[]} replaces
*/
function castReplacesDataToArray(replaces) {
if (!replaces) {
return []
} else if (typeof replaces === 'string') {
return [replaces]
} else if (Array.isArray(replaces)) {
return replaces
}
throw new Error(
`expected replaces of ${path} to be falsey, string or an array`
)
}