Created
September 19, 2019 19:15
-
-
Save matepaiva/de1c8c6a1d827c462b4e17e2839fbc83 to your computer and use it in GitHub Desktop.
Compile all mjml files to html files
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 mjml = require('mjml'); | |
const { readFile, writeFile } = require('fs'); | |
const glob = require('glob'); | |
const { promisify } = require('util'); | |
const chalk = require('chalk'); | |
const readFileAsync = promisify(readFile); | |
const writeFileAsync = promisify(writeFile); | |
const globAsync = promisify(glob); | |
const log = { | |
// eslint-disable-next-line no-console | |
error: err => console.error(chalk.red(err)), | |
// eslint-disable-next-line no-console | |
success: err => console.log(chalk.green(err)), | |
}; | |
const main = async () => { | |
const filePaths = await globAsync('emails/**/*.mjml'); | |
const htmlFiles = await Promise.all( | |
filePaths.map(async path => { | |
try { | |
return { | |
path, | |
content: await readFileAsync(path, 'utf8').then( | |
content => mjml(content).html, | |
), | |
}; | |
} catch (error) { | |
log.error(`Error parsing file: ${path}: ${error}`); | |
return { path, content: undefined }; | |
} | |
}), | |
); | |
const result = await Promise.all( | |
htmlFiles | |
.filter(({ content }) => content) | |
.map(async ({ path, content }) => { | |
const htmlPath = path.replace('.mjml', '.html'); | |
await writeFileAsync(htmlPath, content, 'utf8'); | |
}), | |
); | |
log.success( | |
`- ${result.length} of ${filePaths.length} file(s) parsed with success.`, | |
); | |
log.error( | |
`- ${htmlFiles.filter(({ content }) => !content).length} of ${ | |
filePaths.length | |
} file(s) could not be parsed.`, | |
); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment