Created
February 13, 2020 10:59
-
-
Save blvdmitry/1e4d5491df0cb9264094bbdb60ac6a0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const path = require('path'); | |
const fs = require('fs-extra'); | |
const sassExtract = require('sass-extract'); | |
const Handlebars = require('handlebars'); | |
const juice = require('juice'); | |
const templatesPath = path.resolve(__dirname, '../templates/'); | |
const partialsPath = path.resolve(__dirname, '../components/'); | |
const templateNames = fs.readdirSync(templatesPath); | |
const partialNames = fs.readdirSync(partialsPath); | |
let cssSource = ''; | |
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { | |
switch (operator) { | |
case '==': | |
return (v1 == v2) ? options.fn(this) : options.inverse(this); | |
case '===': | |
return (v1 === v2) ? options.fn(this) : options.inverse(this); | |
case '!=': | |
return (v1 != v2) ? options.fn(this) : options.inverse(this); | |
case '!==': | |
return (v1 !== v2) ? options.fn(this) : options.inverse(this); | |
case '<': | |
return (v1 < v2) ? options.fn(this) : options.inverse(this); | |
case '<=': | |
return (v1 <= v2) ? options.fn(this) : options.inverse(this); | |
case '>': | |
return (v1 > v2) ? options.fn(this) : options.inverse(this); | |
case '>=': | |
return (v1 >= v2) ? options.fn(this) : options.inverse(this); | |
case '&&': | |
return (v1 && v2) ? options.fn(this) : options.inverse(this); | |
case '||': | |
return (v1 || v2) ? options.fn(this) : options.inverse(this); | |
default: | |
return options.inverse(this); | |
} | |
}); | |
Handlebars.registerHelper('current-year', function () { | |
return new Date().getFullYear(); | |
}); | |
partialNames.forEach(partialName => { | |
// Register all partials | |
const partialPath = path.resolve(partialsPath, `${partialName}/${partialName}.hbs`); | |
const templateSource = fs.readFileSync(partialPath); | |
Handlebars.registerPartial(partialName, templateSource.toString()); | |
// Save partial css | |
const cssPath = path.resolve(partialsPath, `${partialName}/${partialName}.scss`); | |
if (!fs.existsSync(cssPath)) return; | |
const cssRendered = sassExtract.renderSync({ | |
file: cssPath, | |
}); | |
cssSource += `${cssRendered.css.toString()}\n`; | |
}); | |
// Generate output html using hbs template, saved css and json data files | |
templateNames.forEach(templateName => { | |
const templatePath = path.resolve(templatesPath, `${templateName}/template.hbs`); | |
const dataPath = path.resolve(templatesPath, `${templateName}/data`); | |
const templateSource = fs.readFileSync(templatePath); | |
const dataNames = fs.readdirSync(dataPath); | |
dataNames.forEach(filename => { | |
const jsonName = filename.split('.')[0]; | |
const outputPath = path.resolve(__dirname, `../../dist/emails/${templateName}/${jsonName}.html`); | |
const jsonPath = path.resolve(dataPath, filename); | |
const json = fs.readFileSync(jsonPath); | |
const template = Handlebars.compile(templateSource.toString()); | |
const data = JSON.parse(json); | |
data['styles'] = cssSource; | |
const outputHTML = template(data); | |
const output = juice(`${outputHTML}`); | |
fs.outputFile(outputPath, output); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment