-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload-sketch-file.js
53 lines (51 loc) · 1.37 KB
/
load-sketch-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fs = require('fs')
const path = require('path')
const util = require('util')
const JSZip = require('jszip')
const readFile = util.promisify(fs.readFile)
module.exports = function loadFile(filePath) {
const name = path.basename(filePath).replace('.sketch', '')
return readFile(filePath)
.then(data => JSZip.loadAsync(data))
.then(zip =>
zip
.file('document.json')
.async('string')
.then(data => ({
document: JSON.parse(data),
zip,
}))
)
.then(({ zip, document }) => {
const pages = {}
const images = {}
return Promise.all(
Object.keys(zip.files).reduce((promises, k) => {
if (k.indexOf('pages/') === 0 && k !== 'pages/') {
promises.push(
zip
.file(k)
.async('string')
.then(data => {
pages[k.replace(/\.json$/, '')] = JSON.parse(data)
})
)
} else if (k.indexOf('images/') === 0 && k !== 'images/') {
promises.push(
Promise.resolve().then(() => {
images[k] = zip.file(k)
})
)
}
return promises
}, [])
).then(() => ({
document,
filePath,
name,
pages,
images,
id: document.do_objectID,
}))
})
}