imfreedom/www.imfreedom.org
Clone
Summary
Browse
Changes
Graph
Update everything for Discourse
16 months ago, Gary Kramlich
a4501b4f5c1e
Update everything for Discourse
This includes saying that we shutdown the mailing lists which we haven't
actually done yet, but I don't want to have to come back and make another commit
when we do.
Testing Done:
Ran locally via `npm run hugo:server`
Reviewed at https://reviews.imfreedom.org/r/2153/
/**
* Runs after Hugo has built the site
* * Cleans, Minifies and Optimises hugo output
*/
/*****************************************************************************
* Imports
*****************************************************************************/
const
fs
=
require
(
'fs'
)
const
path
=
require
(
'path'
)
const
SVGO
=
require
(
'svgo'
)
const
{
html
:
beautify_html
}
=
require
(
'js-beautify'
)
/*****************************************************************************
* Set Up
*****************************************************************************/
const
target
=
path
.
join
(
__dirname
,
'../public/'
)
const
svgo
=
new
SVGO
()
const
xmlRegex
=
/\.(html|xml)$/
const
jsonRegex
=
/\.json$/
const
svgRegex
=
/\.svg$/
const
htmlBeautifyOptions
=
{
max_preserve_newlines
:
0
}
/*****************************************************************************
* Execution
*****************************************************************************/
getFilePaths
(
target
).
forEach
(
path
=>
{
if
(
path
.
match
(
xmlRegex
))
{
tidyXML
(
path
)
}
else
if
(
path
.
match
(
jsonRegex
))
{
minifyJSON
(
path
)
}
else
if
(
path
.
match
(
svgRegex
))
{
optimiseSVG
(
path
)
}
})
/*****************************************************************************
* File Handlers
*****************************************************************************/
/**
* Format XML and HTML, removing hugo template artefacts,
* like whitespace chasms
* @param {string} path
*/
function
tidyXML
(
path
)
{
const
data
=
fs
.
readFileSync
(
path
,
'utf8'
)
const
formatted
=
beautify_html
(
data
,
htmlBeautifyOptions
)
fs
.
writeFile
(
path
,
formatted
,
err
=>
{
if
(
err
)
{
console
.
log
(
`Error Tidiyng
${
path
}
`
,
err
)
}
console
.
log
(
`Tidied path:
${
path
.
replace
(
target
,
''
)
}
`
)
})
}
/**
* Minify JSON
* @param {string} path
*/
function
minifyJSON
(
path
)
{
const
data
=
fs
.
readFileSync
(
path
,
'utf8'
)
const
formatted
=
JSON
.
stringify
(
JSON
.
parse
(
data
))
fs
.
writeFile
(
path
,
formatted
,
err
=>
{
if
(
err
)
{
console
.
log
(
`Error minifying
${
path
}
`
,
err
)
}
console
.
log
(
`Minified path:
${
path
.
replace
(
target
,
''
)
}
`
)
})
}
/**
* Optimise SVG files
* @param {string} path
*/
function
optimiseSVG
(
path
)
{
const
data
=
fs
.
readFileSync
(
path
,
'utf8'
)
svgo
.
optimize
(
data
)
.
then
(
d
=>
{
fs
.
writeFileSync
(
path
,
d
.
data
)
console
.
log
(
`Optimised:
${
path
.
replace
(
target
,
''
)
}
`
)
})
.
catch
(
err
=>
console
.
log
(
`Error Optimising SVG
${
path
}
`
,
err
))
}
/*****************************************************************************
* Helpers
*****************************************************************************/
function
getFilePaths
(
input
)
{
const
items
=
readDir
(
input
)
const
ret
=
[]
while
(
items
.
length
)
{
const
item
=
items
.
pop
()
const
stat
=
fs
.
statSync
(
item
)
if
(
stat
.
isDirectory
())
{
items
.
push
(...
readDir
(
item
))
}
else
if
(
stat
.
isFile
())
{
ret
.
push
(
item
)
}
}
return
ret
}
function
readDir
(
item
)
{
return
fs
.
readdirSync
(
item
).
map
(
i
=>
path
.
join
(
item
,
i
))
}