Skip to content

Commit

Permalink
Update svgo to the latest version 🚀 (#1473)
Browse files Browse the repository at this point in the history
* fix(package): update svgo to version 1.0.5
* Update package-lock
* Update invocation for SVGO 1.x
* Remove helper
  • Loading branch information
greenkeeper[bot] authored and paulmelnikow committed Apr 2, 2018
1 parent f9b0aed commit 724abd0
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 77 deletions.
67 changes: 33 additions & 34 deletions lib/make-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const badgeKeyWidthCache = new LruCache(1000);
const templates = {};
const templateFiles = fs.readdirSync(path.join(__dirname, '..', 'templates'));
dot.templateSettings.strip = false; // Do not strip whitespace.
templateFiles.forEach(function(filename) {
templateFiles.forEach(async function(filename) {
if (filename[0] === '.') { return; }
const templateData = fs.readFileSync(
path.join(__dirname, '..', 'templates', filename)).toString();
Expand All @@ -32,37 +32,41 @@ templateFiles.forEach(function(filename) {
mapping.set(mapKey, match);
return mapKey;
});
compressSvg(untemplatedSvg, function(object) {
if (object.error !== undefined) {
console.error(`Template ${filename}: ${object.error}\n` +
' Generated untemplated SVG:\n' +
`---\n${untemplatedSvg}---\n`);
return;
}
// Substitute dot code back.
let svg = object.data;
const unmappedKeys = [];
mapping.forEach(function(value, key) {
let keySubstituted = false;
svg = svg.replace(RegExp(key, 'g'), function() {
keySubstituted = true;
return value;
});
if (!keySubstituted) {
unmappedKeys.push(key);
}

const svgo = new SVGO();
const { data, error } = await svgo.optimize(untemplatedSvg);

if (error !== undefined) {
console.error(`Template ${filename}: ${error}\n` +
' Generated untemplated SVG:\n' +
`---\n${untemplatedSvg}---\n`);
return;
}

// Substitute dot code back.
let svg = data;
const unmappedKeys = [];
mapping.forEach(function(value, key) {
let keySubstituted = false;
svg = svg.replace(RegExp(key, 'g'), function() {
keySubstituted = true;
return value;
});
if (unmappedKeys.length > 0) {
console.error(`Template ${filename} has unmapped keys ` +
`${unmappedKeys.join(', ')}.\n` +
' Generated untemplated SVG:\n' +
`---\n${untemplatedSvg}\n---\n` +
' Generated template:\n' +
`---\n${svg}\n---\n`);
return;
if (!keySubstituted) {
unmappedKeys.push(key);
}
templates[style + '-' + extension] = dot.template(svg);
});
if (unmappedKeys.length > 0) {
console.error(`Template ${filename} has unmapped keys ` +
`${unmappedKeys.join(', ')}.\n` +
' Generated untemplated SVG:\n' +
`---\n${untemplatedSvg}\n---\n` +
' Generated template:\n' +
`---\n${svg}\n---\n`);
return;
}

templates[style + '-' + extension] = dot.template(svg);
}
});

Expand All @@ -84,11 +88,6 @@ function capitalize(s) {

const definedColorschemes = require(path.join(__dirname, 'colorscheme.json'));

function compressSvg(string, callback) {
const svgo = new SVGO();
svgo.optimize(string, callback);
}

const cssColor = /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/;

// Inject the measurer to avoid placing any persistent state in this module.
Expand Down
Loading

0 comments on commit 724abd0

Please sign in to comment.