Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI init functionality #13

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { waitForAssets } from '../utils/watcloud-uri';
const program = new Command();

// Returns the email template with the given name and performs any necessary initialization.
async function getTemplate(template_name: string) {
async function getTemplate(template_name: string, props_array: any[] = []) {
const mod = require(`../emails/${template_name}`);
if (mod.init) {
await mod.init();
await Promise.all(props_array.map(mod.init));
}
await waitForAssets();

Expand All @@ -30,13 +30,13 @@ program
.option('-p, --pretty', 'Pretty print the output')
.option('-t, --text', 'Generate plain text output')
.action(async (template_name, options) => {
const template = await getTemplate(template_name);

let data = {};
if (options.data) {
data = JSON.parse(options.data);
}

const template = await getTemplate(template_name, [data]);

const out = await render(template(data), {
pretty: options.pretty,
plainText: options.text,
Expand All @@ -55,13 +55,13 @@ program
.option('-d, --data <data>', 'Data to pass to the template. Should be a JSON array of objects')
.option('-o, --output <output>', 'Output file. A JSON array of HTML and text emails will be written to this file')
.action(async (template_name, options) => {
const template = await getTemplate(template_name);

let data = [];
if (options.data) {
data = JSON.parse(options.data);
}

const template = await getTemplate(template_name, data);

const out = await Promise.all(data.map(async (d: any) => ({
html: await render(template(d)),
text: await render(template(d), { plainText: true }),
Expand Down