Created
December 24, 2019 09:20
-
-
Save Hirosaji/2320f20aa92580cc1fb25df0b128f46b to your computer and use it in GitHub Desktop.
read all svg file name in current dir -> edit svg text -> export
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
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