-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.js
183 lines (155 loc) · 5.89 KB
/
install.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
const fs = require('fs')
const path = require('path')
const ch = require('child_process')
const compVer = require('compare-versions')
const DB_VER = 'v0.1.0' // current db version
const MIN_DB_VER = 'v0.1.0' // minimum checkpoint db version
const globalElements = require('./classes/globalElements')
globalElements(false)
console.log('Loaded Global Properties and Functions.')
// Run
run().then(installDeps).then((v) => {
console.log('Installation completed!')
process.exit(0)
}).catch((e) => {
console.error('Error: ' + e.stack)
process.exit(1)
})
async function run () {
const config = require('./config')
console.log('Initializing Database...')
switch (config.db.type) {
case 'mysql':
case 'pg': {
const knex = require('knex')({
client: config.db.type,
connection: config.db.connection
})
// 'dbinfo' table (db structure version storage)
const exists = await knex.schema.hasTable('dbinfo')
if (!exists) {
console.log("'dbinfo' table not found. Creating...")
await knex.schema.createTable('dbinfo', function (t) {
t.string('DB_VER', 32).notNullable().collate('utf8_unicode_ci')
if (config.db.type === 'mysql') t.charset('utf8')
})
await knex('dbinfo').insert({ DB_VER })
console.log("Created 'dbinfo' table.")
// Fresh Install
dbInstall(config.db.type, knex)
} else {
// check db version
const dbver = (await knex('dbinfo').select('DB_VER'))[0].DB_VER
console.log('Database structure version: ' + dbver)
if (compVer.compare(dbver, DB_VER, '=')) {
console.log('Database structure already at latest version. No need to upgrade.')
} else if (compVer.compare(dbver, MIN_DB_VER, '>=')) {
// Run upgrade
await dbUpgrade(config.db.type, knex)
} else if (compVer.compare(dbver, DB_VER, '>=')) {
// Cannot Downgrade
throw new Error('Newer database version detected. Cannot downgrade.\n\nInstallation terminated.')
} else {
// db structure outdated. exit.
throw new Error('Error: The database structure version is outdated. Please upgrade the database structure to ' + MIN_DB_VER + ' to install.\n\nInstallation terminated.')
}
}
break
}
case 'json': {
// JSON Database
console.log('JSON DB Init...')
const dbPath = path.join(path.resolve(), 'data')
if (!fs.existsSync(dbPath) || !fs.lstatSync(dbPath).isDirectory()) {
fs.mkdirSync(dbPath)
console.log('Created data directory.')
}
const jsonDBPath = path.join(dbPath, 'jsondb')
if (!fs.existsSync(jsonDBPath) || !fs.lstatSync(jsonDBPath).isDirectory()) {
fs.mkdirSync(jsonDBPath)
console.log('Created data/jsondb directory.')
}
const guildjson = path.join(jsonDBPath, 'guild.json')
if (!fs.existsSync(guildjson)) {
fs.writeFileSync(guildjson, '{}')
console.log('Created guild.json file.')
}
const userjson = path.join(jsonDBPath, 'user.json')
if (!fs.existsSync(userjson)) {
fs.writeFileSync(userjson, '{}')
console.log('Created user.json file.')
}
const blacklistjson = path.join(jsonDBPath, 'blacklist.json')
if (!fs.existsSync(blacklistjson)) {
fs.writeFileSync(blacklistjson, '[]')
console.log('Created blacklist.json file.')
}
console.log('JSON DB Init complete.')
}
}
}
async function installDeps () {
// Extensions module dependency
console.log('Installing module dependency for extensions...')
const extensions = fs.readdirSync(path.join(path.resolve(), 'extensions'))
await extensions.asyncForEach(async (extension) => {
const fullpath = path.join(path.resolve(), 'extensions', extension)
if (!fs.lstatSync(fullpath).isDirectory()) return
console.log('[Working] Installing modules for ' + extension)
await runInstallDeps(extension, fullpath)
console.log('[SUCCESS] Installed modules for ' + extension)
})
}
async function dbInstall (type, obj) {
switch (type) {
case 'mysql':
case 'pg': {
const knex = obj
// 'guilds' table
console.log("Creating 'guilds' table...")
await knex.schema.createTable('guilds', function (t) {
t.string('id', 20).primary().notNullable().collate('utf8_unicode_ci')
t.boolean('activated').notNullable().defaultTo(false)
t.string('prefix', 11).nullable().collate('utf8_unicode_ci')
t.string('locale', 5).notNullable().defaultTo('ko_KR').collate('utf8_unicode_ci')
if (type === 'mysql') t.charset('utf8')
})
console.log("Created table 'guilds'.")
// 'users' table
console.log("Creating 'users' table...")
await knex.schema.createTable('users', function (t) {
t.string('id', 20).primary().notNullable().collate('utf8_unicode_ci')
t.string('locale', 5).notNullable().defaultTo('ko_KR').collate('utf8_unicode_ci')
if (type === 'mysql') t.charset('utf8')
})
console.log("Created table 'users'.")
break
}
}
}
async function dbUpgrade (type, obj) {
switch (type) {
case 'mysql':
case 'pg': {
const knex = obj
await knex.schema.table('users', function (t) {
t.string('locale', 5).notNullable().defaultTo('ko_KR').collate('utf8_unicode_ci')
})
}
}
}
function runInstallDeps (extension, fullpath) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(path.join(fullpath, 'package.json'))) {
console.log(`[INFO] Cannot find package.json on ${extension}. Skipping.`)
resolve()
return
}
const out = ch.exec('yarn --prod', { cwd: fullpath }, (err, stdout, stderr) => {
if (err) reject(new Error('[ERROR] Failed to install modules for ' + extension + ': ' + err.stack))
resolve()
})
out.stdout.on('data', console.log)
out.stderr.on('data', console.log)
})
}