forked from stevage/figmasset-export
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exportAssets.js
61 lines (59 loc) · 2.06 KB
/
exportAssets.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
54
55
56
57
58
59
60
61
const { getFigmaIconsByFrames } = require('figmasset');
const fetch = require('node-fetch');
const Downloader = require('nodejs-file-downloader');
const fs = require('fs');
async function download({ figmaOptions, downloadDir }) {
const results = await getFigmaIconsByFrames(figmaOptions);
const assetsByScale = figmaOptions.scales.reduce(
(byScale, scale) => ({ ...byScale, [scale]: [] }),
{}
);
await Promise.all(
Object.keys(results).flatMap((assetKey) => {
const asset = results[assetKey];
return figmaOptions.scales.map((scale) => {
assetsByScale[scale].push({
id: assetKey,
fileName: `${assetKey}.${figmaOptions.format}`,
scale: +scale,
// TODO width/height etc
});
return new Downloader({
url: asset[`@${scale}x`],
directory: downloadDir + `@${scale}x`,
fileName: `${assetKey}.${figmaOptions.format}`,
cloneFiles: false,
}).download();
});
})
);
for (const scale of figmaOptions.scales) {
const filename = downloadDir + `@${scale}x/assets.json`;
fs.writeFileSync(
filename,
JSON.stringify(assetsByScale[scale], null, 2)
);
console.log(
`Wrote ${filename}: ${assetsByScale[scale].map((a) => a.id)}`
);
}
return results;
}
async function runExport(argv) {
const options = {
figmaOptions: {
fetchFunc: fetch,
personalAccessToken: argv.token,
fileKey: argv.file,
scales: argv.scale,
frameIds: argv.frame.filter((id) => id.match(/^\d+:\d+$/)),
frameNames: argv.frame.filter((id) => !id.match(/^\d+:\d+$/)),
format: argv.format,
},
downloadDir: argv.out,
};
const results = await download(options);
// console.log(results);
console.log('Done!');
}
module.exports = runExport;