@zextras/zapp-cli
Version:
CLI tool to build Zextras Apps and Themes
49 lines (42 loc) • 1.21 kB
JavaScript
/*
* **** BEGIN LICENSE BLOCK *****
* Copyright (C) 2011-2019 ZeXtras
*
* The contents of this file are subject to the ZeXtras EULA;
* you may not use this file except in compliance with the EULA.
* You may obtain a copy of the EULA at
* http://www.zextras.com/zextras-eula.html
* **** END LICENSE BLOCK *****
*/
module.exports = (source) => {
const props = {};
function lukePathWalker(path, value) {
let pos = props;
const steps = path.split('.');
while (steps.length > 1) {
const step = steps.shift();
if (!pos.hasOwnProperty(step)) pos[step] = {};
pos = pos[step];
}
try {
pos[steps[0]] = JSON.parse(`"${value}"`);
}
catch (err) {
console.error(`Ignoring translation for ${path} as "${value}"`);
}
}
const raw = source.replace(/^(#.*)?\s*$(?:\r\n?|\n)/gm, '');
const parts = raw.split('\n');
for (let i = 0; i < parts.length; i++) {
const KEY_VAL_REG = /([^\s]*)\s?=\s?(.*)/g;
const matches = KEY_VAL_REG.exec(parts[i]);
if (!matches) continue;
const key = matches[1];
const value = matches[2];
lukePathWalker(
key,
value.replace(/\\:/, ':')
);
}
return `const properties = ${ JSON.stringify(props) };\nexport default properties;`;
};