-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofish.js
98 lines (85 loc) · 2.47 KB
/
gofish.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
const request = require('request');
const fs = require('fs');
const username = process.argv[2];
const password = process.argv[3];
const api_url = `https://${username}:${password}@api.github.com`;
const aq_url = api_url + '/repos/klavinslab/aquarium/contents';
const models_url = aq_url + '/app/assets/javascripts/models';
const http_options = {
json: true,
headers: {
'User-Agent': "GoFish"
}
};
function authenticate() {
return new Promise(function(resolve,reject) {
request(api_url, http_options, (err,res,body) => {
console.log(body);
if (body.current_user_url) {
resolve(true)
} else {
reject("Could not authorize: " + body.message);
}
});
});
}
function get_paths() {
console.log("discovering file names");
return new Promise(function(resolve,reject) {
request(models_url, http_options, (err,res,contents) => {
if (err) { reject(err); }
let paths = [ '/app/assets/javascripts/aq.js' ];
for ( var i=0; i<contents.length; i++ ) {
paths.push(contents[i].path)
}
if ( paths.length == 0 ) { reject("could not connect. are you authenticated?"); }
console.log("found " + paths.length + " js files");
resolve(paths);
});
});
}
function get_content(path) {
return new Promise(function(resolve,reject) {
request(aq_url + "/" + path, http_options, (err,res,body) => {
console.log("retrieved " + path.split('/').pop());
if (err) { reject(err); }
let buf = new Buffer(body.content, 'base64');
resolve(`\n//\n// ${path}\n//\n\n` + buf.toString('ascii'));
});
});
}
function get_all_content(paths) {
console.log("getting files");
let promises = [];
for ( var i=0; i<paths.length; i++ ) {
promises.push(get_content(paths[i]));
}
return Promise.all(promises);
}
function compile_js(js_files) {
console.log("compiling javascript");
let str = fs.readFileSync('prequel.js', 'utf8');
str += "\n";
for (var i=0; i<js_files.length; i++) {
str += js_files[i];
}
str += fs.readFileSync('sequel.js', 'utf8');
str += "\nmodule.exports = AQ;\n";
return str;
}
function write_js(js) {
console.log("writing to aquarium.js");
fs.writeFile('aquarium.js', js, function (err) {
if (err) throw err;
console.log('done!');
});
}
authenticate()
.then(get_paths)
.then(get_all_content)
.then(compile_js)
.then(write_js)
.catch(msg => {
console.log(msg);
console.log("Usage: node gofish.js username password");
});