From 42ba687aa2989846bbe569b9e9a8a6237cf7ff45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 28 Sep 2020 10:45:21 +0200 Subject: [PATCH] MOBILE-3523 gulp: Fix config gulp task escaping single quotes in values --- gulp/task-build-config.js | 67 +++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/gulp/task-build-config.js b/gulp/task-build-config.js index 98d380140..69bf71aa3 100644 --- a/gulp/task-build-config.js +++ b/gulp/task-build-config.js @@ -45,6 +45,8 @@ class BuildConfigTask { * @param done Function to call when done. */ run(path, done) { + const self = this; + // Get the last commit. exec('git log -1 --pretty=format:"%H"', (err, commit, stderr) => { if (err) { @@ -61,28 +63,9 @@ class BuildConfigTask { let contents = LICENSE + '// tslint:disable: variable-name\n' + 'export class CoreConfigConstants {\n'; for (let key in config) { - let value = config[key]; + let value = self.transformValue(config[key]); - if (typeof value == 'string') { - // Wrap the string in ' and escape them. - value = "'" + value.replace(/([^\\])'/g, "$1\\'") + "'"; - } else if (typeof value != 'number' && typeof value != 'boolean') { - // Stringify with 4 spaces of indentation, and then add 4 more spaces in each line. - value = JSON.stringify(value, null, 4).replace(/^(?: )/gm, ' ').replace(/^(?:})/gm, ' }'); - // Replace " by ' in values. - value = value.replace(/: "([^"]*)"/g, ": '$1'"); - - // Check if the keys have "-" in it. - const matches = value.match(/"([^"]*\-[^"]*)":/g); - if (matches) { - // Replace " by ' in keys. We cannot remove them because keys have chars like '-'. - value = value.replace(/"([^"]*)":/g, "'$1':"); - } else { - // Remove ' in keys. - value = value.replace(/"([^"]*)":/g, "$1:"); - } - - // Add type any to the key. + if (typeof config[key] != 'number' && typeof config[key] != 'boolean' && typeof config[key] != 'string') { key = key + ': any'; } @@ -108,6 +91,48 @@ class BuildConfigTask { .on('end', done); }); } + + + /** + * Recursively transform a config value into personalized TS. + * + * @param value Value to convert + * @return Converted value. + */ + transformValue(value) { + if (typeof value == 'string') { + // Wrap the string in ' and escape them. + return "'" + value.replace(/([^\\])'/g, "$1\\'") + "'"; + } + + if (typeof value != 'number' && typeof value != 'boolean') { + const isArray = Array.isArray(value); + let contents = ''; + + let quoteKeys = false; + if (!isArray) { + for (let key in value) { + if (key.indexOf('-') >= 0) { + quoteKeys = true; + break; + } + } + } + + for (let key in value) { + value[key] = this.transformValue(value[key]); + + const quotedKey = quoteKeys ? "'" + key + "'" : key; + contents += ' ' + (isArray ? '' : quotedKey + ': ') + value[key] + ",\n"; + } + + contents += (isArray ? ']' : '}'); + + return (isArray ? '[' : '{') + "\n" + contents.replace(/^/gm, ' '); + } + + return value; + } } module.exports = BuildConfigTask;