forked from ks-tech/cs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (98 loc) · 3.17 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
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
var Path = require('path');
var FS = require('fs');
var startServer = require('rbs/lib/server').startServer;
var RBS = require('rbs/lib/rbs').RBS;
var rbs = new RBS();
var setupJSRequire = require('jsi').setupJSRequire;
var setupCS = require('./lib/compiler').setupCS;
var addExample = require('rbs/lib/server-ext').addExample;
setupJSRequire(rbs,'/static/');
setupCS(rbs,/\.css$/i,/\.html?$/i);
var config = JSON.parse(FS.readFileSync(require.resolve('cs/package.json')));
console.log('welcome to test cs: '+config.version);
['index.html','index.css','default.css',
'static/cs.js',
'static/gitbug.js',
'static/cs.htc','example/test.css','example/test.html'].forEach(function(path){
var file = require.resolve('cs/'+path);
var expect = FS.readFileSync(file);
addExample(path,expect);
});
if(require.resolve('cs/lib/runtime/cs-exported.js') != Path.resolve('./lib/runtime/cs-exported.js')){
addExample('static/cs.js',FS.readFileSync(require.resolve('cs/lib/runtime/cs-exported.js')));
}
startServer(rbs);
exports.setHtcPath = function(path){
console.log('htc path is reset as: ',path,'\nhtc mimeType is required as: text/x-component')
rbs.config.cs.htcPath = path;
addExample(path.replace(/^\//,''),FS.readFileSync(require.resolve('cs/static/cs.htc')));
return exports;
}
exports.setScriptPath = function(path){
console.log('script path is reset as: ',path)
rbs.config.cs.scriptPath = path;
if(!path.match(/^https?\:\/\//)){
if(path.indexOf('/static/')!=0){
var content = rbs.getContentAsBinary('/static/cs/lib/runtime/cs-exported.js').toString()
}else{
content = FS.readFileSync(require.resolve('cs/lib/runtime/cs-exported.js'));
}
addExample(path.replace(/^\//,''),content);
}
return exports;
}
var querystring = require('querystring');
var http = require('http');
exports.exportTo = function(output,options){
var postData = genPostData(options)
var options = {
host: '127.0.0.1',
port: 2012,
"user-agent":'nodejs',
'path':'/--export.zip',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var post = http.request(options,function(res){
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
console.log('write to:',output)
var out = FS.createWriteStream(output);
res.pipe(out);
res.on('end',function(){
console.log('close output:',output)
//out.close()
})
});
post.on('error', function(e) {
console.dir(e)
console.log("error: "+e.message,'\n',postData,postData.length );
});
// post.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
// });
post.write(postData);
post.end();
}
function genPostData(options){
if(options && options.branch){
var postData = {
};
if(options.branch.css){
postData.cssBranch = true;
}
if(options.branch.js){
postData.jsBranch = true;
}
if(options.branch.jsi){
postData.jsiClosure = true;
}
return querystring.stringify(postData)
}else{
return "cssBranch=true"
}
}
//var s = rbs.getContentAsBinary("/-webkit-index.css").toString();//console.log(s);