Skip to content

Commit 864c752

Browse files
author
Dmitry Shirokov
committed
feat: Add an ability to download only selected databases
1 parent 8dac474 commit 864c752

3 files changed

Lines changed: 36 additions & 35 deletions

File tree

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const path = require("path");
1+
const path = require('path');
22

3-
const {selectedDbs} = require('./utils');
4-
const selected = selectedDbs();
3+
const { getSelectedDbs } = require('./utils');
4+
const selected = getSelectedDbs();
55

66
const makePath = (type) => path.resolve(__dirname, `dbs/GeoLite2-${type}.mmdb`);
77

8-
const paths = selected.reduce((a,c) => {
8+
const paths = selected.reduce((a, c) => {
99
a[c.toLowerCase()] = makePath(c);
1010
return a;
1111
}, {});
1212

1313
module.exports = {
14-
paths
14+
paths,
1515
};

scripts/postinstall.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const https = require('https');
33
const zlib = require('zlib');
44
const tar = require('tar');
55
const path = require('path');
6-
const {getLicense, selectedDbs} = require('../utils');
6+
const { getLicense, getSelectedDbs } = require('../utils');
77

88
let licenseKey;
99
try {
@@ -35,14 +35,10 @@ if (!licenseKey) {
3535
const link = (edition) =>
3636
`https://download.maxmind.com/app/geoip_download?edition_id=${edition}&license_key=${licenseKey}&suffix=tar.gz`;
3737

38-
39-
const selected = selectedDbs();
40-
const links = [
41-
'City',
42-
'Country',
43-
'ASN',
44-
].filter(e => selected.includes(e))
45-
.map(e => link(`GeoLite2-${e}`));
38+
const selected = getSelectedDbs();
39+
const links = ['City', 'Country', 'ASN']
40+
.filter((e) => selected.includes(e))
41+
.map((e) => link(`GeoLite2-${e}`));
4642

4743
const downloadPath = path.join(__dirname, '..', 'dbs');
4844

utils.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const path = require('path');
22
const fs = require('fs');
33

4-
const cwdPath = (file) => path.join(process.env['INIT_CWD'] || process.cwd(), file);
4+
const cwdPath = (file) =>
5+
path.join(process.env['INIT_CWD'] || process.cwd(), file);
56

67
const getConfig = () => {
78
const packageJsonFilename = cwdPath('package.json');
@@ -11,51 +12,55 @@ const getConfig = () => {
1112

1213
const getLicense = () => {
1314
const envKey = process.env.MAXMIND_LICENSE_KEY;
14-
if(envKey) return envKey;
15-
15+
if (envKey) return envKey;
16+
1617
const config = getConfig();
1718
const licenseKey = config['license-key'];
18-
if(licenseKey) return licenseKey;
19+
if (licenseKey) return licenseKey;
1920

2021
const configFile = config['license-file'];
21-
if(!configFile) return;
22+
if (!configFile) return;
2223

2324
const configFilePath = cwdPath(configFile);
24-
return fs.existsSync(configFilePath)
25-
? fs.readFileSync(configFilePath, 'utf8').trim()
26-
: undefined;
27-
}
25+
return fs.existsSync(configFilePath)
26+
? fs.readFileSync(configFilePath, 'utf8').trim()
27+
: undefined;
28+
};
2829

29-
const selectedDbs = () => {
30+
const getSelectedDbs = () => {
3031
const config = getConfig();
3132
const selected = config['selected-dbs'];
3233
const valids = ['City', 'Country', 'ASN'];
33-
if(!selected) return valids;
34-
if(!Array.isArray(selected)) {
34+
if (!selected) return valids;
35+
if (!Array.isArray(selected)) {
3536
console.error('selected-dbs property must have be an array.');
3637
process.exit(1);
3738
}
3839

39-
if(selected.length === 0) return valids;
40+
if (selected.length === 0) return valids;
4041

41-
if(selected.length > 3) {
42-
console.error('selected-dbs has too many values, there are only three valid values: City, Country, ASN.');
42+
if (selected.length > 3) {
43+
console.error(
44+
`selected-dbs has too many values, there are only three valid values: ${valids.join(', ')}.`
45+
);
4346
process.exit(1);
4447
}
4548

46-
for(const value of selected) {
49+
for (const value of selected) {
4750
const index = valids.indexOf(value);
48-
if(index === -1) {
49-
console.error(`Invalid value in selected-dbs: ${value}. The only valid values are: City, Country, ASN.`);
51+
if (index === -1) {
52+
console.error(
53+
`Invalid value in selected-dbs: ${value}. The only valid values are: ${valids.join(', ')}.`
54+
);
5055
process.exit(1);
5156
}
5257
}
5358

5459
return selected;
55-
}
60+
};
5661

5762
module.exports = {
5863
getConfig,
5964
getLicense,
60-
selectedDbs
61-
}
65+
getSelectedDbs,
66+
};

0 commit comments

Comments
 (0)