diff options
author | Clement Ho <ClemMakesApps@gmail.com> | 2017-05-19 18:56:02 -0500 |
---|---|---|
committer | Clement Ho <ClemMakesApps@gmail.com> | 2017-05-19 20:04:26 -0500 |
commit | 44765724b105eaf29e1ac689d5509cb3dc416216 (patch) | |
tree | 3b60d5043e32e9b4e6281ce24940c486b8bdf1a9 /scripts/check_svg.js | |
parent | 9d2d104293f7a4e012de14c6df2601aa59bfe884 (diff) | |
download | gitlab-ce-add-svg-optimizer.tar.gz |
Test svg optimizeradd-svg-optimizer
Diffstat (limited to 'scripts/check_svg.js')
-rw-r--r-- | scripts/check_svg.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/scripts/check_svg.js b/scripts/check_svg.js new file mode 100644 index 00000000000..ebc0bd574a4 --- /dev/null +++ b/scripts/check_svg.js @@ -0,0 +1,57 @@ +const glob = require('glob'); +const fs = require('fs'); +const SVGOptimizer = require('svgo'); + +// node check_svg.js [failIfUncompressedSVGFound = true | false] +const failFlag = process.argv[2]; +const failIfUncompressedSVGFound = failFlag !== undefined ? JSON.parse(failFlag) : true; + +const svgo = new SVGOptimizer(); +const globPath = 'app/views/shared/icons/*.svg'; +const globOptions = { mark: true }; + +function error(err) { + console.error(err); + process.exit(1); +} + +function saveOptimization(options) { + const { filepath, data, originalSize, failIfUncompressed } = options; + + fs.writeFile(filepath, data, (err) => { + if (err) { + return error(err); + } + + const compressedSize = fs.statSync(filepath).size; + const wasUncompressed = compressedSize < originalSize; + const compression = (100 - ((compressedSize / originalSize) * 100)).toFixed(2); + + if (failIfUncompressed && wasUncompressed) { + error(`${filepath} was found to be uncompressed - could be compressed by ${compression}%`); + } else { + console.log(`${filepath} was compressed by ${compression}%`); + } + }); +} + +function optimize(filepath) { + fs.readFile(filepath, 'utf8', (err, data) => { + if (err) { + return error(err); + } + + svgo.optimize(data, (result) => { + saveOptimization({ + filepath, + data: result.data, + originalSize: fs.statSync(filepath).size, + failIfUncompressed: failIfUncompressedSVGFound, + }); + }); + }); +} + +glob(globPath, globOptions, (er, files) => { + files.forEach(filepath => optimize(filepath)); +}); |