@liferay/generator-js
Version:
Yeoman generators for Liferay DXP and Portal CE JavaScript projects.
49 lines (48 loc) • 1.48 kB
JavaScript
;
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* A class to help modifying JSON files.
*/
class JsonModifier {
/**
* @param {Generator} generator a Yeoman generator
* @param {String} path path to file
* @param space the space string/number of spaces to use when stringifying
*/
constructor(generator, path, space = ' ') {
this._generator = generator;
this._path = path;
this._space = space;
}
/**
* Get the JSON object associated to this modifier
* @return {Object} a parsed JSON object
*/
get json() {
return JSON.parse(this._generator.fs.read(this._path));
}
/**
* Modify an existing JSON file.
* @param {Function} modifier the code that modifies the JSON (it receives a
* single parameter with the JSON object)
*/
modifyJson(modifier) {
const gen = this._generator;
const json = this.json;
modifier(json);
gen.fs.write(this._path, JSON.stringify(json, null, this._space));
}
/**
* Escape a property name to make it suitable for use in dot-prop
* @param {string} name name of property
* @return {string} the escaped name
*/
_escapeProp(name) {
return name.replace(/\./g, '\\.');
}
}
exports.default = JsonModifier;