这是一个用PhoneGap新构建的网站。
有趣的是,浏览器使JavaScript上传文件比XHR2更加容易。如果你还没有看到这一动作,请查看优秀的 HTML5 Rocks这篇文章。
下面我将整个pgbuild.js代码贴出来:
var http = require("https");
var fs = require("fs");
var path = require("path");
var username = "";
var password = "";
exports.setUsername = function(u) { username = u; }
exports.setPassword = function(p) { password = p; }
exports.createApp = function(options, success, fail) {
var httpOptions = getConfig("apps");
httpOptions.method = "POST";
//Detect if options.create_method is file, and if so, suck in the bits
//Fails if no .file
//Also note it doesn't support .zip yet.
if(options.create_method === "file") {
if(!options.file) throw new Error("Must supply file value.");
console.log("Need to read in a file:"+options.file);
//Shell out for file uploads
PreparePost(httpOptions,JSON.stringify(options), options.file, success);
} else {
//TODO
}
}
exports.getAllApps = function(success,fail) {
doCall("apps", function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.apps);
},function(e) {
if(fail) fail(e);
});
}
exports.getApp = function(id, success, fail) {
doCall("apps/"+id, function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res);
},function(e) {
if(fail) fail(e);
});
}
exports.getAppIcon = function(id, success, fail) {
doCall("apps/"+id +"/icon", function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.location);
},function(e) {
if(fail) fail(e);
});
}
//todo: Possibly validate platform? Should be: Android,blackberry,ios,symbian,webos,winphone
exports.getAppDownload = function(id, platform, success, fail) {
doCall("apps/"+id +"/"+platform, function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.location);
},function(e) {
if(fail) fail(e);
});
}
exports.getKeys = function() {
var platform = "";
if(arguments.length == 1) {
success = arguments[0];
} else if(arguments.length === 2) {
success = arguments[0];
fail = arguments[1];
} else if(arguments.length == 3) {
platform = arguments[0];
success = arguments[1];
fail = arguments[2];
}
var path = "keys";
if(platform != "") path+="/"+platform;
doCall(path, function(res) {
if(res.error && res.error.length && fail) fail(res.error);
else success(res.keys);
},function(e) {
if(fail) fail(e);
});
}