This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AppHubDeploy.js
executable file
·284 lines (216 loc) · 8.47 KB
/
AppHubDeploy.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/sh
":" //# comment; exec /usr/bin/env node --harmony "$0" "$@"
// http://stackoverflow.com/a/28739537/5498949 for above.
var fs = require('fs');
var open = require('open');
var package = require('./package.json');
var path = require('path');
var program = require('commander');
var readlineSync = require('readline-sync');
var APP_HUB_ID;
var APP_HUB_SECRET;
var BUILD_FILE_NAME = 'AppHubBuild_' + Date.now() + '.zip';
var BUILD_FILE_PATH = path.resolve('./', BUILD_FILE_NAME);
var BUILD_URL_BASE = 'https://dashboard.apphub.io/projects/';
var BUILD_URL;
program
.version(package.version)
.option('-a, --app-versions <app-versions>', 'App Versions separated by commas that are compatible with this build. Either do not use a space in between version or wrap it in quotes. Example: -a 1.0.3,1.0.4 Defaults to value in info.plist of build file.' )
.option('-c, --configure', '(Re)Configure AppHub ID and Secret key.')
.option('-d, --build-description <description>', 'Description of the build. Wrap in quotes if more than one word.')
.option('-e, --entry-file <entry-file>', 'The entry file for your application e.g. `index.ios.js` passed into `apphub build`.')
.option('-o, --open-build-url', 'Open AppHub Builds URL after a successful build and deploy.')
.option('-n, --build-name <name>', 'Name of the build. Wrap in quotes if more than one word.')
.option('-p, --plist-file <plist-file>', 'Use a custom plist file path in the `apphub build` command.')
.option('-r, --retain-build', 'Do not remove the build after a successful deploy. By default it will be removed.')
.option('-t, --target <target>', 'One of [all, debug, none] which specifies the target audience of the build. Defaults to none.')
.option('-v, --verbose', 'Unleashes the "Chatty Kathy" to the STDOUT - great for debugging!')
.parse(process.argv);
checkOptionValues();
if (program.configure) {
setup();
}
else {
readPreviouslySavedAppHubCredentials();
}
var BUILD_URL = BUILD_URL_BASE + APP_HUB_ID;
build();
deploy();
if (!program.retainBuild)
removeBuildFile();
if (program.openBuildUrl)
openBuildUrl();
process.exit(0);
// Private Functions
function checkOptionValues() {
permittedValues = ["all", "debug", "none"];
if (program.target && !permittedValues.includes(program.target)) {
console.log('-t --target option needs to be one of ' + permittedValues.join(", ") + '.');
console.log('');
process.exit(1);
}
}
function setup() {
APP_HUB_ID = readlineSync.question('AppHub App ID: ');
APP_HUB_SECRET = readlineSync.question('AppHub App Secret: ');
console.log('');
var appHubCredentialsAsJSON = JSON.stringify( { "appHubId": APP_HUB_ID, "appHubSecret": APP_HUB_SECRET } );
try {
fs.writeFileSync( './.apphub', appHubCredentialsAsJSON, { mode: 0600 } );
}
catch (error) {
console.log('There was an error saving your AppHub config to file.');
console.log(error.message);
process.exit(1);
}
console.log('AppHub configuration saved to .apphub for future deploys.');
};
function readPreviouslySavedAppHubCredentials() {
// If run without any .apphub file then run setup.
var appHubData;
try {
var appHubFileData = fs.readFileSync( './.apphub' );
appHubData = JSON.parse(appHubFileData);
// If .apphub exists, try and get values.
if (!appHubData.appHubId.trim() || !appHubData.appHubSecret.trim())
throw new Error('One or both of your AppHub credentials are blank');
// .apphub file exists, can be read and the credentials are reasonable (i.e. present and not blank).
if (program.verbose)
console.log('Found .apphub file! Reading credentials.');
APP_HUB_ID = appHubData.appHubId;
APP_HUB_SECRET = appHubData.appHubSecret;
}
catch (error) {
if (error.code == 'ENOENT') {
// If missing file, no problem, we'll kick off the Setup function to create it.
setup();
}
else {
console.log('The contents of .apphub file were not what we were expecting. Try running with --configure command to re-enter your AppHub credentials.');
console.log('');
process.exit(1);
}
}
};
function build() {
console.log('');
process.stdout.write('Building... ');
var appHubBuildOptions = ["--verbose"]
if (program.plistFile) { appHubBuildOptions.push("--plist-file " + program.plistFile) }
if (program.entryFile) { appHubBuildOptions.push("--entry-file " + program.entryFile) }
if (program.target == "debug") { appHubBuildOptions.push("--dev") }
appHubBuildOptions.push("--output-zip " + BUILD_FILE_NAME)
buildResult = require('child_process').execSync( './node_modules/.bin/apphub build ' + appHubBuildOptions.join(" ") ).toString();
if (program.verbose) {
console.log(buildResult);
console.log('');
}
process.stdout.write('Done!');
};
function deploy() {
console.log('');
process.stdout.write('Deploying... ');
try {
// Compile any Meta Data into an Array to be used in the cURL request.
var metaData = {};
if (program.target)
metaData['target'] = program.target;
if (program.buildName)
metaData['name'] = program.buildName;
if (program.buildDescription)
metaData['description'] = program.buildDescription;
if (program.appVersions)
metaData['app_versions'] = program.appVersions;
var metaDataString = '{ ';
Object.keys(metaData).forEach( (key, index) => {
if (index != 0)
metaDataString += ', ';
metaDataString += '"' + key + '": ';
if (key == 'app_versions') {
metaDataString += '[';
metaData[key].split(',').forEach( (appVersion, index) => {
if (index != 0)
metaDataString += ',';
metaDataString += '"' + appVersion.trim() + '"';
})
metaDataString += ']';
}
else {
metaDataString += '"' + metaData[key] + '"';
}
})
metaDataString += ' }'
getUrlForPutCommand = 'curl -X GET';
if (!program.verbose)
getUrlForPutCommand += ' --silent';
getUrlForPutCommand += ' -H "X-AppHub-Application-ID: ' + APP_HUB_ID + '"';
getUrlForPutCommand += ' -H "X-AppHub-Application-Secret: ' + APP_HUB_SECRET + '"';
getUrlForPutCommand += ' -H "Content-Type: application/zip"';
// Add Meta Data if any are set with the options.
if (metaDataString != '{ }')
getUrlForPutCommand += ' -H \'X-AppHub-Build-Metadata: ' + metaDataString.replace(/'/g, '_') + "'";
getUrlForPutCommand += ' -L https://api.apphub.io/v1/upload';
getUrlForPutCommand += ' | python -c \'import json,sys;obj=json.load(sys.stdin);print obj["data"]["s3_url"]\'';
if (program.verbose) {
console.log('GET Command:');
console.log(getUrlForPutCommand);
}
urlForPut = require('child_process').execSync( getUrlForPutCommand ).toString().trim();
if (program.verbose) {
console.log('urlForPut:');
console.log(urlForPut);
}
putCommand = 'curl -X PUT';
if (!program.verbose)
putCommand += ' --silent';
putCommand += ' -H "Content-Type: application/zip"';
putCommand += ' -L "' + urlForPut + '"';
putCommand += ' --upload-file ' + BUILD_FILE_NAME;
if (program.verbose) {
console.log('putCommand:');
console.log(putCommand);
}
putResponse = require('child_process').execSync( putCommand ).toString().trim();
if (program.verbose) {
console.log( putResponse );
console.log('');
}
process.stdout.write('Done!');
}
catch(error) {
console.log('');
console.log('There was a problem uploading the build:');
console.log(error);
process.exit(1);
}
console.log('');
console.log('');
console.log('SUCCESSFULLY BUILT AND DEPLOYED TO APPHUB!')
console.log('');
console.log('You can see your build here: ' + BUILD_URL);
};
function removeBuildFile() {
try {
console.log('');
process.stdout.write('Removing Build File... ');
if (program.verbose) {
console.log('BUILD_FILE_PATH: ')
console.log(BUILD_FILE_PATH);
}
fs.unlinkSync(BUILD_FILE_PATH)
process.stdout.write('Done!');
console.log('');
console.log('');
}
catch(error) {
console.log('');
console.log('There was a problem removing the build file: ' + BUILD_FILE_PATH);
console.log('');
console.log(error);
process.exit(1);
}
}
function openBuildUrl() {
console.log('Opening AppHub Builds in your browser...');
open(BUILD_URL);
};