UNPKG

@fws/cli

Version:

This CLI is work in progress and it's meant to work together with other Forwardslash boilerplates. Please do not use it if you don't have access to other stuff.

64 lines (56 loc) 2.07 kB
/** * SVG Handle Files * * @description Helper functions for handling SVG files. */ const fs = require('fs'); const path = require('path'); const fancyLog = require('fancy-log'); const colors = require('ansi-colors'); const helpers = require('../helpers'); const SVGO = require('./svg-omg'); module.exports = { renameSvgFiles: function (file, filePath, allFiles, svgDirPath) { return new Promise((resolve) => { helpers.rf(filePath, () => { let newFile; if (file.substring(0, 4) !== 'ico-') { newFile = file.toLowerCase(); newFile = newFile.toLowerCase(); newFile = newFile.replace('.svg', '').replace(/-/g, ' ').replace(/[^\w\s]/gi, '').replace(/ /g, '-'); newFile = `ico-${newFile}.svg`; if (allFiles.includes(newFile)) { // delete file if already exists try { fs.unlinkSync(filePath); helpers.consoleLogWarning(`deleted '${filePath}' as file with same name already exists`, 'red'); } catch (err) { fancyLog(colors.red(err)); } } else { // rename file fs.renameSync(filePath, path.join(svgDirPath, newFile)); } } else { newFile = file; } resolve(newFile); }); }); }, optimizeSvgFiles: function (filePath) { return new Promise((resolve) => { helpers.rf(filePath, (data) => { resolve(SVGO.optimize(data, {path: filePath})); }); }); }, saveSvgFiles: function (filepath, data) { return new Promise((resolve) => { fs.writeFile(filepath, data, err => { if (err) throw err; resolve(filepath); }); }); } };