|
1 | 1 | const fs = require('fs'); |
2 | 2 | const https = require('https'); |
3 | 3 | const zlib = require('zlib'); |
4 | | -const path = require('path'); |
5 | 4 | const tar = require('tar'); |
6 | | - |
7 | | -const getConfig = () => { |
8 | | - const packageJsonFilename = path.join( |
9 | | - process.env['INIT_CWD'], |
10 | | - 'package.json' |
11 | | - ); |
12 | | - const packageJson = JSON.parse(fs.readFileSync(packageJsonFilename, 'utf8')); |
13 | | - return packageJson['geolite2'] || {}; |
14 | | -}; |
15 | | - |
16 | | -const keyLoaders = [ |
17 | | - () => process.env.MAXMIND_LICENSE_KEY, |
18 | | - () => getConfig()['license-key'], |
19 | | - () => { |
20 | | - const configFile = getConfig()['license-file']; |
21 | | - if (!configFile) return; |
22 | | - |
23 | | - const filepath = path.join(process.env['INIT_CWD'], configFile); |
24 | | - return fs.existsSync(filepath) |
25 | | - ? fs.readFileSync(filepath, 'utf8').trim() |
26 | | - : undefined; |
27 | | - }, |
28 | | -]; |
| 5 | +const path = require('path'); |
| 6 | +const {getLicense, selectedDbs} = require('../utils'); |
29 | 7 |
|
30 | 8 | let licenseKey; |
31 | | - |
32 | 9 | try { |
33 | | - let i = 0; |
34 | | - while (i < keyLoaders.length) { |
35 | | - licenseKey = keyLoaders[i++](); |
36 | | - if (licenseKey) break; |
37 | | - } |
| 10 | + licenseKey = getLicense(); |
38 | 11 | } catch (e) { |
39 | 12 | console.error('geolite2: Error retrieving Maxmind License Key'); |
40 | 13 | console.error(e.message); |
@@ -62,11 +35,14 @@ if (!licenseKey) { |
62 | 35 | const link = (edition) => |
63 | 36 | `https://download.maxmind.com/app/geoip_download?edition_id=${edition}&license_key=${licenseKey}&suffix=tar.gz`; |
64 | 37 |
|
| 38 | + |
| 39 | +const selected = selectedDbs(); |
65 | 40 | const links = [ |
66 | | - link('GeoLite2-City'), |
67 | | - link('GeoLite2-Country'), |
68 | | - link('GeoLite2-ASN'), |
69 | | -]; |
| 41 | + 'City', |
| 42 | + 'Country', |
| 43 | + 'ASN', |
| 44 | +].filter(e => selected.includes(e)) |
| 45 | + .map(e => link(`GeoLite2-${e}`)); |
70 | 46 |
|
71 | 47 | const downloadPath = path.join(__dirname, '..', 'dbs'); |
72 | 48 |
|
|
0 commit comments