Last active
May 5, 2022 10:12
-
-
Save rallisf1/cd959bc8f267628ac3a7f83ffd0aeca6 to your computer and use it in GitHub Desktop.
Fix primo.af site assets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs-extra"); | |
const cheerio = require('cheerio'); | |
const crypto = require('crypto'); | |
const slug = require('slug'); | |
// recreate our assets directories aka empty them | |
fs.emptyDirSync('./assets/images'); | |
fs.emptyDirSync('./assets/styles'); | |
let buffer; | |
let name = ''; | |
let ext; | |
let styles = ''; | |
try { | |
if (fs.existsSync('./index.primo.html')) { | |
fs.unlinkSync('./index.primo.html'); | |
} | |
} catch(err) { | |
console.error(err) | |
} | |
fs.renameSync('./index.html', './index.primo.html'); | |
fs.readFile('./index.primo.html', 'utf8', function(err, data) { | |
if (err) { | |
return console.log(err); | |
} | |
const $ = cheerio.load(data); | |
$('img[src^="data:image"]').each(function(i, img) { | |
buffer = $(img).attr('src').split(','); | |
ext = buffer.shift(); | |
ext = ext.replace('data:image/', '').replace(';base64', ''); | |
buffer = buffer.join(','); | |
try { | |
name = slug($(img).attr('alt')) + '-'; | |
} catch (e) { | |
console.warn(`img #${i} missing alt attribute`); | |
} | |
name += crypto.createHash('md5').update(buffer).digest("hex"); | |
buffer = Buffer.from(buffer, "base64"); | |
fs.writeFileSync(`./assets/images/${name}.${ext}`, buffer); | |
$(img).attr('src', `/assets/images/${name}.${ext}`); | |
}); | |
$('style').each(function(_, style) { | |
styles += style.children[0].data.replaceAll(' ', ''); | |
$(style).remove(); | |
}); | |
// remove css imports | |
styles = styles.replaceAll("\n@import url(\"https://unpkg.com/@primo-app/[email protected]/reset.css\");", '') | |
.replaceAll("\n@import url('https://unpkg.com/@primo-app/[email protected]/reset.css');", '') | |
.replaceAll("\n@import url(\"https://unpkg.com/[email protected]/dist/base.css\");", ""); | |
name = crypto.createHash('md5').update(styles).digest("hex"); | |
fs.writeFileSync(`./assets/styles/${name}.css`, styles); | |
$('head').append(`<link rel="stylesheet" href="https://unpkg.com/@primo-app/[email protected]/reset.css">`); | |
// add minify argument if you use css-minify | |
if (process.argv.includes('minify')) { | |
const { execSync } = require('child_process'); | |
execSync('css-minify -d ./assets/styles -o ./assets/styles'); | |
if (fs.existsSync(`./assets/styles/${name}.min.css`)) name += '.min'; | |
} | |
$('head').append(`<link rel="stylesheet" href="/assets/styles/${name}.css">`); | |
if (process.argv.includes('minify')) { | |
const minify = require('html-minifier').minify; | |
fs.writeFileSync(`./index.html`, minify($.root().html())); | |
} else { | |
fs.writeFileSync(`./index.html`, $.root().html()); | |
} | |
return; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "primo_fix", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "rallisf1", | |
"license": "ISC", | |
"dependencies": { | |
"cheerio": "^1.0.0-rc.10", | |
"fs-extra": "^10.0.1", | |
"html-minifier": "^4.0.0", | |
"slug": "^5.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment