marilena
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -10,20 +10,45 @@ "use strict"; | ||
const node_color_log_1 = __importDefault(require("node-color-log")); | ||
const js_yaml_1 = __importDefault(require("js-yaml")); | ||
function default_1(options) { | ||
const { inputFolder } = options.config; | ||
const { inputFolder, templateOptions } = options.config; | ||
const locale = options.locale; | ||
const variablesType = templateOptions?.variablesType || "json"; | ||
let emailVariables = {}; | ||
let common = {}; | ||
try { | ||
common = JSON.parse(fs_1.default.readFileSync(path_1.default.join(inputFolder, `${const_1.FILE_NAME_COMMON_VARIABLES}-${locale}.json`), "utf-8")); | ||
switch (variablesType) { | ||
case "json": { | ||
try { | ||
common = JSON.parse(fs_1.default.readFileSync(path_1.default.join(inputFolder, `${const_1.FILE_NAME_COMMON_VARIABLES}-${locale}.json`), "utf-8")); | ||
} | ||
catch (e) { | ||
node_color_log_1.default.error(e); | ||
node_color_log_1.default.error(`cannot load common variables for email ${options.emailName} ${options.locale}. Please create ${const_1.FILE_NAME_COMMON_VARIABLES}-${locale}.json file under ${inputFolder}`); | ||
} | ||
try { | ||
emailVariables = JSON.parse(fs_1.default.readFileSync(path_1.default.join(inputFolder, options.emailName, locale, `${const_1.FILE_NAME_EMAIL_VARIABLES}.json`), "utf-8")); | ||
} | ||
catch (e) { | ||
node_color_log_1.default.error(e); | ||
node_color_log_1.default.error(`cannot load variables for email ${options.emailName} ${options.locale}. Please create ${const_1.FILE_NAME_EMAIL_VARIABLES}.json file under ${inputFolder}/${options.emailName}/${options.locale}`); | ||
} | ||
break; | ||
} | ||
case "yml": { | ||
try { | ||
common = js_yaml_1.default.load(fs_1.default.readFileSync(path_1.default.join(inputFolder, `${const_1.FILE_NAME_COMMON_VARIABLES}-${locale}.yml`), "utf-8")); | ||
} | ||
catch (e) { | ||
node_color_log_1.default.error(e); | ||
node_color_log_1.default.error(`cannot load common variables for email ${options.emailName} ${options.locale}. Please create ${const_1.FILE_NAME_COMMON_VARIABLES}-${locale}.yml file under ${inputFolder}`); | ||
} | ||
try { | ||
emailVariables = js_yaml_1.default.load(fs_1.default.readFileSync(path_1.default.join(inputFolder, options.emailName, locale, `${const_1.FILE_NAME_EMAIL_VARIABLES}.yml`), "utf-8")); | ||
} | ||
catch (e) { | ||
node_color_log_1.default.error(e); | ||
node_color_log_1.default.error(`cannot load variables for email ${options.emailName} ${options.locale}. Please create ${const_1.FILE_NAME_EMAIL_VARIABLES}.yml file under ${inputFolder}/${options.emailName}/${options.locale}`); | ||
} | ||
break; | ||
} | ||
} | ||
catch (e) { | ||
node_color_log_1.default.error(`cannot loading common variables for email ${options.emailName} ${options.locale}. Please create ${const_1.FILE_NAME_COMMON_VARIABLES}-${locale} file under ${inputFolder}`); | ||
} | ||
try { | ||
emailVariables = JSON.parse(fs_1.default.readFileSync(path_1.default.join(inputFolder, options.emailName, locale, `${const_1.FILE_NAME_EMAIL_VARIABLES}.json` // in the future also yml file with yml-js | ||
), "utf-8")); | ||
} | ||
catch (e) { | ||
node_color_log_1.default.error(`cannot loading variables for email ${options.emailName} ${options.locale}`); | ||
} | ||
return { | ||
@@ -30,0 +55,0 @@ ...common, |
@@ -11,4 +11,5 @@ "use strict"; | ||
const setupWatcher = function (config, callbacks) { | ||
const { inputFolder, templateSuffix } = config; | ||
const regex = new RegExp(`.*(.json|${templateSuffix})`); | ||
const { inputFolder, templateSuffix, templateOptions } = config; | ||
const variablesType = templateOptions?.variablesType || "json"; | ||
const regex = new RegExp(`.*(.json|.yml|${templateSuffix})`); | ||
return (0, node_watch_1.default)([path_1.default.join(inputFolder), const_1.CONFIG_FILE_NAME], { recursive: true, filter: regex }, function (evt, name) { | ||
@@ -21,3 +22,3 @@ if (evt === "update") { | ||
} | ||
else if (name.endsWith(`${const_1.FILE_NAME_EMAIL_VARIABLES}.json`)) { | ||
else if (name.endsWith(`${const_1.FILE_NAME_EMAIL_VARIABLES}.${variablesType}`)) { | ||
// chnged email variables | ||
@@ -24,0 +25,0 @@ const locale = parts[2]; |
@@ -15,8 +15,8 @@ "use strict"; | ||
try { | ||
const folders = fs_1.default | ||
.readdirSync(inputFolderPath) | ||
.filter((item) => !/(^|\/)\.[^\/\.]/g.test(item)); | ||
list = folders.map((folder) => ({ | ||
emailName: folder, | ||
url: path_1.default.join(inputFolderPath, folder), | ||
const folders = fs_1.default.readdirSync(inputFolderPath, { withFileTypes: true }); | ||
list = folders | ||
.filter((f) => f.isDirectory()) | ||
.map((folder) => ({ | ||
emailName: folder.name, | ||
url: path_1.default.join(inputFolderPath, folder.name), | ||
})); | ||
@@ -23,0 +23,0 @@ } |
{ | ||
"name": "marilena", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "a tool to build emails with cool tools like mjml and different template engine lime handlebars or eta.js", | ||
@@ -12,3 +12,3 @@ "scripts": { | ||
"test": "jest --silent=true", | ||
"jet-clean-cache": "jest --clearCache", | ||
"jest-clean-cache": "jest --clearCache", | ||
"prepare": "husky install", | ||
@@ -44,2 +44,3 @@ "release": "release-it" | ||
"fastify": "^4.14.1", | ||
"js-yaml": "^4.1.0", | ||
"minimist": "^1.2.8", | ||
@@ -53,2 +54,3 @@ "mjml": "^4.13.0", | ||
"@jest/globals": "^29.5.0", | ||
"@types/js-yaml": "^4.0.5", | ||
"@types/mjml": "^4.7.0", | ||
@@ -55,0 +57,0 @@ "@types/node": "^18.15.0", |
@@ -136,4 +136,5 @@ # Intro | ||
- [ ] fast-refresh on config change | ||
- [ ] load varibles from yaml format | ||
- [ ] load common variables | ||
- [x] load varibles from yaml format | ||
- [x] load common variables | ||
- [ ] easy way to send a real email | ||
- [ ] pass option to MJML render |
28525
499
140
11
13
+ Addedjs-yaml@^4.1.0
+ Addedargparse@2.0.1(transitive)
+ Addedjs-yaml@4.1.0(transitive)