-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
156 lines (132 loc) · 5.39 KB
/
convert.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
const useFunctionalVarNames = require('yargs').argv.functional;
const fs = require('fs-extra');
const json2scss = require('json2scss');
const path = require('path');
const functionalColorMap = [
{asIs: "ansi-0-color", functional: "black-normal"},
{asIs: "ansi-1-color", functional: "red-normal"},
{asIs: "ansi-2-color", functional: "green-normal"},
{asIs: "ansi-3-color", functional: "yellow-normal"},
{asIs: "ansi-4-color", functional: "blue-normal"},
{asIs: "ansi-5-color", functional: "magenta-normal"},
{asIs: "ansi-6-color", functional: "cyan-normal"},
{asIs: "ansi-7-color", functional: "white-normal"},
{asIs: "ansi-8-color", functional: "black-bright"},
{asIs: "ansi-9-color", functional: "red-bright"},
{asIs: "ansi-10-color", functional: "green-bright"},
{asIs: "ansi-11-color", functional: "yellow-bright"},
{asIs: "ansi-12-color", functional: "blue-bright"},
{asIs: "ansi-13-color", functional: "magenta-bright"},
{asIs: "ansi-14-color", functional: "cyan-bright"},
{asIs: "ansi-15-color", functional: "white-bright"}
];
var parse = function (file) {
const saxStream = require("sax").createStream(true, {}); // strict = true
const rgbToHex = require('rgb-to-hex');
var colors = {};
var colorStarted;
var colorId;
var colorBlue;
var colorGreen;
var colorRed;
var blue;
var green;
var red;
var blueDone;
var greenDone;
var redDone;
return new Promise(function (resolve, reject) {
saxStream.on("opentag", function (node) {
if (node.name == "key" && !colorStarted) {
colorStarted = true;
}
if (node.name == "dict" && colorStarted) {
blue = false;
green = false;
red = false;
blueDone = false;
greenDone = false;
redDone = false;
}
});
saxStream.on("closetag", function (node) {
if (node == "dict" && colorStarted) {
colorStarted = false;
}
});
saxStream.on("text", function (text) {
if (blueDone && greenDone && redDone) {
colors[colorId] = "#" + rgbToHex("rgb(" + colorRed + "," + colorGreen + "," + colorBlue + ")");
}
if (text.match(/^[0-9]/)) {
if (blue && !blueDone) {
colorBlue = Math.round(text * 255);
blueDone = true;
}
if (green && !greenDone) {
colorGreen = Math.round(text * 255);
greenDone = true;
}
if (red && !redDone) {
colorRed = Math.round(text * 255);
redDone = true;
}
return;
}
if (text.match(/^[BGR]/)) {
switch (text) {
case "Blue Component":
blue = true;
break;
case "Green Component":
green = true;
break;
case "Red Component":
red = true;
break;
}
}
if (text.indexOf("Color") > -1) {
colorId = text.replace(/ /g, "-");
colorId = colorId.toLowerCase();
colors[colorId] = {};
}
});
saxStream.on("end", function () {
var distFolder = path.join(__dirname, "dist/abstract");
var jsonString = JSON.stringify(colors, null, 2); // Pretty print Json
if (useFunctionalVarNames) {
functionalColorMap.forEach(function(map){
jsonString = jsonString.replace(map.asIs, map.functional);
});
distFolder = path.join(__dirname, "dist/functional");
}
fs.ensureDirSync(path.join(distFolder, "json"));
fs.ensureDirSync(path.join(distFolder, "scss"));
fs.ensureDirSync(path.join(distFolder, "less"));
fs.ensureDirSync(path.join(distFolder, "sass"));
fs.ensureDirSync(path.join(distFolder, "stylus"));
// Generate JSON
fs.writeFileSync(path.join(distFolder, "json") + '/' + file + '.json', jsonString);
var scss = json2scss(require(path.join(distFolder, "json") + '/' + file + '.json'));
var sass = scss.replace(/;/g,"");
var stylus = sass.replace(/:/g,"");
var less = scss.replace(/\$/g,"@");
fs.writeFileSync(path.join(distFolder, "scss") + '/' + file + '.scss', scss);
fs.writeFileSync(path.join(distFolder, "sass") + '/' + file + '.sass', sass);
fs.writeFileSync(path.join(distFolder, "stylus") + '/' + file + '.styl', stylus);
fs.writeFileSync(path.join(distFolder, "less") + '/' + file + '.less', less);
resolve();
});
fs.createReadStream(path.join(`./node_modules/iterm2-color-schemes/schemes`, file + '.itermcolors'))
.pipe(saxStream);
});
};
fs.readdir(`./node_modules/iterm2-color-schemes/schemes`, function (err, files) {
if (err) return;
files.forEach(function (f) {
var fileBaseName = path.basename(f, '.itermcolors');
console.log('Parsing ' + fileBaseName);
parse(fileBaseName);
});
});