-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunbuild.js
98 lines (83 loc) · 1.99 KB
/
unbuild.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
var fs = require('fs');
var nodePath = require('path');
var dir = __dirname + '/../';
var read = function(path){
return fs.readFileSync(nodePath.join(dir, path)).toString();
}
var write = function(path, data){
return fs.writeFileSync(nodePath.join(dir, path), data);
}
var rm = function(path, full) {
path = full || nodePath.join(dir, path);
if(!fs.existsSync(path)){ return }
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
rm(null, curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
};
var mk = function(path){
path = nodePath.join(dir, path);
if(fs.existsSync(path)){ return }
fs.mkdirSync(path);
}
var between = function(text, start, end){
end = end || start;
var s = text.indexOf(start);
if(s < 0){ return ''}
s += start.length;
var e = text.indexOf(end, s);
if(e < 0){ return '' }
var code = text.slice(s, e);
return {s: s, t: code, e: e};
}
var next = function(start, end){
end = end || start;
if(!next.text){
next.text = start;
return;
}
var code = between(next.text, start, end);
next.text = next.text.slice(code.e + end.length);
return code.t;
}
var path = function(){
var code = next(',', ')');
var path;
try{path = eval(code);
}catch(e){console.log("fail", e)};
if(!path){ return }
if('.js' !== path.slice(-3)){
path += '.js';
}
return nodePath.join('./src', path);
}
var undent = function(code, n){
var regex = /\n\t\t/g;
if(1 === n){
regex = /\n\t/g;
}
return code.replace(regex, '\n');
}
;(function(){
rm('./src');
mk('./src');
mk('./src/polyfill');
mk('./src/adapters');
var gun = read('gun.js');
var code = next(gun);
code = next("/* UNBUILD */");
write('src/polyfill/unbuild.js', undent(code, 1));
(function recurse(c){
code = next(";require(function(module){", "})(require");
if(!code){ return }
var file = path();
if(!file){ return }
write(file, undent(code));
recurse();
}());
}());