-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
180 lines (156 loc) · 5.1 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const { writeFileSync, mkdirSync, readFileSync } = require('fs');
const fetch = require('node-fetch');
const TOKEN = process.env.FIGMA_API_TOKEN;
const FILE_KEY = '5dEFiujMXZX0nKyde7nZ6n';
const isDefined = (node) => node != null;
const isLeaf = (node) => isDefined(node) && !('children' in node);
const isEllipse = (node) => isDefined(node) && node.type === 'ELLIPSE';
const fetchFigmaFile = (key) => {
return fetch(
`https://api.figma.com/v1/files/${key}`,
{ headers: { 'X-Figma-Token': TOKEN } }
)
.then(response => response.json());
}
const findStyleInTree = (root, styleId) => {
if(isLeaf(root)) {
return isEllipse(root) && root.styles && root.styles.fill === styleId
? root
: undefined;
} else {
return root.children
.map((item) => findStyleInTree(item, styleId))
.reduce(
(accumulator, current) => isDefined(accumulator)
? accumulator
: current, // we keep the first children that uses the color
undefined
);
}
};
const getStylesFromFile = ({styles}) => Object.entries(styles)
.filter(([, {styleType}]) => styleType === 'FILL')
.map(([id, {name}]) => ({name, id}));
const mapStyleToNode = (file, styles) => styles
.map(({name, id}) => {
const node = findStyleInTree(file.document, id);
const color = isEllipse(node) && node.fills[0]
? node.fills[0].color
: undefined;
return {name, color};
})
.filter(({color}) => isDefined(color)); // remove all not used styles
const getStyleColors = (file) => Promise.resolve(file)
.then(getStylesFromFile)
.then(styles => mapStyleToNode(file, styles));
const toHex = (value) => Math.round(value * 255);
const formatColor = ({r: red, g: green, b: blue, a: alpha}) => {
return alpha !== 1
? `rgba(${toHex(red)}, ${toHex(green)}, ${toHex(blue)}, ${toHex(alpha)})`
: `rgb(${toHex(red)}, ${toHex(green)}, ${toHex(blue)})`;
}
const formatName = (name) => name
.toUpperCase()
.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // removes diacritics
.replace(/\//g, '_') // replaces '/' by '_'
.replace(/[^a-zA-Z0-9_]/g, '') // removes non alphanumeric or '_' characters
const NEW_LINE = `
`;
const templateSCSS = (styles) => {
return styles
.map(({name, color}) => `$${formatName(name)}: ${formatColor(color)};`)
.join(NEW_LINE);
}
const templateTS = (styles) => {
return styles
.map(({name, color}) => `const ${formatName(name)} = '${formatColor(color)}';`)
.join(NEW_LINE);
}
const templateJSON = (styles) => {
return `{${NEW_LINE}${styles
.map(({name, color}) => ` "${formatName(name)}": "${formatColor(color)}"`)
.join(`,${NEW_LINE}`)
}${NEW_LINE}}`;
}
const createDir = (path) => {
try{
mkdirSync(path);
}catch(e){
if(e.code !== 'EEXIST'){ // we don't mind if the folder already exists
throw e;
}
}
};
const readFile = (path) => readFileSync(path).toString('utf-8');
const generateFiles = (styles) => {
createDir('./build');
writeFileSync('./build/colors.scss', templateSCSS(styles));
writeFileSync('./build/colors.ts', templateTS(styles));
writeFileSync('./build/colors.json', templateJSON(styles));
}
const getState = () => Promise.resolve()
.then(() => readFile('./build/colors.json'))
.then(fileContent => ({
state: 'RETRIEVED',
data: JSON.parse(fileContent),
}))
.catch(e => e.code === 'ENOENT'
? Promise.resolve({ // the script has not been run yet
state: 'EMPTY'
})
: Promise.reject(e)
);
const getAddedData = (lastData, newData) => Object
.entries(newData)
.filter(([name]) => !lastData[name])
.map(([name]) => name);
const getDeletedData = (lastData, newData) => Object
.entries(lastData)
.filter(([name]) => !newData[name])
.map(([name]) => name);
const getUpdatedData = (lastData, newData) => Object
.entries(newData)
.filter(([name, color]) => lastData[name] !== color)
.map(([name]) => name);
const interpretChanges = (lastState, newState ) => {
if(lastState.state === 'EMPTY'){
return {
added: newState.data,
updated: [],
deleted: [],
}
}
return {
added: getAddedData(lastState.data, newState.data),
updated: getUpdatedData(lastState.data, newState.data),
deleted: getDeletedData(lastState.data, newState.data),
}
};
const getVersionType = (changes) => {
if(changes.deleted.length > 0){
return 'MAJOR';
}
if(changes.added.length > 0 || changes.updated.length > 0){
return 'MINOR';
}
return '';
}
const createVersionBumpType = (lastState) => Promise.resolve()
.then(getState)
.then(newState => interpretChanges(lastState, newState))
.then(getVersionType)
.then((versionType) => writeFileSync('./VERSION_BUMP_TYPE', versionType))
async function run (){
if(!TOKEN){
console.error('The Figma API token is not defined, you need to set an environment variable `FIGMA_API_TOKEN` to run the script');
return;
}
const lastState = await getState();
fetchFigmaFile(FILE_KEY)
.then(getStyleColors)
.then(generateFiles)
.then(() => createVersionBumpType(lastState))
.then(() => console.log('Done'))
.catch((error) => console.error('Oops something went wrong: ', error));
}
run();