Skip to content

Instantly share code, notes, and snippets.

@Hirosaji
Created December 24, 2019 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hirosaji/2320f20aa92580cc1fb25df0b128f46b to your computer and use it in GitHub Desktop.
Save Hirosaji/2320f20aa92580cc1fb25df0b128f46b to your computer and use it in GitHub Desktop.
read all svg file name in current dir -> edit svg text -> export
const fs = require('fs');
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
const dirPath = require('path').dirname(require.main.filename);
const mkdirExceptExist = (dirPath) => {
if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath); }
};
mkdirExceptExist("output");
fs.readdir('.', function(err, files){
if (err) throw err;
var fileList = files.filter(function(file){
return fs.statSync(file).isFile() && /.*\.svg$/.test(file); // svgのみに絞り込み
})
fileList.forEach(function(path) {
const text = readTextFile(path);
const regex = /viewBox="0 0 *(.*?)">*/;
const matched = regex.exec(text);
const wh = matched[1].split(' ');
const output = text.replace('viewBox', 'width="' + wh[0] + '" height="' + wh[1] + '" viewBox');
fs.writeFileSync(dirPath + '/output/' + path, output);
});
});
function readTextFile(file) {
const pathParam = 'file://' + dirPath + '/' + file;
const rawFile = new XMLHttpRequest();
rawFile.open('GET', pathParam, false);
rawFile.send(null);
return rawFile.responseText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment