* Runs after Hugo has built the site * * Cleans, Minifies and Optimises hugo output * USAGE: node tools/pre-prod [path to hugo output] /***************************************************************************** *****************************************************************************/ const path = require('path') const SVGO = require('svgo') const { html: beautify_html } = require('js-beautify') /***************************************************************************** *****************************************************************************/ // looks for a path in first arg or default to `./public/` path.join(__dirname, `..`, process.argv[2] || 'public') + path.sep const xmlRegex = /\.(html|xml)$/ const jsonRegex = /\.json$/ const svgRegex = /\.svg$/ 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, 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 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()) { items.push(...readDir(item)) } else if (stat.isFile()) { return fs.readdirSync(item).map(i => path.join(item, i))