forked from Pomax/node-flickrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
executable file
·158 lines (141 loc) · 4.79 KB
/
compile.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
/**
* This compiles a client-side Flickr library
* based on the flickr method information.
*
* run with `node compile`
*/
(function() {
"use strict";
process.CLIENT_COMPILE = true;
var habitat = require("habitat"),
env = habitat.load(),
flickrOptions = env.get("FLICKR"),
APIBuilder = require("./src/flickr-api-object.js"),
Utils = require("./src/utils.js"),
filename = "flickrapi";
if(process.argv.indexOf("dev") > -1) {
filename = "flickrapi.dev";
}
var buffer = (function() {
var data = "";
return {
write: function(s) { data += s + "\n"; },
getData: function() { return data; }
};
}());
var methods = {};
var setupMethods = function(node, name) {
name = name || "flickr";
if(typeof node === "function" && node.data) {
Object.keys(node.data).forEach(function(key){
if(!node.data[key] || node.data[key].length === 0) {
delete node.data[key];
}
});
methods[name] = node.data;
} else {
Object.keys(node).forEach(function(key) {
setupMethods(node[key], name + "." + key);
});
}
};
var stripDev = function(methods) {
Object.keys(methods).forEach(function(methodName) {
var data = methods[methodName];
delete data.required;
delete data.optional;
delete data.errors;
delete data.url;
});
};
/**
* Write out a (nested) object
*/
var writeTree = function(node, name, write, wrapped) {
if(typeof node === "function") {
if (wrapped) {
write("Flickr.prototype"+name + " = (function(Utils) {");
write(" var method_name = \"flickr"+name+"\";");
write(" var security = " + JSON.stringify(node.security,null,2) + ";");
if(process.argv.indexOf("dev") > -1) {
write(" var required = " + JSON.stringify(node.data.required,null,2) + ";");
write(" var optional = " + JSON.stringify(node.data.optional,null,2) + ";");
write(" var errors = " + JSON.stringify(node.data.errors,null,2) + ";");
write(" var fn = " + node.toString());
write(" fn.data = { required: required, optional: optional, errors: errors, name: method_name };");
write(" return fn;");
}
else { write(" return " + node.toString()); }
write("}(Utils));\n");
methods.push("flickr"+name);
} else {
write(name + " = " + node.toString() + ";");
}
} else {
Object.keys(node).forEach(function(key) {
writeTree(node[key], name + "." + key, write, wrapped);
});
}
};
/**
* Compile a client-side library based on the flickr-api-object code.
*/
new APIBuilder(flickrOptions, Utils, function(err, flickr) {
if(err) {
console.error(err);
process.exit(1);
}
delete flickr.options;
buffer.write("(function() {");
// library-specific Utils
buffer.write(" var Utils = {};");
writeTree(require("./browser/Utils.js"), "Utils", buffer.write);
buffer.write(" Utils.errors = " + JSON.stringify(Utils.getCallErrors(),false, 4) + ";");
// Flickr object definition
buffer.write(" var Flickr = " + (function(flickrOptions) {
this.bindOptions(flickrOptions);
}).toString() + ";");
buffer.write(" Flickr.prototype = {};");
// Prototype from methods
setupMethods(flickr);
if (process.argv.indexOf("dev") === -1) { stripDev(methods); }
buffer.write(" Flickr.methods = " + JSON.stringify(methods,false,1) + ";");
var fn = (function() {
Object.keys(Flickr.methods).forEach(function(method) {
var level = method.split(".").slice(1);
var e = Flickr.prototype, key;
while(level.length > 1) {
key = level.splice(0,1)[0];
if(!e[key]) { e[key] = {}; }
e = e[key];
}
e[level] = Utils.generateAPIFunction(Flickr.methods[method]);
});
}).toString();
if (process.argv.indexOf("dev") > -1) { fn = fn.replace("generateAPIFunction","generateAPIDevFunction"); }
buffer.write("\n(" + fn + "());\n");
// option binding for individual instances
buffer.write(" Flickr.prototype.bindOptions = " + (function(flickrOptions) {
this.flickrOptions = flickrOptions;
(function bindOptions(obj, props) {
Object.keys(props).forEach(function(key) {
if (key === "flickrOptions") return;
if (typeof obj[key] === "object") {
bindOptions(obj[key], props[key]);
obj[key].flickrOptions = flickrOptions;
}
});
}(this, Flickr.prototype));
}).toString() + ";");
// end of library
buffer.write("\n window.Flickr = Flickr;");
buffer.write("}());");
// write out the library to file
var fs = require("fs");
filename = "./browser/"+filename+".js";
fs.writeFile(filename, buffer.getData(), function() {
console.log("written "+filename);
process.exit(0);
});
});
}());