Created
May 28, 2017 21:24
-
-
Save souporserious/4d1f365ba7e700f631c165095aa9c496 to your computer and use it in GitHub Desktop.
Sketch to svg json loader
This file contains hidden or 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 { readdir, readFile } = require('fs') | |
const { resolve } = require('path') | |
const { getOptions } = require('loader-utils') | |
const SketchTool = require('sketch-tool') | |
const svgson = require('svgson') | |
const del = require('del') | |
module.exports = function(content) { | |
this.cacheable && this.cacheable(true) | |
const callback = this.async() | |
const query = getOptions(this) || {} | |
const svgsDir = resolve(__dirname, '../.svgs') | |
const Sketch = new SketchTool({ | |
file: this.resourcePath, | |
}) | |
this.addDependency(this.resourcePath) | |
this.addDependency(svgsDir) | |
Sketch.exportCall('artboards', { | |
formats: 'svg', | |
output: svgsDir, | |
}).then(() => { | |
const jsonData = {} | |
function readJsonData() { | |
callback(null, 'cool') | |
del(svgsDir) | |
} | |
readdir(svgsDir, 'utf8', (err, files) => { | |
if (err) { | |
callback(null, err) | |
} | |
files.forEach((file, index) => { | |
const svgFile = `${svgsDir}/${file}` | |
readFile(svgFile, 'utf8', (err, data) => { | |
if (err) { | |
callback(null, err) | |
} | |
svgson(data, { svgo: true, pretty: true }, result => { | |
jsonData[file.replace('.svg', '')] = JSON.stringify(result) | |
if (index === files.length - 1) { | |
readJsonData() | |
} | |
}) | |
}) | |
}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment