-
Notifications
You must be signed in to change notification settings - Fork 913
/
deploy.js
112 lines (103 loc) · 3.39 KB
/
deploy.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
'use strict';
var RSVP = require('rsvp');
var prepareUpload = require('../../prepareUpload');
var utils = require('../../utils');
var chalk = require('chalk');
var ProgressBar = require('progress');
var api = require('../../api');
var _ = require('lodash');
var FirebaseError = require('../../error');
module.exports = function(context, options, payload) {
var local = context.hosting;
utils.logBullet('preparing ' + chalk.bold(payload.hosting.public) + ' directory for upload...');
return prepareUpload(options, payload.hosting).then(function(upload) {
if (!upload.foundIndex) {
utils.logWarning(chalk.bold('Warning:') + ' Public directory does not contain index.html');
}
if (!upload.manifest.length) {
return utils.reject('Must have at least one file in public directory to deploy.', {exit: 1});
}
_.assign(payload.hosting, {
version: local.versionId,
prefix: local.versionId + '/',
manifest: upload.manifest.map(function(file) {
return {path: file, object: file};
})
});
return new RSVP.Promise(function(resolve, reject) {
var lastCount = 0;
var lastPercent = 0;
var bar;
if (!options.nonInteractive && process.stderr) {
bar = new ProgressBar(chalk.bold('Uploading:') + ' [:bar] :percent', {
total: upload.manifest.length,
width: 40,
complete: chalk.green('='),
incomplete: ' ',
clear: true
});
} else {
process.stdout.write(chalk.cyan.bold('\ni') + chalk.bold(' Progress: ['));
}
local.versionRef.on('value', function(snap) {
var status = snap.child('status').val();
switch (status) {
case 'deploying':
var uc = snap.child('uploadedCount').val() || 0;
var percent = Math.round(100.0 * uc / upload.manifest.length);
if (bar) {
bar.tick(uc - lastCount);
} else {
process.stdout.write(_.repeat(chalk.green('.'), percent - lastPercent));
lastPercent = percent;
}
lastCount = uc;
break;
case 'deployed':
if (bar) {
bar.terminate();
} else {
process.stdout.write(chalk.bold(']') + '\n\n');
}
utils.logSuccess(upload.manifest.length + ' files uploaded successfully');
local.versionRef.off('value');
resolve();
break;
case 'removed':
reject(new FirebaseError('Not Implemented', {
exit: 2,
context: snap.val()
}));
break;
case null:
break;
default:
var msg = 'File upload failed';
if (snap.hasChild('statusMessage')) {
msg += ': ' + snap.child('statusMessage').val();
}
reject(new FirebaseError(msg, {
exit: 2,
context: snap.val()
}));
}
});
api.request('PUT', '/firebase/' + context.firebase + '/uploads/' + local.versionId, {
auth: true,
query: {
fileCount: upload.manifest.length,
message: options.message
},
files: {
site: {
filename: 'site.tar.gz',
stream: upload.stream,
contentType: 'application/x-gzip',
knownLength: upload.size
}
},
origin: api.uploadOrigin
}).catch(reject);
});
});
};