@fusionauth/cli
Version:
114 lines (113 loc) • 6.12 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
import { Command } from "@commander-js/extra-typings";
import { validate as isUUID } from "uuid";
import chalk from "chalk";
import { lstat, readdir, readFile, writeFile } from "fs/promises";
import { compile } from "html-to-text";
const htmlToText = compile({
wordwrap: false,
selectors: [
{ selector: 'p', options: { leadingLineBreaks: 1, trailingLineBreaks: 1 } },
]
});
// noinspection JSUnusedGlobalSymbols
export const emailHtmlToText = new Command('email:html-to-text')
.description('Find missing text templates and create them from the html templates')
.argument('[emailTemplateId]', 'The email template id to convert. If not provided, all email templates will be converted')
.option('-o, --output <output>', 'The output directory', './emails/')
.action((emailTemplateId, { output }) => __awaiter(void 0, void 0, void 0, function* () {
var _a, e_1, _b, _c, _d, e_2, _e, _f, _g, e_3, _h, _j;
if (!emailTemplateId) {
console.log(`Converting all email templates in ${output}`);
}
else {
console.log(`Converting email template ${emailTemplateId} in ${output}`);
}
const emailTemplateIds = [];
if (!emailTemplateId) {
const files = yield readdir(output);
try {
for (var _k = true, files_1 = __asyncValues(files), files_1_1; files_1_1 = yield files_1.next(), _a = files_1_1.done, !_a; _k = true) {
_c = files_1_1.value;
_k = false;
const file = _c;
// Validate directory
if ((yield lstat(`${output}/${file}`)).isDirectory() && isUUID(file)) {
emailTemplateIds.push(file);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_k && !_a && (_b = files_1.return)) yield _b.call(files_1);
}
finally { if (e_1) throw e_1.error; }
}
}
try {
for (var _l = true, emailTemplateIds_1 = __asyncValues(emailTemplateIds), emailTemplateIds_1_1; emailTemplateIds_1_1 = yield emailTemplateIds_1.next(), _d = emailTemplateIds_1_1.done, !_d; _l = true) {
_f = emailTemplateIds_1_1.value;
_l = false;
const templateId = _f;
const emailTemplateDirectory = `${output}/${templateId}/`;
console.log(`Converting email template ${templateId}`);
// Check default locale
const htmlContent = yield readFile(`${emailTemplateDirectory}/body.html`, 'utf-8');
const textContent = yield readFile(`${emailTemplateDirectory}/body.txt`, 'utf-8');
if (htmlContent.length && !textContent.length) {
console.log(`Creating text template for ${templateId}`);
yield writeFile(`${emailTemplateDirectory}/body.txt`, htmlToText(htmlContent));
}
// Check locales
const locales = yield readdir(emailTemplateDirectory);
try {
for (var _m = true, locales_1 = (e_3 = void 0, __asyncValues(locales)), locales_1_1; locales_1_1 = yield locales_1.next(), _g = locales_1_1.done, !_g; _m = true) {
_j = locales_1_1.value;
_m = false;
const locale = _j;
// Validate directory
const localeDirectory = `${emailTemplateDirectory}/${locale}`;
if ((yield lstat(`${emailTemplateDirectory}/${locale}`)).isDirectory()) {
const localizedHtmlContent = yield readFile(`${localeDirectory}/body.html`, 'utf-8');
const localizedTextContent = yield readFile(`${localeDirectory}/body.txt`, 'utf-8');
if (localizedHtmlContent.length && !localizedTextContent.length) {
console.log(`Creating text template for ${templateId} in ${locale}`);
yield writeFile(`${localeDirectory}/body.txt`, htmlToText(localizedHtmlContent));
}
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (!_m && !_g && (_h = locales_1.return)) yield _h.call(locales_1);
}
finally { if (e_3) throw e_3.error; }
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_l && !_d && (_e = emailTemplateIds_1.return)) yield _e.call(emailTemplateIds_1);
}
finally { if (e_2) throw e_2.error; }
}
console.log(chalk.green(`Finished converting email template(s)`));
}));