|
// Wrapper module around phonegap-build-api NodeJS package that provides a |
|
// promise-enabled set of commands for creating a PhoneGapBuild build script |
|
// References: |
|
// http://docs.build.phonegap.com/en_US/developer_api_api.md.html |
|
// https://www.npmjs.org/package/phonegap-build-api |
|
|
|
module.exports = function(api) { |
|
|
|
var $q = require("q"); |
|
var fs = require("fs"); |
|
|
|
var getFormData = function(data, file) { |
|
var fd = { |
|
form: { |
|
data: data |
|
} |
|
}; |
|
|
|
if (file) { |
|
fd.form.file = file; |
|
} |
|
|
|
return fd; |
|
}; |
|
|
|
var getKeyId = function(type, title) { |
|
console.log("Getting key id: " + type + "/" + title); |
|
var q = $q.defer(); |
|
|
|
api.get("/keys/" + type, function(e, data) { |
|
if (e) { |
|
q.reject(e); |
|
return; |
|
} |
|
|
|
var found = false; |
|
data.keys.forEach(function(key) { |
|
if (key.title == title) { |
|
console.log("Found key id: " + type + "/" + title + " - " + key.id); |
|
q.resolve(key.id); |
|
found = true; |
|
} |
|
}); |
|
|
|
if (found) { |
|
return; |
|
} |
|
|
|
q.reject("Couldn't find " + type + "/" + title + "; given: " |
|
+ data.keys.reduce(function(prev, curr) { |
|
if (prev) { |
|
prev = prev + ", "; |
|
} |
|
prev += curr.title |
|
}, "") |
|
); |
|
}); |
|
|
|
return q.promise; |
|
}; |
|
|
|
var unlockIosKey = function(id, password) { |
|
console.log("Unlocking ios key: " + id); |
|
var q = $q.defer(); |
|
|
|
api.put("/keys/ios/"+id, getFormData({password: password}), function(e, data) { |
|
if (e) { |
|
q.reject(e); |
|
return; |
|
} |
|
|
|
q.resolve(); |
|
}); |
|
|
|
return q.promise; |
|
}; |
|
|
|
var unlockAndroidKey = function(id, password) { |
|
console.log("Unlocking android key: " + id); |
|
var q = $q.defer(); |
|
|
|
api.put("/keys/android/"+id, getFormData({key_pw: password, keystore_pw: password}), function(e, data) { |
|
if (e) { |
|
q.reject(e); |
|
return; |
|
} |
|
|
|
q.resolve(); |
|
}); |
|
|
|
return q.promise; |
|
}; |
|
|
|
var uploadApp = function(zipFile, id) { |
|
console.log("Uploading app id " + id + " from " + zipFile); |
|
var q = $q.defer(); |
|
|
|
api.put("/apps/" + id, getFormData({debug: false}, zipFile), function(e, data) { |
|
if (e) { |
|
q.reject(e); |
|
return; |
|
} |
|
|
|
console.log("Successfully uploaded app id " + id); |
|
q.resolve(data); |
|
}); |
|
|
|
return q.promise; |
|
}; |
|
|
|
var setAppKeys = function(id, keys) { |
|
console.log("Setting app keys for app id: " + id); |
|
console.log(keys); |
|
var q = $q.defer(); |
|
|
|
api.put("/apps/" + id, getFormData({keys:keys}), function(e, data) { |
|
if (e) { |
|
q.reject(e); |
|
return; |
|
} |
|
|
|
console.log("Set app keys for app id: " + id); |
|
|
|
q.resolve(); |
|
}); |
|
|
|
return q.promise; |
|
}; |
|
|
|
var waitForBuild = function(id, type) { |
|
console.log("Waiting for build of app id " + id + " type " + type); |
|
var q = $q.defer(); |
|
|
|
var checkStatus = function() { |
|
api.get("/apps/" + id, function(e, data) { |
|
if (e) { |
|
q.reject(e); |
|
return; |
|
} |
|
|
|
var buildStatus = data.status[type]; |
|
console.log("Current build status for app id " + id + " type " + type + " is " + buildStatus); |
|
|
|
if (buildStatus == "complete") { |
|
q.resolve(); |
|
return; |
|
} |
|
|
|
if (buildStatus == "error") { |
|
q.reject("Build for app id " + id + " type " + type + " is error"); |
|
return; |
|
} |
|
|
|
setTimeout(checkStatus, 300); |
|
}); |
|
}; |
|
|
|
checkStatus(); |
|
|
|
return q.promise; |
|
}; |
|
|
|
var downloadAppBuild = function(id, type) { |
|
|
|
console.log("Downloading " + type + " build for app id: " + id); |
|
|
|
var fileNames = { |
|
ios: "build.ipa", |
|
android: "build.apk", |
|
winphone: "build.xap" |
|
}; |
|
|
|
var fileName = fileNames[type]; |
|
if (!fileName) { |
|
return $q.reject("Unknown build type: " + type); |
|
} |
|
|
|
var q = $q.defer(); |
|
|
|
api.get("/apps/" + id + "/" + type) |
|
.on("response", function(res) { |
|
res.pipe(fs.createWriteStream(fileName)) |
|
.on("close", function() { |
|
console.log("Downloaded " + type + " build for app id: " + id + " to " + fileName); |
|
q.resolve(fileName); |
|
}) |
|
.on("error", function() { |
|
q.reject("Failed to download the " + type + " build for app id: " + id); |
|
}); |
|
}) |
|
.on("error", function(e) { |
|
q.reject(e); |
|
}); |
|
|
|
return q.promise; |
|
}; |
|
|
|
return { |
|
getKeyId: getKeyId, |
|
unlockIosKey: unlockIosKey, |
|
unlockAndroidKey: unlockAndroidKey, |
|
uploadApp: uploadApp, |
|
setAppKeys: setAppKeys, |
|
waitForBuild: waitForBuild, |
|
downloadAppBuild: downloadAppBuild |
|
}; |
|
|
|
}; |