-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Dependency inversion on generator
- Loading branch information
Showing
17 changed files
with
425 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export const DOCSOC_DEFAULT_FROM_LINE = `"DoCSoc" <[email protected]>`; | ||
|
||
/** Expected names of requried email fields in CSVRecords */ | ||
export const CSV_DEFAULT_FIELD_NAMES = { | ||
/** Expected names of requried email fields in records to data merge on */ | ||
export const DEFAULT_FIELD_NAMES = { | ||
to: "email", | ||
subject: "subject", | ||
cc: "cc", | ||
bcc: "bcc", | ||
}; | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type EmailString = `${string}@${string}`; | ||
export type FromEmail = `"${string}" <${EmailString}>`; | ||
|
||
export type RawCSVRecord = Record<string, unknown>; | ||
export type MappedCSVRecord = Record<string, unknown>; | ||
export type RawRecord = Record<string, unknown>; | ||
export type MappedRecord = Record<string, unknown>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { RawRecord } from "@docsoc/libmailmerge"; | ||
import { createLogger } from "@docsoc/util"; | ||
import { parse } from "csv-parse"; | ||
import fs from "fs/promises"; | ||
|
||
/** | ||
* Generic way of loading data in to do a data merge on. | ||
* | ||
* All you need to do is implement the `loadRecords` method, that resolves | ||
* to a set of headers and records. | ||
* | ||
* Headers are a set of strings, and records are an array of objects where the keys are the headers and are strings | ||
* | ||
* For an example, see {@link CSVBackend} | ||
* | ||
*/ | ||
export interface DataSource { | ||
loadRecords: () => Promise<{ | ||
headers: Set<string>; | ||
records: RawRecord[]; | ||
}>; | ||
} | ||
|
||
const logger = createLogger("csv"); | ||
|
||
export class CSVBackend implements DataSource { | ||
constructor(private csvFile: string) {} | ||
async loadRecords(): Promise<{ headers: Set<string>; records: RawRecord[] }> { | ||
// Load CSV | ||
logger.info("Loading CSV..."); | ||
const csvRaw = await fs.readFile(this.csvFile, "utf-8"); | ||
logger.debug("Parsing & loading CSV..."); | ||
const csvParsed = parse(csvRaw, { columns: true }); | ||
const records: RawRecord[] = []; | ||
for await (const record of csvParsed) { | ||
records.push(record); | ||
} | ||
logger.info(`Loaded ${records.length} records`); | ||
logger.debug("Extracting headers from first record's keys..."); | ||
if (records.length === 0) { | ||
logger.error("No records found in CSV"); | ||
throw new Error("No records found in CSV"); | ||
} | ||
const headers = new Set<string>(Object.keys(records[0])); | ||
logger.info(`Headers: ${Object.keys(records[0]).join(", ")}`); | ||
return { headers, records }; | ||
} | ||
} |
Oops, something went wrong.