@@ -2,35 +2,23 @@ import { createLogger } from "@docsoc/util";
22import chalk from "chalk" ;
33// Load dotenv
44import "dotenv/config" ;
5- import { promises as fs } from "fs" ;
65import readlineSync from "readline-sync" ;
76
87import { ENGINES_MAP } from "../engines/index.js" ;
98import { TemplateEngineConstructor } from "../engines/types.js" ;
109import Mailer from "../mailer/mailer.js" ;
10+ import { InlineImagesSpec , loadInlineImageJSON } from "../util/inline-images.js" ;
1111import { EmailString , FromEmail } from "../util/types.js" ;
1212import { StorageBackend , MergeResultWithMetadata , PostSendActionMode } from "./storage/types.js" ;
1313
14- /** See https://www.nodemailer.com/message/embedded-images/ */
15- export interface InlineImage {
16- /** Filename to attach to email as */
17- filename : string ;
18- /** Path to file relative to CWD */
19- path : string ;
20- /** Content ID to use in the email */
21- cid : string ;
22- }
23-
24- export type InlineImagesSpec = InlineImage [ ] ;
25-
2614interface SendEmailsOptions {
2715 /** Time to sleep between sending emails to prevent hitting rate limits */
2816 sleepBetween ?: number ;
2917 /** Only send this many emails (i.e. the first X emails) */
3018 onlySend ?: number ;
3119 /** Send the top {@link onlySend} emails to this email as a test */
3220 testSendTo ?: EmailString ;
33- /** Path to JSON file conforming to {@link InlineImagesSpec} with attachments */
21+ /** Path to JSON file conforming to a {@link InlineImagesSpec} with files to attach for use as inline images. */
3422 inlineImages ?: string ;
3523}
3624
@@ -46,7 +34,7 @@ const DEFAULT_SLEEP_BETWEEN = 0;
4634 * @param enginesMap Map of engine names to engine constructors, as we need to ask the engine what the HTML is to send from the result
4735 * @param disablePrompt If true, will not prompt the user before sending emails. Defaults to false (will prompt)
4836 * @param logger Logger to use for logging
49- * @param options
37+ * @param options Other options
5038 */
5139export async function sendEmails (
5240 storageBackend : StorageBackend ,
@@ -70,31 +58,7 @@ export async function sendEmails(
7058 let inlineImages : InlineImagesSpec = [ ] ;
7159 if ( options . inlineImages ) {
7260 logger . info ( "Loading inline images..." ) ;
73- inlineImages = JSON . parse ( await fs . readFile ( options . inlineImages , "utf-8" ) ) ;
74- // Validate the inline images
75- if ( ! Array . isArray ( inlineImages ) ) {
76- logger . error (
77- "Invalid inline images - must be an array of InlineImage objects. See https://www.nodemailer.com/message/embedded-images/ for format." ,
78- ) ;
79- throw new Error ( "Invalid inline images - must be an array of InlineImage objects." ) ;
80- }
81- logger . info ( `Loaded ${ inlineImages . length } inline images.` ) ;
82- // Check all the paths are valid (accessble) & have a cid & filename proprty
83- for ( const image of inlineImages ) {
84- if (
85- typeof image . filename !== "string" ||
86- typeof image . path !== "string" ||
87- typeof image . cid !== "string"
88- ) {
89- logger . error (
90- "Invalid inline image - must have a filename, path, and cid property. Got " ,
91- image ,
92- ) ;
93- throw new Error (
94- `Invalid inline image - must have a filename, path, and cid property. Got ${ image } ` ,
95- ) ;
96- }
97- }
61+ inlineImages = await loadInlineImageJSON ( options . inlineImages , logger ) ;
9862 }
9963
10064 // 1: Load data
0 commit comments