Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.readdirSync(SVG_DIR).map(async file => {
console.log(`Converting ${file}...`)
if (!ICON_PATTERN.test(file)) {
return
}
let fileData = fs.readFileSync(path.resolve(SVG_DIR, file), 'utf8')
let { error, data } = await svgo.optimize(fileData)
if (error) {
console.error(file, error)
return
}
let { attributes, children } = await svgson(data)
let { width, height, viewBox } = attributes
if (!(width && height)) {
if (!viewBox) {
console.error(file, `doesn't contain a valid size declaration.`)
}
[width, height] = (viewBox.match(/0 0 (\d+) (\d+)/) || []).map(size => parseInt(size, 10))
}
if (!(width && height)) {
console.error(file, `doesn't contain a valid size declaration.`)
}
let icon
let paths
if (children.every(({ name }) => name === 'path')) {
let [match, name] = file.match(ICON_PATTERN) || []
if (!match) {
return
}
if (!ICON_PATTERN.test(file)) {
return
}
let fileData = fs.readFileSync(path.resolve(SVG_DIR, file), 'utf8')
let { error, data } = await svgo.optimize(fileData)
if (error) {
console.error(file, error)
return
}
let el = await svgson(data)
console.log(`Converting ${file}...`)
let { attributes, children } = el
let { width, height, viewBox } = attributes
if (!(width && height)) {
if (!viewBox) {
console.error(file, `doesn't contain a valid size declaration.`)
console.error(width, height, viewBox)
}
;[, width, height] = (viewBox.match(/0 0 (\d+) (\d+)/) || []).map(size =>
parseInt(size, 10)
)
}
if (!(width && height)) {
console.error(file, `doesn't contain a valid size declaration.`)
export default async svg => {
const parsed = await svgson(svg)
const convertedToPath = elemToPath(parsed)
return stringify(convertedToPath)
}