@k03mad/ip2geo 8.1.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/cli.js +18 -7
- package/app/helpers/cache.js +13 -4
- package/package.json +1 -1
package/app/cli.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import {log, logErrorExit} from '@k03mad/simple-log';
|
|
4
|
+
import chalk from 'chalk';
|
|
4
5
|
|
|
5
6
|
import {DEFAULT_CACHE_FILE_DIR, DEFAULT_CACHE_FILE_NEWLINE, DEFAULT_CACHE_FILE_SEPARATOR, ip2geo} from './api.js';
|
|
6
7
|
import {pruneCache} from './helpers/cache.js';
|
|
7
8
|
import {codeText, nameText} from './helpers/colors.js';
|
|
8
9
|
|
|
10
|
+
const {blue, red, green} = chalk;
|
|
11
|
+
|
|
9
12
|
const args = process.argv.slice(2);
|
|
10
13
|
const argsExtra = args.filter(arg => !arg.startsWith('-'));
|
|
11
14
|
|
|
@@ -42,18 +45,26 @@ if (isPrune) {
|
|
|
42
45
|
const cacheFolder = argsExtra[0] || DEFAULT_CACHE_FILE_DIR;
|
|
43
46
|
|
|
44
47
|
try {
|
|
45
|
-
const {duplicates,
|
|
48
|
+
const {duplicates, empty, longLinesFiles} = await pruneCache(
|
|
46
49
|
cacheFolder,
|
|
47
50
|
DEFAULT_CACHE_FILE_SEPARATOR,
|
|
48
51
|
DEFAULT_CACHE_FILE_NEWLINE,
|
|
49
52
|
);
|
|
50
53
|
|
|
51
|
-
|
|
52
|
-
`Removed duplicate cache entries: ${duplicates}`,
|
|
53
|
-
`Removed empty cache entries: ${
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
const message = [
|
|
55
|
+
`Removed duplicate cache entries: ${green(duplicates)}`,
|
|
56
|
+
`Removed empty cache entries: ${green(empty)}`,
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
if (longLinesFiles.size > 0) {
|
|
60
|
+
message.push(
|
|
61
|
+
'',
|
|
62
|
+
red('Something went wrong with these cache files (lines too long):'),
|
|
63
|
+
...[...longLinesFiles].map(elem => blue(`— ${elem}`)),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
log(message);
|
|
57
68
|
} catch (err) {
|
|
58
69
|
if (err.code !== 'ENOENT') {
|
|
59
70
|
logErrorExit(err);
|
package/app/helpers/cache.js
CHANGED
|
@@ -170,6 +170,7 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
|
|
|
170
170
|
|
|
171
171
|
let duplicates = 0;
|
|
172
172
|
let empty = 0;
|
|
173
|
+
const longLinesFiles = new Set();
|
|
173
174
|
|
|
174
175
|
await Promise.all(files.map(async file => {
|
|
175
176
|
const fullFilePath = path.join(cacheDir, file);
|
|
@@ -179,15 +180,23 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
|
|
|
179
180
|
|
|
180
181
|
const dataArrRemoveEmpty = dataArr.filter(elem => {
|
|
181
182
|
const splitted = elem.split(cacheFileSeparator);
|
|
183
|
+
|
|
184
|
+
if (splitted.length > outputKeys.length) {
|
|
185
|
+
longLinesFiles.add(fullFilePath);
|
|
186
|
+
}
|
|
187
|
+
|
|
182
188
|
return splitted.filter(Boolean).length > 1;
|
|
183
189
|
});
|
|
184
190
|
|
|
185
|
-
const uniq = [...new Set(dataArrRemoveEmpty)]
|
|
186
|
-
|
|
191
|
+
const uniq = [...new Set(dataArrRemoveEmpty)]
|
|
192
|
+
.sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
|
|
193
|
+
|
|
194
|
+
const fileContent = uniq.join(cacheFileNewline).trim();
|
|
195
|
+
await (fileContent ? fs.writeFile(fullFilePath, fileContent) : fs.rm(fullFilePath));
|
|
187
196
|
|
|
188
|
-
duplicates += dataArr.length - uniq.length;
|
|
189
197
|
empty += dataArr.length - dataArrRemoveEmpty.length;
|
|
198
|
+
duplicates += dataArrRemoveEmpty.length - uniq.length;
|
|
190
199
|
}));
|
|
191
200
|
|
|
192
|
-
return {duplicates, empty};
|
|
201
|
+
return {duplicates, empty, longLinesFiles};
|
|
193
202
|
};
|