forked from fossasia/open-event-wsgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.js
214 lines (185 loc) · 6.73 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
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
'use strict';
var distHelper = require('./dist.js');
var fs = require('fs');
var Github = require('github');
var gh = new Github();
var async = require('async');
function encode(file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString('base64');
}
var Gemfile = __dirname + '/Gemfile';
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
next();
});
}
else {
results.push(file);
next();
}
});
})();
});
};
module.exports = function(accessToken, folder, user, socket, callback) {
gh.authenticate({type: 'oauth', token: accessToken});
var fullPath = distHelper.distPath + '/' + folder;
var eventName = folder.substr(folder.search('/') + 1);
var repoName = 'eventSiteWebApp';
async.series([
(done) => {
socket.emit('started', 'Deleting previously existing repo of same name');
console.log('----------Deleting previously existing repo of same name -----------------');
gh.repos.delete({owner: user, repo: repoName }, function(err, res) {
if(err) {
console.log('Event Repo doesn\'t exist');
socket.emit('errorLog', 'Event Repo doesn\'t exist. Continuing further');
}
if(socket.abortDeploy) {
done('aborted');
return;
}
done(null, 'delete');
});
},
(done) => {
socket.emit('started', 'Creating the eventSiteWebApp repo');
console.log('------------Creating the eventSiteWebApp repo ------------');
gh.repos.create({name: repoName, auto_init: 1}, function(err, response) {
if(err) {
console.log('Error happened while creating repository');
done(err, 'error');
socket.emit('errorLog', 'Error happened while creating repository. Can\'t continue further');
return;
}
if(socket.abortDeploy) {
done('aborted');
return;
}
done(null, 'create');
});
},
(done) => {
socket.emit('started', 'Creating Gemfile in the repository');
console.log('-------------Creating Gemfile in the eventsiteWebApp repository');
gh.repos.createFile({owner: user, repo: repoName, path: 'Gemfile', message: 'commit by web app', content: encode(Gemfile)},
function(err, res) {
if(err) {
console.log('Error occured in pushing Gemfile');
done(err, 'error');
socket.emit('errorLog', 'Error occured in pushing Gemfile. Can\'t continue further');
return;
}
if(socket.abortDeploy) {
done('aborted');
return;
}
done(null, 'Gemfile');
});
},
(done) => {
socket.emit('started', 'Creating a list of all files to be uploaded and committed to Github');
console.log('-------------Creating a list of all the files to be uploaded and committed to the github--------------');
walk(fullPath, function(err, results) {
if(err) {
console.log('Error happened while traversing the event site folder');
done(err, 'error');
socket.emit('errorLog', 'Error happened while traversing the event site folder. Can\'t continue further');
return;
}
var total = results.length;
var counter = 0;
function doOne() {
if(results.length > 0) {
var elem = results.shift();
var fileName = elem.substr(elem.search(eventName)+eventName.length + 1);
console.log(fileName);
setTimeout(function(){
socket.emit('select', fileName + ' uploading');
gh.repos.createFile({owner: user, repo: repoName, path: fileName, message: 'commit by web app', content: encode(elem)},
function(err, res) {
counter += 1;
if(err) {
console.log('Error occured while uploading '+ fileName);
console.log(err);
socket.emit('errorLog', 'Error happened while uploading ' + fileName + '. Ignoring');
}
else {
socket.emit('fileUpload', {file: fileName, percent: (counter*100/total)});
}
if(socket.abortDeploy) {
done('aborted');
return;
}
doOne();
});
}, 1000);
}
else {
done(null, 'uploaded and committed');
}
}
doOne();
});
},
(done) => {
socket.emit('progress', 'File upload done. Creating gh-pages branch');
console.log('-------------Creating gh-pages branch in the created repository--------------------');
gh.gitdata.getReference({owner: user, repo: repoName, ref: 'heads/master'}, function(err, res) {
if(err) {
console.log('Error happened when getting the sha of latest commit on master branch');
done(err, 'error');
socket.emit('errorLog', 'Error happened when getting the sha of latest commit on master branch. Can\'t continue further');
return;
}
if(socket.abortDeploy) {
done('aborted');
return;
}
var sha = res['object']['sha'];
gh.gitdata.createReference({owner: user, repo: repoName, ref: 'refs/heads/gh-pages', sha: sha}, function(err, res) {
if(err) {
console.log('Error happened while creating gh-pages branch');
done(err, 'error');
socket.emit('errorLog', 'Error happened while creating gh-pages branch. Can\'t continue further');
return;
}
if(socket.abortDeploy) {
done('aborted');
return;
}
socket.emit('progress', 'Gh-pages branch created');
console.log('Gh-pages branch created');
setTimeout(function() {
socket.emit('finished', 'http://'+user + '.github.io/' + repoName);
callback();
}, 10000);
});
});
}
],
function(err, results) {
if(err) {
if (err === 'aborted') {
console.log('The process was aborted by the user');
socket.emit('abort', 'The process has been successfully aborted');
}
else {
console.log('The deployment process failed with an error');
console.log(err);
}
}
});
};