forked from fossasia/open-event-wsgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
214 lines (187 loc) · 6.88 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
/* eslint-disable no-empty-label */
'use strict';
const distHelper = require('./dist.js');
const fs = require('fs');
const Github = require('github');
const gh = new Github();
const async = require('async');
let results, bitmap, i, file, fullPath, eventName, repoName, total, counter, elem, fileName, sha;
const Gemfile = __dirname + '/Gemfile';
const walk = function(dir, done) {
results = [];
fs.readdir(dir, function(err, list) {
if (err) {
return done(err);
}
i = 0;
(function next() {
file = list[i++];
if (!file) {
return done(null, results);
}
file = dir + '/' + file;
fs.stat(file, function(error_stat, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(error, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
function encode(file_name) {
bitmap = fs.readFileSync(file_name);
return new Buffer(bitmap).toString('base64');
}
module.exports = function(accessToken, folder, user, socket, callback) {
gh.authenticate({type: 'oauth', token: accessToken});
fullPath = distHelper.distPath + '/' + folder;
eventName = folder.substr(folder.search('/') + 1);
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, resultsArray) {
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;
}
total = resultsArray.length;
counter = 0;
function doOne() {
if (resultsArray.length > 0) {
elem = resultsArray.shift();
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(error, res) {
counter += 1;
if (error) {
console.log('Error occured while uploading ' + fileName);
console.log(error);
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;
}
sha = res.data.object.sha;
gh.gitdata.createReference({owner: user, repo: repoName, ref: 'refs/heads/gh-pages', sha: sha}, function(error, response) {
if (error) {
console.log('Error happened while creating gh-pages branch');
done(error, '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, result) {
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);
}
}
});
};