From 86c966a97e123097b3d3059b896e73f5ff9166ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Hern=C3=A1ndez=20Fisac?= Date: Mon, 28 Jan 2019 20:51:20 +0100 Subject: [PATCH 1/2] Encoding as argument for instantiation. --- src/PropertiesReader.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/PropertiesReader.js b/src/PropertiesReader.js index d576794..f4b3f2b 100644 --- a/src/PropertiesReader.js +++ b/src/PropertiesReader.js @@ -7,13 +7,14 @@ /** * * @param {String} sourceFile + * @param {String} encoding * @constructor * @name {PropertiesReader} */ - function PropertiesReader(sourceFile) { + function PropertiesReader(sourceFile, encoding) { this._properties = {}; this._propertiesExpanded = {}; - this.append(sourceFile); + this.append(sourceFile, encoding); } /** @@ -42,11 +43,12 @@ /** * Append a file to the properties into the PropertiesReader * @param {string} sourceFile + * @param {String} encoding * @return {PropertiesReader} this instance */ - PropertiesReader.prototype.append = function (sourceFile) { + PropertiesReader.prototype.append = function (sourceFile, encoding) { if (sourceFile) { - this.read(fs.readFileSync(sourceFile, 'utf-8')); + this.read(fs.readFileSync(sourceFile, encoding ? encoding : 'utf-8')); } return this; }; @@ -272,8 +274,8 @@ return this; }; - PropertiesReader.builder = function(sourceFile) { - return new PropertiesReader(sourceFile); + PropertiesReader.builder = function(sourceFile, encoding) { + return new PropertiesReader(sourceFile, encoding); }; module.exports = PropertiesReader.builder; From b8b4a450e993cffa17bfd951ece54ab45e7d40ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Hern=C3=A1ndez=20Fisac?= Date: Mon, 28 Jan 2019 20:53:12 +0100 Subject: [PATCH 2/2] method update to write changes or new properties in the properties file. --- src/PropertiesReader.js | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/PropertiesReader.js b/src/PropertiesReader.js index f4b3f2b..3dfe079 100644 --- a/src/PropertiesReader.js +++ b/src/PropertiesReader.js @@ -23,6 +23,18 @@ */ PropertiesReader.prototype._section = ''; + /** + * @type {String} The output to be written when updating properties + * @ignore + */ + PropertiesReader.prototype._output = ''; + + /** + * @type {Array} The keys already updated in the output + * @ignore + */ + PropertiesReader.prototype._updatedKeys = []; + /** * Gets the number of properties that have been read into this PropertiesReader. * @@ -84,6 +96,53 @@ } }; + /** + * Updates the sourceFile with the given properties string + * @param {string} destFile + * @param {String} encoding + * @return {PropertiesReader} this instance + */ + PropertiesReader.prototype.update = function(destFile, encoding) { + var input = fs.readFileSync(destFile, encoding ? encoding : 'utf-8'); + this._output = ''; + ('' + input).split('\n').forEach(this._updateLine, this); + this.each((key, value) => { + if (!this._updatedKeys.includes(key)) { + this._output = this._output + key + '=' + value + '\n'; + } + }); + // Se elimina el último retorno de carro + fs.writeFileSync(destFile, this._output.substring(0, this._output.length - 1), encoding); + return this; + }; + + /** + * Used as a processor for the array of input lines when updating to a dest file + * @param {String} propertyString + */ + PropertiesReader.prototype._updateLine = function(propertyString) { + var updated = false; + if (!!(propertyString = propertyString.trim())) { + var section = /^\[([^=]+)\]$/.exec(propertyString); + var property = !section && /^([^#=]+)(={0,1})(.*)$/.exec(propertyString); + if (section) { + this._section = section[1]; + } else if (property) { + section = this._section ? this._section + '.' : ''; + var key = section + property[1].trim(); + var newValue = this.get(key); + if (newValue != null && newValue != undefined) { + updated = true; + this._output = this._output + key + "=" + newValue + '\n'; + this._updatedKeys.push(key); + } + } + } + if (!updated) { + this._output += propertyString + '\n'; + } + }; + /** * Calls the supplied function for each property *