-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·66 lines (52 loc) · 1.91 KB
/
cli.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
#! /usr/bin/env node
const options = require('commander');
options.version('1.0.1');
options
.arguments('<src>')
.description('Retrieve all emails from a given file and saves them in a txt file.')
.option('-d, --dest <path>', 'Destination file path, if not set the file will be saved in ./emails-found.txt')
.action(function (src) {
options.src = src
});
options.parse(process.argv);
const path = require('path');
const fs = require('fs');
const untildify = require('untildify');
const chalk = require('chalk');
const log = console.log;
const logError = t => log(chalk.red(t) + "\n");
const logSuccess = t => log(chalk.green.bold(t) + "\n");
const logInfo = t => log('• ' + chalk.blueBright(t) + "\n");
if (!options.src) {
return options.outputHelp();
}
options.src = untildify(options.src);
options.dest = options.dest || path.dirname(options.src) + '/emails-found.txt';
log();
logInfo(`Reading ${options.src} ...`);
fs.readFile(untildify(options.src), 'utf8', function (err, contents) {
if (err) {
if (err.code === 'ENOENT'){
return logError(`File not found: "${options.src}"`);
}
return logError(err.message);
}
const regex = /[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/img;
let emails = contents.match(regex);
emails = Array.from(new Set(
emails.map(e => e.toLowerCase()).filter(e => e).sort()
));
let emailsCount = emails.length;
if (!emailsCount) {
logError(`No emails found in ${options.src}!`);
}
if (emailsCount) {
logInfo(`Writing emails to ${options.dest} ...`);
fs.writeFile(options.dest, emails.join('\n'), function (err) {
if (err) {
return console.log(err);
}
logSuccess(`Saved ${emailsCount} emails in ${options.dest}`);
});
}
});