forked from amitevski/eml_to_html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
executable file
·54 lines (47 loc) · 1.3 KB
/
parse.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
#!/usr/bin/env node
import { MailParser } from 'mailparser';
import { writeFileSync, createReadStream, readdir } from 'fs';
import { basename } from 'path';
import nomnom from 'nomnom';
const opts = nomnom
.option('directory', {
abbr: 'd',
flag: true,
help: 'Adds the ability to use directories instead of files'
})
.option('infile', {
abbr: 'i',
required: true,
help: 'Specify input file or directory with -d flag'
})
.option('outfile', {
abbr: 'o',
required: true,
help: 'Specify output file or directory with -d flag'
})
.parse();
if (opts.directory) {
readdir(opts.infile, (err, files) => {
if (err) console.error("Error:", err);
else {
files.forEach(file => {
const fileName = basename(file, '.eml');
const mailparser = new MailParser();
mailparser.on('data', data => {
if (data.type === 'text') {
writeFileSync(`${opts.outfile}/${fileName}.html`, data.html);
}
});
createReadStream(`${opts.infile}/${file}`).pipe(mailparser);
});
}
});
} else {
const mailparser = new MailParser();
mailparser.on('data', data => {
if (data.type === 'text') {
writeFileSync(opts.outfile, data.html);
}
});
createReadStream(opts.infile).pipe(mailparser);
}