-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·231 lines (176 loc) · 5.08 KB
/
build.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var fs = require('fs');
var yaml = require('js-yaml');
var color = require('onecolor');
var async = require('async');
const __src = 'src/';
function _template(path, name, cb) {
function templateEngine(tpl, data) {
var re = /<<([^>]+)>>/g;
var hex = /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/g;
var ansi = '$1/$2/$3';
while(match = re.exec(tpl)) {
// TODO change eval for a vm
var repl = eval("data." + match[1]);
// console.log(match[0], " => ", repl);
tpl = tpl.replace(match[0], repl)
}
return tpl;
}
var file = fs.readFileSync(path + '/' + name);
var content = templateEngine('' + file, context);
fs.writeFileSync(path + '/' + __src + name, content);
cb(null);
}
function _less(path, name, dest, cb) {
var file = '' + fs.readFileSync(path + '/' + name);
if (!cb) {
cb = dest;
var re = /.less$/;
dest = name.replace(re.exec(name), ".css");
}
require('less').render(file, {
paths: [path + '/'], // Specify search paths for @import directives
filename: name // Specify a filename, for better error messages
}, function (e, tree) {
if (e) {
console.error('\x1B[31m>>\x1B[39m ' + e.message);
for (var i = e.extract.length - 1; i >= 0; i--) {
console.error(' \x1B[31m' + e.extract[i] + '\x1B[39m');
};
} else {
var css = tree.toCSS({
// Minify CSS output
compress: true
});
fs.writeFileSync(path + '/' + dest, css);
cb(null, "less done"); // TODO unsync the writeFile.
}
})
}
function run_cmd(cmd, args, cb, end) {
args = args || [];
// console.log(" > ", cmd, args.join(' '));
var child = require('child_process').spawn(cmd, args);
child.stdout.on('data', cb || function(buffer) {console.log('' + buffer)});
child.stdout.on('end', end || function() {console.log('done')});
child.stderr.on('data', function(buffer) {console.error('' + buffer)});
}
function _install(src, dest, cb) {
src.unshift('-avzh');
src.unshift('rsync');
src.push(dest);
var buffer = '';
// console.log(src.join(' '));
new run_cmd('sudo', src, // TODO the sudo shit, run sudo rsync, instead of rsync because of privileges in the destination folder : /usr/share ...
function(buf){
buffer += ' ' + buf;
},
function() {
cb(null, "install done")
})
}
function sh(src) {
new run_cmd(src)
}
function build(theme) {
var _tasks = {};
_tasks.template = function(src, cb) {
if (src instanceof Array) {
async.every(src, function(_src, cb) {
_template(theme, _src, cb);
}, function() {
cb(null, "template done");
})
} else {
_template(theme, src, function() {
cb(null, "template done");
});
}
}
_tasks.less = function(src, cb) {
if (src) {
if (src instanceof Array) {
async.every(src, function(src, cb) {
_less(theme + '/' + __src, src, cb);
}, cb)
} else {
_less(theme + '/' + __src, src, cb);
}
}
}
_tasks.run = function(src, cb) {
new run_cmd('./' + theme + '/' + __src + src, [],
function(buf) {
// console.log('' + buf);
},
function() {
cb(null, "run done");
})
}
_tasks.install = function(files, cb) {
if (files) {
if (files.src && files.dest) {
var _files = [];
if(files.src instanceof Array)
for (var i = files.src.length - 1; i >= 0; i--) {
_files.push(theme + '/' + __src + files.src[i]);
}
else
_files.push(theme + '/' + __src + files.src);
new run_cmd("sync", [],
function(buf) {
// console.log('' + buf)
},
function() {
_install(_files, files.dest, cb);
})
} else {
console.error("Install error : src or dest not defined", files.src, files.dest);
}
}
}
_tasks.load = function(load, cb) {
var sep = load.indexOf(" ");
var cmd = load.substring(0, sep);
var args = load.substring(sep + 1).split(" ");
new run_cmd(cmd, args, function(buf) {
// console.log('' + buf);
},
function() {
cb(null, "load done");
});
}
return function _build(err, file) {
var buildCtx = yaml.safeLoad(fs.readFileSync(theme + '/build.yml', 'utf8'));
var tasks = [];
function tasksFactory(fn, argument) {
return function(cb) {
return fn(argument, cb);
}
}
for(var b in buildCtx) {
tasks.push(tasksFactory(_tasks[b], buildCtx[b]));
}
async.series(tasks, function(errors, results) {
console.log('\x1B[1m\x1B[36m>\x1B[35m>\x1B[39m\x1B[22m ' + theme);
for( var i in results) { var res = results[i]
console.log(' \x1B[1m\x1B[32m✓\x1B[39m\x1B[22m ' + res);
}
});
}
}
// MAIN
var context = yaml.safeLoad(fs.readFileSync('Ink.yml', 'utf8'));
for (var c in context.colors) {
context.colors[c] = color(context.colors[c]);
context.colors[c].ansi = function() { // TODO put this in the prototype of onecolor
return this.hex().replace(/#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/g, '$1/$2/$3');
}
};
// if (context.themes) for (var i = context.themes.length - 1; i >= 0; i--) {
// fs.readFile(context.themes[i] + '/build.yml', build(context.themes[i]));
// };
context.themes.forEach(function(theme) {
var file = fs.readFile(theme + '/build.yml', new build(theme));
// build(theme)(undefined, file);
})