-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
76 lines (70 loc) · 2.4 KB
/
setup.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
const zlib = require('node:zlib')
const decompress = require('decompress')
const fs = require('node:fs')
const path = require('node:path')
const needle = require('needle')
console.log('[i] Setting up the databases')
const config = require('./config')
const { db } = require('./database')
const timezoneMigrations = path.resolve(__dirname, 'migrations', 'database-migrations.sql')
const timezoneSeeders = path.resolve(__dirname, 'migrations', 'database-seeders.sql')
const archivePath = path.resolve(__dirname, 'database', 'database-ip-archive.zip')
const unarchivedPath = path.resolve(__dirname, 'database', 'database-ip')
const binaryName = 'database-ip.bin'
const binaryPath = path.resolve(__dirname, 'database', 'database-ip', binaryName)
const databaseIpPath = path.resolve(__dirname, 'database', 'database-ip.bin')
const baseUrl = 'https://www.ip2location.com/download/'
const params = new URLSearchParams({
token: config.DOWNLOAD_TOKEN,
file: config.DATABASE_CODE,
})
console.log('[i] Running the migrations')
fs.readFileSync(timezoneMigrations)
.toString()
.split(';')
.map(migration => db.exec(migration))
console.log('[i] Running the seeders')
fs.readFileSync(timezoneSeeders)
.toString()
.split(';')
.map(seed => db.exec(seed))
const downloadUrl = `${baseUrl}?${params}`
console.log('[i] Downloading archive')
new Promise((resolve, reject) => {
needle.get(downloadUrl, { output: archivePath }, function (error, resp, body) {
if (error) {
return reject(error.message)
}
return resolve()
})
})
.catch(error => {
console.error(error)
throw new Error(error)
})
.then(() => {
console.log('[i] Extracting archive')
return decompress(archivePath, unarchivedPath, {
filter: file => {
console.log('[i] filtering', file.path)
return path.extname(file.path).toLowerCase() === '.bin'
},
map: file => {
console.log('[i] maping', file.path)
file.path = binaryName
return file
}
})
})
.catch(error => {
console.error(error)
throw new Error(error)
})
.then(() => new Promise(resolve => setTimeout(() => resolve(), 1000)))
.then(() => {
console.log('[i] Files cleanup ("MIMOP" -BBDaph)')
fs.copyFileSync(binaryPath, databaseIpPath)
fs.rmSync(unarchivedPath, { recursive: true })
fs.rmSync(archivePath)
console.log('[√] Setup completed')
})