This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathupload.js
More file actions
416 lines (346 loc) · 11.9 KB
/
Copy pathupload.js
File metadata and controls
416 lines (346 loc) · 11.9 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
var fs = require('fs');
var cheerio = require('cheerio');
var path = require('path');
var url = require('url');
var FormData = require('form-data');
var IonicProject = require('./project');
var Utils = require('./utils');
var Q = require('q');
var settings = require('./settings');
var ioLib = require('./io-config');
var request = require('request');
var shelljs = require('shelljs');
var log = require('./logging').logger;
var chalk = require('chalk');
var TEMP_FILENAME = 'www.zip';
var Upload = module.exports;
Upload.doUpload = function doUpload(appDirectory, jar, note, deploy) {
log.info('Uploading app...\n');
log.debug('Upload doUpload - ', appDirectory, jar);
if (deploy && deploy === true) {
log.info('Deploy channel not provided; defaulting to dev.');
deploy = 'dev'; // this is the default channel_tag
}
var project = IonicProject.load(appDirectory);
var documentRoot = project.get('documentRoot') || 'www';
var indexPath = path.join(appDirectory, documentRoot, 'index.html');
var upload;
try {
return Upload.addCacheBusters(indexPath)
.then(function() {
return Upload.zipContents(appDirectory, documentRoot);
})
.then(function() {
return Upload.removeCacheBusters(indexPath);
})
.then(function() {
return Upload.getDirectUploadKey(project, jar, note);
})
.then(function(key) {
// Save App ID and API key to the io-config
ioLib.writeIoConfig('app_id', key.app_id, true)
.then(function() {
// Do nothing
}, function(error) {
log.error('Error saving app ID:', error);
});
// Set project vars
project.set('app_id', key.app_id);
project.save();
return Upload.uploadToS3(appDirectory, key);
})
.then(function() { // receives status
return Upload.signalDashUpload(project, jar);
})
.then(function(status) {
upload = status;
var version = false;
if (status && (typeof status === 'object') && status.version) {
version = status.version;
if (status.api_key) {
ioLib.writeIoConfig('api_key', status.api_key, true).then(function() { // receives apiKey
ioLib.warnMissingData();
}, function(error) {
log.error('Error saving API key:', error);
});
}
}
return Upload.verify_tag(project, jar, deploy, version);
})
.then(function(deploy) {
return Upload.deploy(project, jar, deploy);
})
.then(function() { // receives status
return upload;
})
.catch(function(ex) {
log.error('An error occurred uploading the build: %s', ex, {});
throw ex;
});
} catch (ex) {
log.error('Upload errors occurred - %s', ex, {});
}
};
Upload.uploadToS3 = function uploadToS3(appDirectory, keyInfo) {
var q = Q.defer();
log.debug('Uploading zip file to S3');
var proxy = Utils.getProxy();
var zipFile = path.join(appDirectory, TEMP_FILENAME);
// Now we upload with the signed URL the dash returned
log.debug(zipFile);
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
proxy: proxy,
uri: keyInfo.signed_request,
headers: {
'x-amz-acl': 'private',
'content-type': 'application/zip'
},
body: fs.readFileSync(zipFile)
},
function(error) {
shelljs.rm('-f', path.join(appDirectory, TEMP_FILENAME));
if (error) {
return q.reject(error);
} else {
q.resolve(true);
}
});
return q.promise;
};
Upload.signalDashUpload = function signalDashUpload(project, jar) {
var q = Q.defer();
log.debug('Signaling to ionic.io completion of the upload');
var proxy = Utils.getProxy();
// The final step is to signal the dash that the file was successfully uploaded
request({
method: 'GET',
proxy: proxy,
uri: settings.IONIC_DASH_API + 'app/direct-upload/' + project.get('app_id'),
headers: {
cookie: jar.map(function(c) {
return c.key + '=' + encodeURIComponent(c.value);
}).join('; ')
}
},
function(error, response, body) {
if (error || parseInt(response.statusCode, 10) !== 200) {
q.reject('Upload Failed:', error || 'Server Error: ' + response.statusCode);
} else {
log.info(chalk.bold('Successfully uploaded (' + project.get('app_id') + ')\n'));
log.info(chalk.bold('Manage your app and access powerful services through the Ionic dashboard: https://apps.ionic.io/'));
q.resolve(JSON.parse(body));
}
});
return q.promise;
};
Upload.verify_tag = function verify_tag(project, jar, deploy, version) { // eslint-disable-line camelcase
var q = Q.defer();
log.debug('Checking if we need to deploy the upload');
if (deploy && version) {
var proxy = Utils.getProxy();
request({
method: 'GET',
proxy: proxy,
uri: settings.IONIC_DASH_API + 'apps/' + project.get('app_id') + '/channels/tag/' + deploy,
headers: {
cookie: jar.map(function(c) {
return c.key + '=' + encodeURIComponent(c.value);
}).join('; ')
}
},
function(error, response, body) {
if (error || parseInt(response.statusCode, 10) !== 200) {
q.reject('Deploy failed to verify the channel tag');
} else {
log.debug('Verified channel tag...');
q.resolve({
version: version,
channel: JSON.parse(body)
});
}
});
} else {
q.resolve(false);
}
return q.promise;
};
Upload.deploy = function deploy(project, jar, deploy) {
var q = Q.defer();
log.debug('Check for a deploy...');
if (deploy) {
log.info('Deploying to channel: ' + chalk.green.bold(deploy.channel.label));
var proxy = Utils.getProxy();
var csrftoken = Utils.retrieveCsrfToken(jar);
request({
method: 'PUT',
proxy: proxy,
uri: settings.IONIC_DASH_API + 'apps/' + project.get('app_id') +
'/channels/' + deploy.channel.id.toString() + '/',
headers: {
cookie: jar.map(function(c) {
return c.key + '=' + encodeURIComponent(c.value);
}).join('; '),
X_CSRFToken: csrftoken, // eslint-disable-line camelcase
'Content-Type': 'application/json'
},
form: { version: deploy.version }
},
function(error, response) {
if (error || parseInt(response.statusCode, 10) !== 200) {
q.reject('Deploy failed: ' + (error || response.statusCode));
} else {
log.info(chalk.green.bold('Deploy Successful!'));
q.resolve(true);
}
});
} else {
q.resolve(false);
}
return q.promise;
};
Upload.getDirectUploadKey = function getDirectUploadKey(project, jar, note) {
var q = Q.defer();
note = note ? note : '';
log.debug('Getting Upload information from ', settings.IONIC_DASH);
var csrftoken = '';
csrftoken = Utils.retrieveCsrfToken(jar);
var projectName = project.get('name');
if(!projectName) {
log.error(chalk.bold('Please set the name of your app in ionic.config.json before uploading.\n'));
q.reject('Missing app name in ionic.config.json');
return q.promise;
}
var form = new FormData();
form.append('name', project.get('name'));
form.append('note', note);
form.append('csrfmiddlewaretoken', csrftoken);
var directUploadUrl = settings.IONIC_DASH_API + 'app/direct-upload/' + project.get('app_id');
var params = url.parse(directUploadUrl);
form.submit({
protocol: params.protocol,
hostname: params.hostname,
port: params.port,
path: params.path,
headers: form.getHeaders({
cookie: jar.map(function(c) {
return c.key + '=' + encodeURIComponent(c.value);
}).join('; ')
})
}, function(err, response) {
if (err) {
log.error('There was an error trying to upload your app.');
var errorMessage;
if (err.code === 'ENOTFOUND' || err.code === 'EPIPE') {
errorMessage = 'The address you are trying to reach could not be found. \n' +
'This could be your internet connection or the server itself is having issues.';
} else {
errorMessage = 'The specific error message: ' + err;
}
q.reject(errorMessage);
return;
}
response.setEncoding('utf8');
var data = '';
var jsonData;
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
if (parseInt(response.statusCode, 10) === 401) {
return q.reject('Session expired (401). Please log in and run this command again.');
} else if (parseInt(response.statusCode, 10) === 403) {
return q.reject('Forbidden upload (403)');
} else if (parseInt(response.statusCode, 10) === 500) {
return q.reject('Server Error (500) :(');
} else if (parseInt(response.statusCode, 10) === 522) {
return q.reject('Connection timed out (522) :(');
}
try {
jsonData = JSON.parse(data);
} catch (parseEx) {
// keep error msg reasonably short
return q.reject(parseEx);
}
if (parseInt(response.statusCode, 10) !== 200) {
var errorMessage = jsonData ? jsonData['errors'] : 'Unknown error';
return q.reject(['An error occurred uploading your application - ', errorMessage].join(''));
}
q.resolve(jsonData);
});
});
return q.promise;
};
Upload.zipContents = function zipContents(appDirectory, documentRoot) {
return Utils.createArchive(appDirectory, documentRoot);
};
// If your Webview's strange, and its cache is no good? Who you gonna call?
//
// Cachebusters!
Upload.addCacheBusters = function addCacheBusters(indexPath) {
var q = Q.defer();
log.debug('When your webview is acting crazy who do you call? Cachebusters!');
var randomString = Math.floor(Math.random() * 100000);
var indexHtml = fs.readFileSync(indexPath, 'utf8');
var $ = cheerio.load(indexHtml, { decodeEntities: false });
var urlObj;
try {
$('script').each(function(i, el) {
if (typeof el.attribs.src === 'undefined') return true; // continue
urlObj = url.parse(el.attribs.src, true);
urlObj.query['ionicCachebuster'] = randomString;
el.attribs.src = url.format(urlObj);
});
$('link').each(function(i, el) {
if (typeof el.attribs.href === 'undefined') return true; // continue
urlObj = url.parse(el.attribs.href, true);
urlObj.query['ionicCachebuster'] = randomString;
el.attribs.href = url.format(urlObj);
});
var htmlToSave = $.html();
var bomIndex = htmlToSave.indexOf('');
if (bomIndex !== -1) {
htmlToSave = htmlToSave.replace('', '');
}
fs.writeFileSync(indexPath, htmlToSave);
q.resolve();
} catch (e) {
log.error('Unable to append cachebusters to index.html asset urls. Err: ' + e);
q.reject(e);
}
return q.promise;
};
Upload.removeCacheBusters = function removeCacheBusters(indexPath) {
var q = Q.defer();
log.debug('Removing cachebusting ', indexPath);
var indexHtml = fs.readFileSync(indexPath, 'utf8');
var $ = cheerio.load(indexHtml, { decodeEntities: false });
var index,
urlObj;
try {
$('script').each(function(i, el) {
if (typeof el.attribs.src === 'undefined') return true; // continue
urlObj = url.parse(el.attribs.src, true);
// Fix for: https://github.com/driftyco/ionic-cli/issues/504
if (!urlObj.query['ionicCachebuster']) return true;
delete urlObj.query['ionicCachebuster'];
delete urlObj.search; // or url.format will ignore modified `query`
el.attribs.src = url.format(urlObj);
});
$('link').each(function(i, el) {
if (typeof el.attribs.href === 'undefined') return true; // continue
urlObj = url.parse(el.attribs.href, true);
delete urlObj.query['ionicCachebuster'];
delete urlObj.search; // or url.format will ignore modified `query`
el.attribs.href = url.format(urlObj);
});
fs.writeFileSync(indexPath, $.html());
q.resolve();
} catch (e) {
log.error('Unable to remove cachebusters from index.html asset urls. Err: ' + e);
q.reject(e);
}
return q.promise;
};