Skip to content

Commit 463c5eb

Browse files
committed
fix: compiled
1 parent a593988 commit 463c5eb

1 file changed

Lines changed: 78 additions & 9 deletions

File tree

dist/index.js

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50532,24 +50532,93 @@ var __webpack_exports__ = {};
5053250532
(() => {
5053350533
const core = __nccwpck_require__(2186);
5053450534
const SibApiV3Sdk = __nccwpck_require__(9168);
50535+
const path = __nccwpck_require__(1017);
50536+
const fs = __nccwpck_require__(7147);
50537+
const templateDir = core.getInput("templates");
50538+
const senderName = core.getInput("SENDER_NAME");
50539+
const senderEmail = core.getInput("SENDER_EMAIL");
50540+
const replyTo = core.getInput("REPLY_TO");
5053550541
const client = SibApiV3Sdk.ApiClient.instance;
5053650542
const apiKey = client.authentications["api-key"];
5053750543
apiKey.apiKey = core.getInput("SENDINBLUE_API_KEY");
5053850544
const api = new SibApiV3Sdk.TransactionalEmailsApi();
5053950545

50546+
async function getTemplatesNames() {
50547+
const dir = await fs.promises.opendir(templateDir);
50548+
const ret = [];
50549+
for await (const dirent of dir) {
50550+
const file = path.parse(dirent.name);
50551+
if (file.ext !== ".html") {
50552+
continue;
50553+
}
50554+
ret.push(file.name);
50555+
}
50556+
return ret;
50557+
}
50558+
5054050559
async function run() {
50541-
const templates = await getTemplates();
50542-
console.log("TEMPLATES", templates);
50560+
const templateNames = await getTemplatesNames();
50561+
const templates = await getTemplatesByName(templateNames);
50562+
Object.entries(templates).map(async ([templateName, template]) => {
50563+
const action = template ? updateTemplate : createTemplate;
50564+
console.log(
50565+
`${template ? "Updating" : "Creating"} ${templateName} template...`
50566+
);
50567+
const { error } = await action({
50568+
[templateName]: templates[templateName],
50569+
});
50570+
console.log(error ? `Error: ${error}` : "Success");
50571+
});
5054350572
}
5054450573

50545-
async function getTemplates() {
50546-
const opts = {
50547-
templateStatus: true,
50548-
limit: 50,
50549-
offset: 0,
50550-
};
50574+
async function buildTemplate(template) {
50575+
const [templateName] = Object.keys(template);
50576+
const data = fs
50577+
.readFileSync(`${templateDir}/${templateName}.html`, "utf8")
50578+
.toString();
50579+
const regex = /<!-- subject:([\s\S]*?) -->/gim;
50580+
const matches = regex.exec(data);
50581+
const subject = matches && matches[1] ? matches[1].trim() : templateName;
50582+
50583+
const smtpTemplate = new SibApiV3Sdk.CreateSmtpTemplate();
50584+
smtpTemplate.sender = { name: senderName, email: senderEmail };
50585+
smtpTemplate.templateName = templateName;
50586+
smtpTemplate.htmlContent = data;
50587+
smtpTemplate.subject = subject;
50588+
smtpTemplate.replyTo = replyTo;
50589+
smtpTemplate.isActive = true;
50590+
return smtpTemplate;
50591+
}
5055150592

50552-
return api.getSmtpTemplates(opts);
50593+
async function updateTemplate(template) {
50594+
const [currentTemplate] = Object.values(template);
50595+
const smtpTemplate = await buildTemplate(template);
50596+
try {
50597+
const req = await api.updateSmtpTemplate(currentTemplate.id, smtpTemplate);
50598+
return { success: true };
50599+
} catch (err) {
50600+
return { success: false, error: err };
50601+
}
50602+
}
50603+
50604+
async function createTemplate(template) {
50605+
const smtpTemplate = await buildTemplate(template);
50606+
try {
50607+
await api.createSmtpTemplate(smtpTemplate);
50608+
return { success: true };
50609+
} catch (err) {
50610+
return { success: false, error: err };
50611+
}
50612+
}
50613+
50614+
async function getTemplatesByName(names) {
50615+
const request = await api.getSmtpTemplates();
50616+
const { templates } = request;
50617+
return names.reduce((agg, x) => {
50618+
const template = templates?.find((t) => t.name === x);
50619+
agg[x] = template || false;
50620+
return agg;
50621+
}, {});
5055350622
}
5055450623

5055550624
run();

0 commit comments

Comments
 (0)