-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
129 lines (107 loc) · 3.16 KB
/
server.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
(function() {
'use strict';
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var enableSsl = process.env.SIS_ENABLE_SSL || false;
var shell = require('shelljs');
var chalk = require('chalk');
var serveIndex = require('serve-index');
var Admzip = require('adm-zip');
/**
* Make sure we only pass data over https
*/
var forceSSL = function(req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
return next();
};
// Force SSL when the option is enabled
if (enableSsl) {
app.use(forceSSL);
}
// List public for the static assets
app.use('/dist', serveIndex(__dirname + '/dist', {'icons': true}));
/**
* Echo console.log messages in a unified way
*/
var echo = function(message) {
shell.echo(chalk.green('Build: ') + message);
};
/**
* Creating the zip file
* @return {[type]} [description]
*/
var createZip = function() {
echo('Creating the zip file');
var zip = new Admzip();
zip.addLocalFolder('dist');
zip.writeZip('dist/files_latest.zip');
};
/**
* The build command
* - Fetch the latest code
* - Run a gulp build
* - Copy the build dir to the public directory
*/
var build = function() {
echo('Start ...');
if (shell.test('-d', 'x-sis-custom')) {
echo('Repo update');
shell.cd('x-sis-custom');
shell.exec('git pull');
echo('Repo update dependencies');
shell.exec('npm install');
echo('Gulp build - Start');
shell.exec('npm run build');
echo('Copy dist directory');
shell.cd('..');
shell.exec('rm -rf dist');
shell.exec('cp -Rv x-sis-custom/dist dist');
createZip();
app.use('/dist', express.static(__dirname + '/dist'));
} else {
echo(chalk.red('Initial fetch step is still happening'));
}
};
var buildResponse = function(req, res) {
res.json({
'status': 'building'
});
build();
};
// Will fire the build command
app.get('/api/build', buildResponse);
app.post('/api/build', buildResponse);
// Listen to a ping request
// We use this for uptimerobot
app.get('/api/ping', function(req, res) {
res.json({
'status': 'ok'
});
});
var fetch = function() {
if (!shell.test('-d', 'x-sis-custom') && !shell.test('-d', 'z-sis-custom')) {
echo('Fetch - Repo clone - Start');
shell.exec('git clone --depth 1 https://github.com/ucberkeley/sis-custom.git z-sis-custom');
echo('Fetch - Install packages');
shell.cd('z-sis-custom');
shell.exec('npm install');
echo('Fetch - Packages installed');
shell.cd('..');
shell.mv('z-sis-custom', 'x-sis-custom');
build();
}
};
var port = process.env.PORT || 5000;
server.listen(port);
server.on('listening', function() {
console.log('Express server started on port %s at %s', server.address().port, server.address().address);
// Clean up previous directories
shell.exec('rm -r dist x-sis-custom z-sis-custom');
// Initial Fetch
fetch();
});
})();