-
Notifications
You must be signed in to change notification settings - Fork 1
/
freemail-cli
executable file
·87 lines (73 loc) · 1.75 KB
/
freemail-cli
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
#!/usr/bin/env node
const parser = require('meow');
const fs = require('fs');
const path = require('path');
const split = require('split');
const disposable = load('freemail/data/disposable.txt');
const free = extend(load('freemail/data/free.txt'), disposable);
const cli = parser(`
Usage
$ freemail-cli [options] <file>
Matches free emails, including disposable, by default
Options
-d, --disposable Only match disposable emails
-i, --inverse Print emails that don't match
Examples
$ freemail-cli ./path/to/file
$ cat ./path/to/file | freemail-cli
`, {
boolean: ['d', 'i'],
alias: {
d: 'disposable',
i: 'inverse'
}
});
const file = cli.input[0];
if (file && !isFile(file)) {
console.error('Invalid file path');
process.exit(1);
}
const readStream = file ? fs.createReadStream(file) : process.stdin;
readStream
.pipe(split())
.on('data', (line) => {
if (cli.flags.inverse ^ match(line)) console.log(line);
});
function load(path) {
return fs.readFileSync(require.resolve(path), 'utf8')
.split('\n')
.reduce((res, cur) => {
res[cur] = true;
return res;
}, {});
}
function isFile(path) {
try {
const stat = fs.lstatSync(path);
return !stat.isDirectory();
} catch (err) {
return false;
}
}
function match(line) {
const split = line.toLowerCase().split('@');
const domain = split[1] || split[0];
if (cli.flags.disposable) {
return !!disposable[domain];
} else if (cli.flags.free) {
return !!free[domain];
} else {
return !!disposable[domain] || !!free[domain];
}
}
function extend(a, b) {
const res = {};
var key;
for (key in a) {
res[key] = a[key];
}
for (key in b) {
res[key] = b[key];
}
return res;
}