forked from jgthms/bulma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
213 lines (177 loc) · 5.59 KB
/
Copy pathutils.js
File metadata and controls
213 lines (177 loc) · 5.59 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const fs = require('fs');
const path = require('path');
const base_path = '../_data/variables/';
const css_keywords = ['null', 'ease-out', 'optimizeLegibility'];
const regex_unitless = /^([0-9]+\.[0-9]+)$/;
let utils = {
getVariableType: (name, value) => {
if (!value) {
return 'string';
}
if (name == '$sizes') {
return 'map';
}
if (
value.startsWith('hsl') ||
value.startsWith('#') ||
value.startsWith('rgb')
) {
return 'color';
} else if (css_keywords.includes(value)) {
return 'keyword';
} else if (value.startsWith('mergeColorMaps')) {
return 'map';
} else if (value.startsWith('findColorInvert')) {
return 'function';
} else if (value.startsWith('$')) {
return 'variable';
} else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') {
return 'font-family';
} else if (value == 'true' || value == 'false') {
return 'boolean';
} else if (name.endsWith('shadow')) {
return 'shadow';
} else if (value.endsWith('ms')) {
return 'duration';
} else if (value.includes('+')) {
return 'computed';
} else if (value.endsWith('00') || value == 'normal') {
return 'font-weight';
} else if (
value.endsWith('px') ||
value.endsWith('em') ||
value.includes('px ') ||
value.includes('em ')
) {
return 'size';
} else if (value.includes('$')) {
return 'compound';
} else if (value.match(regex_unitless)) {
return 'unitless';
}
return 'string';
},
parseLine: (line) => {
if (line.startsWith('$') && line.endsWith('!default')) {
const colon_index = line.indexOf(':');
const variable_name = line.substring(0, colon_index).trim();
const default_index = line.indexOf('!default');
let variable_value = line
.substring(colon_index + 1, default_index)
.trim();
return (variable = {
name: variable_name,
value: variable_value,
type: utils.getVariableType(variable_name, variable_value),
});
}
return false;
},
getLines: (files, file_path) => {
const file = files[file_path];
const slash_index = file_path.lastIndexOf('/');
const dot_index = file_path.lastIndexOf('.');
const file_name = file_path.substring(slash_index + 1, dot_index);
return {
file_name,
lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
};
},
writeFile: (file_path, data) => {
const json_data = JSON.stringify(data, null, ' ');
const json_file_path = base_path + file_path.replace('.sass', '.json');
utils.ensureDirectoryExistence(json_file_path);
fs.writeFile(json_file_path, json_data, (err) => {
if (err) {
return console.log(err);
}
console.log(`The file ${json_file_path} was saved!`);
});
},
getInitialValue: (value, type, initial_variables) => {
if (value.startsWith('$') && value in initial_variables.by_name) {
const second_value = initial_variables.by_name[value].value;
if (
second_value.startsWith('$') &&
second_value in initial_variables.by_name
) {
const third_value = initial_variables.by_name[second_value].value;
return third_value;
}
return second_value;
}
return value;
},
getComputedData: (
name,
value,
type,
initial_variables,
derived_variables = {}
) => {
let computed_value = '';
if (value.startsWith('$')) {
let second_value;
if (value in initial_variables.by_name) {
second_value = initial_variables.by_name[value].value;
} else if (
derived_variables.by_name &&
value in derived_variables.by_name
) {
second_value = derived_variables.by_name[value].value;
}
if (second_value && second_value.startsWith('$')) {
let third_value;
if (second_value in initial_variables.by_name) {
third_value = initial_variables.by_name[second_value].value;
} else if (
derived_variables.by_name &&
second_value in derived_variables.by_name
) {
third_value = derived_variables.by_name[second_value].value;
}
computed_value = third_value;
} else {
computed_value = second_value;
}
} else if (value.startsWith('findColorInvert')) {
// e.g. $turquoise-invert -> findColorInvert($turquoise)
if (value.endsWith('($yellow)')) {
computed_value = 'rgba(0, 0, 0, 0.7)';
} else {
computed_value = '#fff';
}
}
if (computed_value && computed_value != '') {
// e.g. $primary-invert -> $turquoise-invert -> findColorInvert($turquoise)
if (computed_value.startsWith('findColorInvert')) {
if (computed_value.endsWith('($yellow)')) {
computed_value = 'rgba(0, 0, 0, 0.7)';
} else {
computed_value = '#fff';
}
}
const computed_type = utils.getVariableType(name, computed_value);
if (computed_value.startsWith('mergeColorMaps')) {
computed_value = 'Bulma colors';
}
return {
computed_type,
computed_value,
};
}
return {};
},
ensureDirectoryExistence: (file_path) => {
var dirname = path.dirname(file_path);
if (fs.existsSync(dirname)) {
return true;
}
utils.ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
},
};
utils.files = {};
utils.files.initial_variables = `${base_path}utilities/initial-variables.json`;
utils.files.derived_variables = `${base_path}utilities/derived-variables.json`;
module.exports = utils;