forked from adaptlearning/adapt_authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.js
397 lines (311 loc) · 10.3 KB
/
upgrade.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
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
var builder = require('./lib/application');
var prompt = require('prompt');
var fs = require('fs');
var request = require('request');
var async = require('async');
var exec = require('child_process').exec;
var rimraf = require('rimraf');
var path = require('path');
var optimist = require('optimist');
// Constants
var DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36';
// Helper
var isVagrant = function () {
if (process.argv.length > 2) {
return true;
}
return false;
};
// GLOBALS
var app = builder();
var installedBuilderVersion = '';
var latestBuilderTag = '';
var installedFrameworkVersion = '';
var latestFrameworkTag = '';
var shouldUpdateBuilder = false;
var shouldUpdateFramework = false;
var versionFile = JSON.parse(fs.readFileSync('version.json'), {encoding: 'utf8'});
var configFile = JSON.parse(fs.readFileSync('conf/config.json'), {encoding: 'utf8'});
var steps = [
function(callback) {
console.log('Checking versions');
if (versionFile) {
installedBuilderVersion = versionFile.adapt_authoring;
installedFrameworkVersion = versionFile.adapt_framework;
}
console.log('Currently installed versions:\n- ' + app.polyglot.t('app.productname') + ': ' + installedBuilderVersion + '\n- Adapt Framework: ' + installedFrameworkVersion);
callback();
},
function(callback) {
console.log('Checking for ' + app.polyglot.t('app.productname') + ' upgrades...');
// Check the latest version of the project
request({
headers: {
'User-Agent' : DEFAULT_USER_AGENT
},
uri: 'https://api.github.com/repos/adaptlearning/adapt_authoring/tags',
method: 'GET'
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var tagInfo = JSON.parse(body);
if (tagInfo) {
latestBuilderTag = tagInfo[0].name;
}
callback();
}
});
},
function(callback) {
console.log('Checking for Adapt Framework upgrades...');
// Check the latest version of the framework
request({
headers: {
'User-Agent' : DEFAULT_USER_AGENT
},
uri: 'https://api.github.com/repos/adaptlearning/adapt_framework/tags',
method: 'GET'
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var tagInfo = JSON.parse(body);
if (tagInfo) {
latestFrameworkTag = tagInfo[0].name;
}
callback();
}
});
},
function(callback) {
// Check what needs upgrading
if (latestBuilderTag != installedBuilderVersion) {
shouldUpdateBuilder = true;
console.log('Update for ' + app.polyglot.t('app.productname') + ' is available: ' + latestBuilderTag);
}
if (latestFrameworkTag != installedFrameworkVersion) {
shouldUpdateFramework = true;
console.log('Update for Adapt Framework is available: ' + latestFrameworkTag);
}
// If neither of the Builder or Framework need updating then quit the upgrading process
if (!shouldUpdateFramework && !shouldUpdateBuilder) {
console.log('No updates available at this time\n');
process.exit(0);
}
callback();
}, function(callback) {
// Upgrade Builder if we need to
if (shouldUpdateBuilder) {
upgradeBuilder(latestBuilderTag, function(err) {
if (err) {
return callback(err);
}
versionFile.adapt_authoring = latestBuilderTag;
callback();
});
} else {
callback();
}
}, function(callback) {
// Upgrade Framework if we need to
if (shouldUpdateFramework) {
upgradeFramework(latestFrameworkTag, function(err) {
if (err) {
return callback(err);
}
versionFile.adapt_framework = latestFrameworkTag;
callback();
});
} else {
callback();
}
}, function(callback) {
// After upgrading let's update the version.json to the latest version
fs.writeFile('version.json', JSON.stringify(versionFile, null, 4), function(err) {
if(err) {
callback(err);
} else {
console.log("Version file updated\n");
callback();
}
});
}, function(callback) {
if (shouldUpdateFramework) {
// If the framework has been updated, interrogate the adapt.json file from the adapt_framework
// folder and install the latest versions of the core plugins
fs.readFile(path.join(configFile.root, 'temp', configFile.masterTenantID, 'adapt_framework', 'adapt.json'), function (err, data) {
if (err) {
return callback(err);
}
var json = JSON.parse(data);
// 'dependencies' contains a key-value pair representing the plugin name and the semver
var plugins = Object.keys(json.dependencies);
async.eachSeries(plugins, function(plugin, pluginCallback) {
app.bowermanager.installPlugin(plugin, json.dependencies[plugin], function(err) {
if (err) {
return pluginCallback(err);
}
pluginCallback();
});
}, function(err) {
if (err) {
console.log(err);
return callback(err);
}
callback();
});
});
} else {
callback();
}
},
function(callback) {
// Left empty for any upgrade scripts - just remember to call the callback when done.
callback();
}
];
app.run({skipVersionCheck: true, skipStartLog: true});
app.on('serverStarted', function () {
prompt.override = optimist.argv;
prompt.start();
// Prompt the user to begin the install
if (isVagrant()) {
console.log(`\nUpdate the ${app.polyglot.t('app.productname')} (and/or Adapt Framework) to the latest released version.`);
} else {
console.log(`\nThis script will update the ${app.polyglot.t('app.productname')} (and/or Adapt Framework) to the latest released version. Would you like to continue?`);
}
prompt.get({ name: 'Y/n', type: 'string', default: 'Y' }, function (err, result) {
if (!/(Y|y)[es]*$/.test(result['Y/n'])) {
return exitUpgrade();
}
// run steps
async.series(steps, function (err, results) {
if (err) {
console.log('ERROR: ', err);
return exitUpgrade(1, 'Upgrade was unsuccessful. Please check the console output.');
}
console.log(' ');
exitUpgrade(0, 'Great work! Your ' + app.polyglot.t('app.productname') + ' is now updated.');
});
});
});
// This upgrades the Builder
function upgradeBuilder(tagName, callback) {
console.log('Upgrading the ' + app.polyglot.t('app.productname') + '...please hold on!');
var child = exec('git fetch origin', {
stdio: [0, 'pipe', 'pipe']
});
child.stdout.on('data', function(err) {
console.log(err);
});
child.stderr.on('data', function(err) {
console.log(err);
});
child.on('exit', function (error, stdout, stderr) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log("Fetch from GitHub was successful.");
console.log("Pulling latest changes...");
var secondChild = exec('git reset --hard ' + tagName, {
stdio: [0, 'pipe', 'pipe']
});
secondChild.stdout.on('data', function(err) {
console.log(err);
});
secondChild.stderr.on('data', function(err) {
console.log(err);
});
secondChild.on('exit', function (error, stdout, stderr) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log("Installing " + app.polyglot.t('app.productname') + " dependencies.\n");
var thirdChild = exec('npm install', {
stdio: [0, 'pipe', 'pipe']
});
thirdChild.stdout.on('data', function(err) {
console.log(err);
});
thirdChild.stderr.on('data', function(err) {
console.log(err);
});
thirdChild.on('exit', function (error, stdout, stderr) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log("Dependencies installed.\n");
console.log("Building front-end.\n");
var fourthChild = exec('grunt build:prod', {
stdio: [0, 'pipe', 'pipe']
});
fourthChild.stdout.on('data', function(err) {
console.log(err);
});
fourthChild.stderr.on('data', function(err) {
console.log(err);
});
fourthChild.on('exit', function (error, stdout, stderr) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log("front-end built.\n");
console.log(app.polyglot.t('app.productname') + " has been updated.\n");
callback();
});
});
});
});
}
// This upgrades the Framework
function upgradeFramework(tagName, callback) {
console.log('Upgrading the Adapt Framework...please hold on!');
var child = exec('git fetch origin', {
cwd: 'temp/' + configFile.masterTenantID + '/adapt_framework',
stdio: [0, 'pipe', 'pipe']
});
child.stdout.on('data', function(err) {
console.log(err);
});
child.stderr.on('data', function(err) {
console.log(err);
});
child.on('exit', function (error, stdout, stderr) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log("Fetch from GitHub was successful.");
console.log("Pulling latest changes...");
var secondChild = exec('git reset --hard ' + tagName + ' && npm install', {
cwd: 'temp/' + configFile.masterTenantID + '/adapt_framework',
stdio: [0, 'pipe', 'pipe']
});
secondChild.stdout.on('data', function(err) {
console.log(err);
});
secondChild.stderr.on('data', function(err) {
console.log(err);
});
secondChild.on('exit', function (error, stdout, stderr) {
if (error) {
return console.log('ERROR: ' + error);
}
console.log("Framework has been updated.\n");
rimraf(configFile.root + '/temp/' + configFile.masterTenantID + '/adapt_framework/src/course', function(err) {
if (err) {
console.log(err);
}
callback();
});
});
});
}
/**
* Exits the install with some cleanup, should there be an error
*
* @param {int} code
* @param {string} msg
*/
function exitUpgrade (code, msg) {
code = code || 0;
msg = msg || 'Bye!';
console.log(msg);
process.exit(code);
}