This repository has been archived by the owner on Jan 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
76 lines (71 loc) · 1.65 KB
/
main.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
var _ = require('lodash');
var fs = require('fs');
var unpack = require('browser-unpack');
var intreq = require('intreq');
var pack = require('browser-pack');
var through = require('through');
var intreqFn = function(unpacked, callback){
var r = intreq();
var rows = [];
r.on('data', function(row){
rows.push(row);
});
r.on('end', function(){
callback(rows);
});
_.each(unpacked, function(row){
r.write(row);
});
r.end();
};
var minify_require_paths = function(orig, callback, packOptions){
if (arguments.length === 0) {
var code = '';
return through(function(data){
code += data;
}, function(){
var self = this;
minify_require_paths(code, function(res){
self.queue(res);
self.queue(null);
});
});
}
var unpacked = unpack(orig.toString());
intreqFn(unpacked, function(rows){
var p = pack(packOptions);
var data = '';
p.on('data', function(buf){
data += buf;
});
p.on('end', function(){
callback(data);
});
p.end(JSON.stringify(rows));
});
};
var process_file = function(file, done, packOptions){
fs.readFile(file, function(err, code){
if(err) throw err;
minify_require_paths(code, function(minied){
fs.writeFile(file, minied, function(err){
if(err) throw err;
done();
});
}, packOptions);
});
};
module.exports = minify_require_paths;
module.exports.process_file = process_file;
module.exports.cli = function(){
var file = arguments[0];
//TODO: accept console arguments for pack
if(file){
if(!fs.existsSync(file)){
return console.error('File does not exist: %s', file);
}
process_file(file, _.noop);
} else {
process.stdin.pipe(minify_require_paths()).pipe(process.stdout);
}
};