-
Notifications
You must be signed in to change notification settings - Fork 3
/
install-all
executable file
·111 lines (80 loc) · 2.74 KB
/
install-all
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
#!/bin/php
<?php
(function() {
$options = getopt('h', ['email:', 'password:', 'fill-db::', 'help::']);
if (!empty($options['h']) || !empty($options['help']) || empty($options)) {
return help();
}
validateArgs($options);
/* installNpmPackages(); */
/* installComposerPackages($options); */
require_once __DIR__ . '/server/vendor/autoload.php';
require_once __DIR__ . '/server/src/test/TestDatabase.php';
initializeDatabase($options);
})();
function help() {
echo "Args should be in the form --arg-name=\"value\"\n";
echo "Example call: `./install-all --email=\"[email protected]\" --password=\"strong_password\"`\n";
echo "To fill database with mock data, pass in `--fill-db=\"true\"`";
echo "To see this message again, run `./install-all --h`\n";
}
/**
* Makes sure options passed into script are in correct format
*/
function validateArgs(array $options) {
if (!filter_var($options['email'], FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid email address');
}
if (empty($options['email']) || empty($options['password'])) {
help();
throw new Exception('Invalid Arguments');
}
}
/**
* Installs client/ npm packages
*/
function installNpmPackages() {
echo "Installing npm packages for client/\n";
echo `npm --prefix ./client install`;
echo "Completed npm installation\n";
}
/**
* Creates database from schema
* If `--fill-db` is passed into shell script, fills database with mock data
*/
function initializeDatabase(array $options) {
echo "Initializing database\n";
if (!empty($options['fill-db']) && $options['fill-db']) {
echo "Creating a database with prefilled data...\n";
$Database = new TestDatabase(true);
$Database->init();
} else {
createEmptyDatabase();
}
echo "Database created\n";
createAdmin($options);
}
function createEmptyDatabase() {
echo "Creating an empty database...\n";
$Database = new TestDatabase(true);
$Database->connect();
$Database->create();
}
/**
* Creates admin account using email and password that were passed in as shell args
*/
function createAdmin(array $options) {
echo "Creating admin account\n";
Db::query("REPLACE INTO users (username, email, password, level, f_name, l_name, auth, auth_time)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
['admin', $options['email'], password_hash($options['password'], PASSWORD_DEFAULT), 3,
'Admin', 'Admin', '', date('Y-m-d H:i:s')]);
echo "Admin created\n";
}
function installComposerPackages($options) {
echo "Installing composer packages...\n";
$serverDir = __DIR__ . '/server';
echo `composer install -d={$serverDir}`;
echo "Packages have been installed successfully";
}
?>