-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinterface.js
executable file
·170 lines (143 loc) · 5.06 KB
/
interface.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
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
var
_api = require('./phonegapbuildapi'),
_program = require('commander'),
_req = require('request'),
_tools = require('./tools'),
URL = 'https://build.phonegap.com',
_MENU =
{
getUserData: {name: "Get User Data", idx: 0},
getAppsData: {name: "Get All Apps Data", idx: 1},
getAppDataById: {name: "Get App Data by ID", idx: 2},
getKeysData: {name: "Get Keys Data", idx: 3},
downloadApp: {name: "Download App", idx: 4},
quit: {name: "Quit", idx: 5}
},
_token = null,
/******************************************************************
* Gracefully ends the program, relinquishing control of std input
*****************************************************************/
quit = function(){
process.stdin.destroy();
},
/******************************************************************
* Standard Error handler. Prints error message (if any) on stdout
*****************************************************************/
stdErrorHandler = function(errorMsg){
if(errorMsg){
console.log(errorMsg);
}
else{
console.log("Unknown Error");
}
showMenu();
},
/******************************************************************
* Processes user's menu selection
*****************************************************************/
doMenuOption = function (menuOption){
var i;
switch(menuOption){
case _MENU.getUserData.idx:
_api.getUserData(_token, {success: function(data){
console.log(data);
showMenu();
}, error: stdErrorHandler});
break;
case _MENU.getAppsData.idx:
_api.getAppsData(_token, {success: function(data){
console.log(data);
showMenu();
}, error: stdErrorHandler});
break;
case _MENU.getAppDataById.idx:
_program.prompt("App ID: ", function(appId){
_api.getAppDataById(_token, appId, {success: function(data){
console.log(data);
showMenu();
}, error: stdErrorHandler});
});
break;
case _MENU.getKeysData.idx:
_api.getKeysData(_token, {success: function(data){
console.log(data);
showMenu();
}, error: stdErrorHandler});
break;
case _MENU.downloadApp.idx:
_program.prompt("App ID: ", function(appId){
var platformList=[], platformKeys = Object.keys(_tools.platforms);
for(i=0; i<platformKeys.length; i++){platformList.push(_tools.platforms[platformKeys[i]].name);}
console.log('\nPlatform:');
_program.choose(platformList, function(platformIdx){
_program.prompt("Output filepath: ", function(outFpath){
_api.downloadApp(_token, appId, platformList[platformIdx], outFpath, {success: function(data){
console.log("File successfully downloaded to: " + data);
showMenu();
}, error: stdErrorHandler});
});
});
});
break;
case _MENU.quit.idx:
quit();
break;
default:
showMenu();
break;
}
}
/******************************************************************
* Displays Menu
*****************************************************************/
showMenu = function(){
console.log('\n\nPlease choose one of the following options');
var menuList = [], menuKeys = Object.keys(_MENU);
for(var i=0; i<menuKeys.length; i++){menuList.push(_MENU[menuKeys[i]].name);}
_program.choose(menuList, function(menuOption){
doMenuOption(menuOption);
});
},
/******************************************************************
* Executes login by requesting an authentication token from the server
*****************************************************************/
doLogin = function(loginCredentials){
_api.createAuthToken(loginCredentials, {success: function(token){
_token = token;
showMenu();
},
error: function(errMsg){
stdErrorHandler(errMsg);
quit();
}});
},
/***************************************************
* Prompts user for login credentials
****************************************************/
getLoginCredentials = function(){
_program.prompt("Phonegap Build Username: ", function(username){
_program.password("Password: ", "*", function(password){
doLogin(username.trim()+":"+password.trim());
});
});
},
/************************************************************
* Initialize the program when no options have been selected
***********************************************************/
stdInit = function(){
getLoginCredentials();
};
/*
* Program Starts here!
*/
_program
.version('0.0.1')
.option('-u, --user <username:pwd>', 'Specify login credentials', String, '')
.parse(process.argv);
if(_program.user){
doLogin(_program.user);
}
else{
stdInit();
}