forked from boltdesignsystem/node-sass-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (64 loc) · 1.99 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
'use strict';
(function (module) {
var _ = require('lodash');
var fs = require('fs-extra');
var exporter = module.exports = function (path, name) {
var out = {};
out[exporter.interface(name)] = exporter.function(path);
return out;
};
exporter.get_value = function get_value(a) {
var value, i;
switch (a.constructor.name) {
case 'SassList':
value = [];
for (i = 0; i < a.getLength(); i++) {
value.push(get_value(a.getValue(i)));
}
break;
case 'SassMap':
value = {};
for (i = 0; i < a.getLength(); i++) {
value[a.getKey(i).getValue()] = get_value(a.getValue(i));
}
break;
case 'SassColor':
if (a.getA() === 1) {
value = `rgb(${Math.round(a.getR())}, ${Math.round(a.getG())}, ${Math.round(a.getB())})`;
} else {
value = `rgba(${Math.round(a.getR())}, ${Math.round(a.getG())}, ${Math.round(a.getB())}, ${a.getA()})`;
}
break;
case 'SassNumber':
value = a.getValue();
if (a.getUnit()) {
value += a.getUnit();
}
break;
default:
value = a.getValue();
}
return value;
};
exporter.function = function (path) {
return function (file, value, options) {
var opt = _.defaults(exporter.get_value(options), {prefix: '', suffix: '', extend: false});
var output = exporter.get_value(value);
if (opt.extend && 'SassMap' === value.constructor.name) {
try {
_.defaults(output, JSON.parse(fs.readFileSync(path + '/' + file.getValue())));
}
catch (e) {
console.log(e);
}
}
fs.mkdirp(path); //Create folder if it doesn't exist.
fs.writeFileSync(path + '/' + file.getValue(), opt.prefix + JSON.stringify(output, null, ' ') + opt.suffix);
return value;
}
};
exporter.interface = function (name) {
name = name || 'export';
return name + '($file, $value, $options:())';
};
})(module);