-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
288 lines (244 loc) · 8.21 KB
/
index.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
#!/usr/bin/env node
/* index.js */
/**
* Installer CLI for the UCT Mars Rover project
*/
import chalk from 'chalk';
import shell from 'shelljs';
import program from 'commander';
import path from 'path';
let pkgMgr;
shell.echo(chalk.blue('------------------------'));
shell.echo(chalk.blue(chalk.bold('UCT'), 'Mars Rover installer'));
shell.echo(chalk.blue('------------------------'));
program
.version('0.1.4')
.usage('[options] <command>')
.option('-D, --debug-mode', 'do not suppress stdout');
program
.command('install').alias('i')
.description('build and install the UCT Mars Rover projects')
.action(() => {
_preChecks();
install();
});
program
.command('clone').alias('c')
.description('clone the UCT Mars Rover projects')
.action(() => {
_preChecks();
clone();
});
program
.command('clone-all').alias('ca')
.description('clone all projects including the writeup source and 3D models')
.action(() => {
_preChecks();
cloneAll();
});
program
.command('start-server').alias('start')
.description('start the RSVP Server as a forever process')
.action(() => {
_preChecks();
startServer();
});
program
.command('stop-server').alias('stop')
.description('stop the RSVP Server forever process')
.action(() => {
_preChecks();
stopServer();
});
program.parse(process.argv);
if (program.args.length === 0) program.help();
function install() {
clone();
shell.echo(chalk.green.bold('\nInstalling projects...\n'));
// Check tools
_checkPkgMgr();
_checkBower();
// Install projects
_installRSVPServer();
_installRSVPClient();
}
function clone() {
shell.echo(chalk.green.bold('\nCloning projects...\n'));
_checkGit();
_cloneRSVPServer();
_cloneRSVPClient();
_cloneRCE();
}
function cloneAll() {
clone();
shell.echo(chalk.green.bold('\nCloning extra projects...\n'));
_cloneModels();
_cloneWriteup();
}
function startServer() {
shell.echo(chalk.green.bold('\nStarting RSVP Server...\n'));
_checkPkgMgr();
_checkForever();
if (!shell.test('-d', 'mars-rover-rsvp-server')) {
shell.echo(chalk.red('\n Not all of the repositories have been cloned and install. Please run \'mars-rover install\' before starting the server.'));
process.exit(-1);
}
shell.cd('mars-rover-rsvp-server');
if (shell.exec(`${pkgMgr} run forever`).code !== 0) {
shell.echo(chalk.red('\n Failed to start the RSVP Server!'));
process.exit(-1);
} else {
shell.echo(chalk.green(' ✓ RSVP Server started at https://localhost:3000'));
}
shell.cd('..');
}
function stopServer() {
shell.echo(chalk.green.bold('\nStopping RSVP Server...\n'));
_checkForever();
shell.cd('mars-rover-rsvp-server');
if (shell.exec('forever stop mars-rover-rsvp-server').code !== 0) {
shell.echo(chalk.red('\n Failed to stop the RSVP Server!'));
process.exit(-1);
} else {
shell.echo(chalk.green(' ✓ RSVP Server stopped'));
}
shell.cd('..');
}
// === Private ===
function _preChecks() {
if (!program.debugMode) {
shell.config.silent = true;
}
}
function _checkGit() {
if (!shell.which('git')) {
shell.echo(chalk.red('\n \'git\' not found! Please install \'git\' before continuing'));
process.exit(-1);
}
}
function _checkPkgMgr() {
if (shell.which('yarn')) {
pkgMgr = 'yarn';
} else if (shell.which('npm')) {
pkgMgr = 'npm';
} else {
shell.echo(chalk.red('\n No node package manager found! Please install either \'yarn\' or \'npm\' before continuing'));
process.exit(-1);
}
}
function _checkBower() {
if (!shell.which('bower')) {
shell.echo(chalk.red('\n \'bower\' not found! Please install \'bower\' by running \'npm i -g bower\' before continuing'));
process.exit(-1);
}
}
function _checkForever() {
if (!shell.which('forever')) {
shell.echo(chalk.red('\n \'forever\' not found! Please install \'forever\' by running \'npm i -g forever\' before continuing'));
process.exit(-1);
}
}
function _cloneRSVPServer() {
shell.echo(chalk.blue('-> Cloning \'mars-rover-rsvp-server\'...'));
if (!shell.test('-d', 'mars-rover-rsvp-server')) {
if (shell.exec('git clone https://github.com/WoodyWoodsta/mars-rover-rsvp-server.git').code !== 0) {
shell.echo(chalk.red(' Failed to clone \'mars-rover-rsvp-server\'!'));
process.exit(-1);
}
} else {
shell.echo(chalk.yellow(' \'mars-rover-rsvp-server\' already cloned'));
}
}
function _cloneRSVPClient() {
shell.echo(chalk.blue('-> Cloning \'mars-rover-rsvp-client\'...'));
if (!shell.test('-d', 'mars-rover-rsvp-client')) {
if (shell.exec('git clone https://github.com/WoodyWoodsta/mars-rover-rsvp-client.git').code !== 0) {
shell.echo(chalk.red(' Failed to clone \'mars-rover-rsvp-client\'!'));
process.exit(-1);
}
} else {
shell.echo(chalk.yellow(' \'mars-rover-rsvp-client\' already cloned'));
}
}
function _cloneRCE() {
shell.echo(chalk.blue('-> Cloning \'mars-rover-rce\'...'));
if (!shell.test('-d', 'mars-rover-rce')) {
if (shell.exec('git clone https://github.com/WoodyWoodsta/mars-rover-rce.git').code !== 0) {
shell.echo(chalk.red(' Failed to clone \'mars-rover-rce\'!'));
process.exit(-1);
}
} else {
shell.echo(chalk.yellow(' \'mars-rover-rce\' already cloned'));
}
}
function _cloneWriteup() {
shell.echo(chalk.blue('-> Cloning \'mars-rover-writeup\'...'));
if (!shell.test('-d', 'mars-rover-writeup')) {
if (shell.exec('git clone https://github.com/WoodyWoodsta/mars-rover-writeup.git').code !== 0) {
shell.echo(chalk.red(' Failed to clone \'mars-rover-writeup\'!'));
process.exit(-1);
}
} else {
shell.echo(chalk.yellow(' \'mars-rover-writeup\' already cloned'));
}
}
function _cloneModels() {
shell.echo(chalk.blue('-> Cloning \'mars-rover-models\'...'));
if (!shell.test('-d', 'mars-rover-models')) {
if (shell.exec('git clone https://github.com/WoodyWoodsta/mars-rover-models.git').code !== 0) {
shell.echo(chalk.red(' Failed to clone \'mars-rover-models\'!'));
process.exit(-1);
}
} else {
shell.echo(chalk.yellow(' \'mars-rover-models\' already cloned'));
}
}
function _installRSVPServer() {
// Install
shell.echo(chalk.blue('-> Installing the RSVP Server...'));
shell.cd('mars-rover-rsvp-server');
if (shell.exec(`${pkgMgr} install`).code !== 0) {
shell.echo(chalk.red(' Failed to install \'mars-rover-rsvp-server\'!'));
} else {
shell.echo(chalk.green(' ✓ RSVP Server installed successfully!'));
}
// Build
shell.echo(chalk.blue('-> Building the RSVP Server...'));
if (shell.exec(`${pkgMgr} run build`).code !== 0) {
shell.echo(chalk.red(' Failed to build \'mars-rover-rsvp-server\'!'));
} else {
shell.echo(chalk.green(' ✓ RSVP Server built successfully!'));
}
shell.cd('..');
}
function _installRSVPClient() {
// Node modules
shell.echo(chalk.blue('-> Installing the RSVP Client node modules...'));
shell.cd('mars-rover-rsvp-client');
if (shell.exec(`${pkgMgr} install`).code !== 0) {
shell.echo(chalk.red(' Failed to install \'mars-rover-rsvp-client\' node modules!'));
} else {
shell.echo(chalk.green(' ✓ RSVP Client node modules installed successfully!'));
}
// Bower components
shell.echo(chalk.blue('-> Installing the RSVP Client bower components...'));
if (shell.exec('bower install').code !== 0) {
shell.echo(chalk.red(' Failed to install \'mars-rover-rsvp-client\' bower components!'));
} else {
shell.echo(chalk.green(' ✓ RSVP Client bower components installed successfully!'));
}
// Build
shell.echo(chalk.blue('-> Building the RSVP Client...'));
shell.mkdir('../mars-rover-rsvp-server/app');
if (shell.exec(`${pkgMgr} run build`).code !== 0) {
shell.echo(chalk.red(' Failed to build \'mars-rover-rsvp-server\'!'));
} else {
shell.echo(chalk.green(' ✓ RSVP Client built successfully!'));
}
// Link bower to server
shell.echo(chalk.blue('-> Linking bower components to RSVP Server...'));
if (shell.ln('-sf', path.join(shell.pwd().toString(), 'bower_components'), '../mars-rover-rsvp-server/app/bower_components').code !== 0) {
shell.echo(chalk.red(' Failed to link bower components to \'../mars-rover-rsvp-server/app/bower_components\'!'));
}
shell.cd('..');
}