Created
May 8, 2018 10:38
-
-
Save BeMacized/d157bfbb1cc5507a2d3e1b9950ad52ae to your computer and use it in GitHub Desktop.
RP Minifier
This file contains 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
// | |
// OPTIONS | |
// | |
const RP_PATH = '/home/bemacized/Downloads/tomo'; | |
const OUTPUT_NAME= 'dist.zip'; | |
// FOR OPTIONS SEE DOCS AT https://github.com/imagemin/imagemin-pngquant | |
const PNG_MINIFY_OPTIONS = { | |
quality: '65-80' | |
}; | |
// | |
// IMPORTS | |
// | |
const imagemin = require('imagemin'); | |
const imageminPngquant = require('imagemin-pngquant'); | |
const Promise = require('bluebird').Promise; | |
const fs = require('fs'); | |
const glob = Promise.promisify(require('glob')); | |
const readFile = Promise.promisify(fs.readFile); | |
const writeFile = require('writefile'); | |
const exec = Promise.promisify(require('child_process').exec); | |
const rimraf = Promise.promisify(require('rimraf')); | |
// | |
// EXEC | |
// | |
(async () => { | |
// Obtain file references | |
const pngFiles = await glob(RP_PATH + '/**/*.png', {}); | |
const jsonFiles = await glob(RP_PATH + '/**/*.json', {}); | |
const otherFiles = await glob(RP_PATH + '/**/*.!(png|json)', {}); | |
// Minify PNGs | |
await Promise.all( | |
pngFiles.map(async (pngPath) => { | |
const destination = pngPath.substring(0, pngPath.lastIndexOf('/')).replace(RP_PATH, 'tmp'); | |
await imagemin([pngPath], destination, { | |
plugins: [imageminPngquant(PNG_MINIFY_OPTIONS)] | |
}); | |
console.log('MINIFIED', pngPath.replace(RP_PATH, '')) | |
}) | |
); | |
// Minify json | |
await Promise.all( | |
jsonFiles.map(async (jsonPath) => { | |
const json = JSON.parse(await readFile(jsonPath, 'utf8')); | |
const destination = jsonPath.replace(RP_PATH, 'tmp'); | |
await writeFile(destination, JSON.stringify(json)); | |
console.log('MINIFIED', jsonPath.replace(RP_PATH, '')); | |
}) | |
); | |
// Copy over additional files | |
await Promise.all( | |
otherFiles.map(async (otherPath) => { | |
const destination = otherPath.replace(RP_PATH, 'tmp'); | |
await writeFile(destination, fs.createReadStream(otherPath)); | |
console.log('COPIED', otherPath.replace('RP_PATH', '')) | |
}) | |
); | |
// ZIP | |
await exec(`cd tmp && zip -r -9 ../${OUTPUT_NAME} * && cd ..`); | |
console.log('GENERATED', OUTPUT_NAME); | |
// Remove tmp | |
await rimraf('tmp'); | |
console.log('CLEANUP COMPLETE'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment