Skip to content

Commit

Permalink
Feat: Template engine class to write previews out to files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gum-Joe committed Jul 30, 2024
1 parent a41320e commit ba67011
Show file tree
Hide file tree
Showing 21 changed files with 11,625 additions and 10,894 deletions.
21,918 changes: 11,187 additions & 10,731 deletions package-lock.json

Large diffs are not rendered by default.

85 changes: 43 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
{
"name": "docsoc-tools",
"version": "0.0.0",
"license": "MIT",
"scripts": {},
"private": true,
"devDependencies": {
"@monodon/rust": "^1.4.0",
"@nx-tools/container-metadata": "^6.0.1",
"@nx-tools/nx-container": "^6.0.1",
"@nx/esbuild": "19.4.3",
"@nx/eslint": "19.4.3",
"@nx/eslint-plugin": "19.4.3",
"@nx/jest": "19.4.3",
"@nx/js": "19.4.3",
"@nx/node": "^19.4.3",
"@swc-node/register": "~1.9.1",
"@swc/core": "~1.5.7",
"@swc/helpers": "~0.5.11",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/jest": "^29.4.0",
"@types/node": "~18.16.9",
"@typescript-eslint/eslint-plugin": "^7.3.0",
"@typescript-eslint/parser": "^7.3.0",
"esbuild": "^0.19.2",
"eslint": "~8.57.0",
"eslint-config-prettier": "^9.0.0",
"jest": "^29.4.1",
"jest-environment-node": "^29.4.1",
"nx": "19.4.3",
"prettier": "^2.6.2",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"typescript": "~5.4.2"
},
"workspaces": [
"packages/*",
"packages/**/*"
],
"dependencies": {
"axios": "^1.6.0",
"tslib": "^2.3.0"
}
"name": "docsoc-tools",
"version": "0.0.0",
"license": "MIT",
"scripts": {},
"private": true,
"devDependencies": {
"@monodon/rust": "^1.4.0",
"@nx-tools/container-metadata": "^6.0.1",
"@nx-tools/nx-container": "^6.0.1",
"@nx/esbuild": "19.4.3",
"@nx/eslint": "19.4.3",
"@nx/eslint-plugin": "19.4.3",
"@nx/jest": "^19.4.3",
"@nx/js": "19.4.3",
"@nx/node": "^19.4.3",
"@swc-node/register": "~1.9.1",
"@swc/core": "~1.5.7",
"@swc/helpers": "~0.5.11",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/jest": "^29.4.0",
"@types/node": "~18.16.9",
"@typescript-eslint/eslint-plugin": "^7.3.0",
"@typescript-eslint/parser": "^7.3.0",
"esbuild": "^0.19.2",
"eslint": "~8.57.0",
"eslint-config-prettier": "^9.0.0",
"jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"jest-environment-node": "^29.4.1",
"nx": "19.4.3",
"prettier": "^2.6.2",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"typescript": "~5.4.2"
},
"workspaces": [
"packages/*",
"packages/**/*"
],
"dependencies": {
"axios": "^1.6.0",
"tslib": "^2.3.0"
}
}
3 changes: 3 additions & 0 deletions packages/email/docsoc-mail-merge/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
7 changes: 7 additions & 0 deletions packages/email/docsoc-mail-merge/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable */
export default {
displayName: "docsoc-mail-merge",
preset: "../../../jest.preset.js",
coverageDirectory: "../../../coverage/packages/email/docsoc-mail-merge",
testMatch: ["<rootDir>/test/**/*.ts"],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- DoCSoc Mail Merge Wrapper - used to wrap results rendered from Markdown -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>Dear Kishan,</p>
<p>I hope this email finds you well. <strong>I am writing</strong> to you to inform you about the upcoming event that we are hosting.</p>
<p>We would like to invite you to join us and participate in the event. We believe that your presence will add value to the event and we would be honored to have you with us.</p>
<p>Kind regards,<br>
Kishan Sambhi (he/him)<br>
DoCSoc Vice President<br>
<signature /></p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Dear Kishan,

I hope this email finds you well. **I am writing** to you to inform you about the upcoming event that we are hosting.

We would like to invite you to join us and participate in the event. We believe that your presence will add value to the event and we would be honored to have you with us.

Kind regards,
Kishan Sambhi (he/him)
DoCSoc Vice President
<signature />
71 changes: 37 additions & 34 deletions packages/email/docsoc-mail-merge/package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
{
"name": "docsoc-mail-merge",
"version": "0.0.1",
"dependencies": {
"dotenv": "^16.4.5",
"email-validator": "^2.0.4",
"html-to-text": "^9.0.5",
"inquirer": "^10.1.4",
"markdown-it": "^14.1.0",
"mkdirp": "^3.0.1",
"nodemailer": "^6.9.14",
"nunjucks": "^3.2.4",
"tslib": "^2.3.0"
},
"scripts": {
"start": "ts-node src/cli.ts"
},
"bin": "./dist/cli.js",
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true,
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/csv-parse": "^1.2.2",
"@types/dotenv": "^8.2.0",
"@types/email-validator": "^1.0.6",
"@types/html-to-text": "^9.0.4",
"@types/markdown-it": "^14.1.1",
"@types/nodemailer": "^6.4.15",
"@types/nunjucks": "^3.2.6",
"@types/winston": "^2.4.4",
"csv-parse": "^5.5.6",
"ts-node": "^10.9.2"
}
"name": "docsoc-mail-merge",
"version": "0.0.1",
"dependencies": {
"dotenv": "^16.4.5",
"email-validator": "^2.0.4",
"html-to-text": "^9.0.5",
"inquirer": "^10.1.4",
"markdown-it": "^14.1.0",
"mkdirp": "^3.0.1",
"nodemailer": "^6.9.14",
"nunjucks": "^3.2.4",
"tslib": "^2.3.0",
"unique-names-generator": "^4.7.1"
},
"scripts": {
"start": "ts-node src/cli.ts",
"test": "jest"
},
"bin": "./dist/cli.js",
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true,
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/csv-parse": "^1.2.2",
"@types/dotenv": "^8.2.0",
"@types/email-validator": "^1.0.6",
"@types/html-to-text": "^9.0.4",
"@types/markdown-it": "^14.1.1",
"@types/nodemailer": "^6.4.15",
"@types/nunjucks": "^3.2.6",
"@types/winston": "^2.4.4",
"csv-parse": "^5.5.6",
"jest-mock-extended": "^3.0.7",
"ts-node": "^10.9.2"
}
}
36 changes: 22 additions & 14 deletions packages/email/docsoc-mail-merge/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { parse } from "csv-parse";
import "dotenv/config";
import { promises as fs } from "fs";
import markdownit from "markdown-it";
import { mkdirp } from "mkdirp";
import nunjucks from "nunjucks";
// load .env
import { join } from "path";

import packageJSON from "../package.json";
import { ENGINES_MAP } from "./engines";
import { TemplatePreview } from "./engines/types";
import { getFileNameSchemeInteractively } from "./interactivity/getFileNameSchemeInteractively";
import getRunNameInteractively from "./interactivity/getRunNameInteractively";
import mapInteractive from "./interactivity/mapInteractive";
import { stopIfCriticalFsError } from "./util/files";
import createLogger from "./util/logger";
import mapInteractive from "./util/mapInteractive";
import { CliOptions } from "./util/types";

const logger = createLogger("docsoc");
Expand All @@ -21,12 +21,14 @@ const opts: CliOptions = {
csvFile: "./data/names.csv",
templateOptions: {
templatePath: "./templates/TEMPLATE.md.njk",
rootHtmlTemplate: "./templates/wrappr.html.njk",
rootHtmlTemplate: "./templates/wrapper.html.njk",
},
templateEngine: "nunjucks",
output: "./output",
};

const ADDITIONAL_FIELDS = ["email"];

async function main(opts: CliOptions) {
logger.info("DoCSoc Mail Merge");
logger.info(`v${packageJSON.version}`);
Expand Down Expand Up @@ -54,36 +56,39 @@ async function main(opts: CliOptions) {

// 4: Map to template
logger.info("Loading template...");
const EngineClass: any = ENGINES_MAP[opts.templateEngine];
const EngineClass = ENGINES_MAP[opts.templateEngine];
if (!EngineClass) {
logger.error(`Invalid template engine: ${opts.templateEngine}`);
throw new Error(`Invalid template engine: ${opts.templateEngine}`);
}
const engine = new EngineClass();
await engine.loadTemplate(opts.templateOptions["templatePath"]);
const engine = new EngineClass(opts.templateOptions);
await engine.loadTemplate();

// 5: Extract template fields
logger.info("Extracting template fields...");
const templateFields: Set<string> = engine.extractFields();
const templateFields = engine.extractFields();
logger.info(`Fields found: ${Array.from(templateFields).join(", ")}`);

// 6: Map CSV fields to template interactively
logger.info("Mapping CSV fields to template interactively");
const fieldsMapCSVtoTemplate = await mapInteractive(templateFields, headers);
const fieldsMapCSVtoTemplate = await mapInteractive(
new Set([...Array.from(templateFields), ...ADDITIONAL_FIELDS]),
headers,
);

// 7: Ask what to name files using
const fileNamer: (record: any) => string = await getFileNameSchemeInteractively(fields, records);
const fileNamer = await getFileNameSchemeInteractively(headers, records);

// 7: Render intermediate results
logger.info("Rendering template previews/intermediates...");
const previews: [TemplatePreview, any][] = await Promise.all(
records.map((csvRecord) => {
const previews: [TemplatePreview, Record<string, any>][] = await Promise.all(
records.map(async (csvRecord) => {
const preparedRecord = Object.fromEntries(
Object.entries(csvRecord).map(([key, value]) => {
return [fieldsMapCSVtoTemplate.get(key) ?? key, value];
}),
);
return [engine.renderPreview(preparedRecord), csvRecord];
return [await engine.renderPreview(preparedRecord), csvRecord];
}),
);
// 7: Write to file
Expand All @@ -97,7 +102,10 @@ async function main(opts: CliOptions) {
previews.flatMap(async ([previews, record]) =>
previews.map(async (preview) => {
const fileName = fileNamer(record);
await fs.writeFile(`./previews/${fileName}__${opts.templateEngine}__${preview}`, preview.content);
await fs.writeFile(
join(previewsRoot, `${fileName}__${opts.templateEngine}__${preview.name}`),
preview.content,
);
}),
),
);
Expand Down
10 changes: 4 additions & 6 deletions packages/email/docsoc-mail-merge/src/engines/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
* Contains the different templating engines mail merge supports
* @module engines
*/

import { TemplateEngine } from "./types";
import nunjucksEngine from "./nunjucks";
import { TemplateEngine, TemplateEngineConstructor } from "./types";


export const ENGINES_MAP: Record<string, TemplateEngine> = {
nunjucks: nunjucksEngine
}
export const ENGINES_MAP: Record<string, TemplateEngineConstructor> = {
nunjucks: nunjucksEngine,
};
Loading

0 comments on commit ba67011

Please sign in to comment.