-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·88 lines (69 loc) · 2.42 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require('dotenv').config();
const { preprocessCanvasComponents, createComponents, generateComponent } = require('./lib');
const { loadCanvas, loadNodes, loadVectors, loadRefImages, loadNodeImages, getHeaders } = require('./api');
const presets = require('./presets');
function getConfig(options = {}) {
let fileKey = options.fileKey || process.argv[2] || process.env.FIGMA_FILE_KEY;
let devToken = options.devToken || process.argv[3] || process.env.FIGMA_DEV_TOKEN;
let presetName = process.argv[4] || process.env.FIGMA_PRESET || 'default';
if (!fileKey || !devToken) {
console.log(
'Usage: figma-react <file-key> [figma-dev-token] <preset-name> or use env.FIGMA_FILE_KEY, env.FIGMA_DEV_TOKEN, process.env.FIGMA_PRESET'
);
process.exit(0);
}
Object.assign({}, presets[presetName], options);
options.dir = options.dir || process.env.FIGMA_DIR;
options.makeDir = options.makeDir == null ? !!process.env.FIGMA_MAKE_DIR : options.makeDir;
const headers = getHeaders(devToken);
return {
headers,
fileKey
};
}
async function runFigmaReact(options = {}) {
const { headers, fileKey } = getConfig(options);
// Create shared objects
const vectorMap = {};
const imageMap = {};
const componentMap = {};
const componentDescriptionMap = {};
const shared = {
componentMap,
componentDescriptionMap,
vectorMap,
imageMap,
options,
fileKey,
headers
};
// Load the document from Figma
const canvas = await loadCanvas(fileKey, headers);
// Debug
// const fs = require('fs');
// fs.writeFileSync('./temp.json', JSON.stringify(canvas, null, 4));
// Wrap vectors and images
preprocessCanvasComponents(canvas, shared);
// Load component description
const nodes = await loadNodes(Object.keys(componentDescriptionMap), fileKey, headers);
for (const id in nodes) {
componentDescriptionMap[id] = nodes[id].components[id].description;
}
// Load all images used in the document from Figma
shared.vectors = await loadVectors(shared);
shared.refImages = await loadRefImages(shared);
shared.images = await loadNodeImages(shared);
// Create components
await createComponents(canvas, shared);
// Generate components
for (const key in componentMap) {
await generateComponent(componentMap[key], options);
}
}
module.exports = {
runFigmaReact,
...require('./lib'),
...require('./api'),
...require('./content.plugins'),
...require('./style.plugins')
};