Merge pull request #2549 from crazyserver/MOBILE-3523

MOBILE-3523 gulp: Fix config gulp task escaping single quotes in values
main
Dani Palou 2020-09-28 10:59:07 +02:00 committed by GitHub
commit e72e2ae799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 21 deletions

View File

@ -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;