-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
81 lines (76 loc) · 2.35 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
'use strict';
(function (module) {
const fs = require('fs');
module.exports = function (path, name) {
const exports = {};
exports[declaration(name)] = implementation(path);
return exports;
};
function toHex(c) {
const hex = Math.round(c).toString(16).toUpperCase();
return hex.length === 1 ? "0" + hex : hex;
}
function get_value(value, options) {
let output;
switch (value.constructor.name) {
case 'SassList':
output = [];
for (let i = 0; i < value.getLength(); i++) {
output.push(get_value(value.getValue(i), options));
}
break;
case 'SassMap':
output = {};
for (let i = 0; i < value.getLength(); i++) {
output[value.getKey(i).getValue()] = get_value(value.getValue(i), options);
}
break;
case 'SassColor':
if (1 === value.getA()) {
if (options.hex_color) {
output = '#' + toHex(value.getR()) + toHex(value.getG()) + toHex(value.getB());
}
else {
output = 'rgb(' + Math.round(value.getR()) + ', ' + Math.round(value.getG()) + ', ' + Math.round(value.getB()) + ')';
}
}
else {
output = 'rgba(' + Math.round(value.getR()) + ', ' + Math.round(value.getG()) + ', ' + Math.round(value.getB()) + ', ' + value.getA() + ')';
}
break;
case 'SassNumber':
output = value.getValue();
if (value.getUnit()) {
output += value.getUnit();
}
break;
default:
output = value.getValue();
}
return output;
}
function implementation(path) {
return (file, value, options) => {
const opt = Object.assign({
prefix: '',
suffix: '',
extend: false,
hex_color: false
}, get_value(options));
let output = get_value(value, opt);
if (opt.extend && 'SassMap' === value.constructor.name) {
try {
output = Object.assign({}, JSON.parse(fs.readFileSync(path + '/' + file.getValue())), output);
}
catch (e) {
console.log(e);
}
}
fs.writeFileSync(path + '/' + file.getValue(), opt.prefix + JSON.stringify(output, null, ' ') + opt.suffix);
return value;
}
}
function declaration(name) {
return (name || 'export') + '($file, $value, $options:())';
}
})(module);