|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | import { MailParser } from 'mailparser'; |
4 | | -import { writeFileSync, createReadStream } from 'fs'; |
5 | | -import nomnom from 'nomnom' |
| 4 | +import { writeFileSync, createReadStream, readdir } from 'fs'; |
| 5 | +import { basename } from 'path'; |
| 6 | +import nomnom from 'nomnom'; |
6 | 7 |
|
7 | | -const mailparser = new MailParser(); |
8 | | -const opts = nomnom.option('infile', { |
| 8 | +const opts = nomnom |
| 9 | + .option('directory', { |
| 10 | + abbr: 'd', |
| 11 | + flag: true, |
| 12 | + help: 'Adds the ability to use directories instead of files' |
| 13 | + }) |
| 14 | + .option('infile', { |
9 | 15 | abbr: 'i', |
10 | 16 | required: true, |
11 | | - help: 'Specify input file' |
| 17 | + help: 'Specify input file or directory with -d flag' |
12 | 18 | }) |
13 | 19 | .option('outfile', { |
14 | 20 | abbr: 'o', |
15 | 21 | required: true, |
16 | | - help: 'Specify output file' |
| 22 | + help: 'Specify output file or directory with -d flag' |
17 | 23 | }) |
18 | 24 | .parse(); |
19 | 25 |
|
20 | | -mailparser.on('data', data => { |
21 | | - if (data.type === 'text') { |
22 | | - writeFileSync(opts.outfile, data.html); |
23 | | - } |
24 | | -}); |
| 26 | +if (opts.directory) { |
| 27 | + readdir(opts.infile, (err, files) => { |
| 28 | + if (err) console.error("Error:", err); |
| 29 | + else { |
| 30 | + files.forEach(file => { |
| 31 | + const fileName = basename(file, '.eml'); |
| 32 | + const mailparser = new MailParser(); |
| 33 | + |
| 34 | + mailparser.on('data', data => { |
| 35 | + if (data.type === 'text') { |
| 36 | + writeFileSync(`${opts.outfile}/${fileName}.html`, data.html); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + createReadStream(`${opts.infile}/${file}`).pipe(mailparser); |
| 41 | + }); |
| 42 | + } |
| 43 | + }); |
| 44 | +} else { |
| 45 | + const mailparser = new MailParser(); |
| 46 | + |
| 47 | + mailparser.on('data', data => { |
| 48 | + if (data.type === 'text') { |
| 49 | + writeFileSync(opts.outfile, data.html); |
| 50 | + } |
| 51 | + }); |
25 | 52 |
|
26 | | -createReadStream(opts.infile).pipe(mailparser); |
| 53 | + createReadStream(opts.infile).pipe(mailparser); |
| 54 | +} |
0 commit comments