Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 67 additions & 6 deletions src/PropertiesReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -22,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.
*
Expand All @@ -42,11 +55,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;
};
Expand Down Expand Up @@ -82,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
*
Expand Down Expand Up @@ -272,8 +333,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;
Expand Down