@liferay/generator-js
Version:
Yeoman generators for Liferay DXP and Portal CE JavaScript projects.
101 lines (100 loc) • 3.5 kB
JavaScript
;
/**
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dot_prop_1 = __importDefault(require("dot-prop"));
const JsonModifier_1 = __importDefault(require("../JsonModifier"));
/**
* A class to help modifying the package.json file.
*/
class default_1 extends JsonModifier_1.default {
/**
* @param {Generator} generator a Yeoman generator
* @param space the space string/number of spaces to use when stringifying
*/
constructor(generator, space = '\t') {
super(generator, 'package.json', space);
}
/**
* Set the main entry of the package.json file
* @param {string} module module name
*/
setMain(module) {
this.modifyJson((json) => {
dot_prop_1.default.set(json, 'main', module);
});
}
/**
* Add a devDependency to the pacakge.json file
* @param {String} name package name
* @param {String} semver semver constraints
*/
addDevDependency(name, semver) {
name = this._escapeProp(name);
this.modifyJson((json) => {
dot_prop_1.default.set(json, `devDependencies.${name}`, semver);
});
}
/**
* Add a dependency to the pacakge.json file
* @param {String} name package name
* @param {String} semver semver constraints
*/
addDependency(name, semver) {
name = this._escapeProp(name);
this.modifyJson((json) => {
dot_prop_1.default.set(json, `dependencies.${name}`, semver);
});
}
/**
* Merge all dependencies and devDependencies contained in a JSON object
* into the package.json file.
* @param {Object} dependencies an object with dependencies and
* devDependencies fields
*/
mergeDependencies(dependencies) {
Object.entries(dependencies.dependencies).forEach(([name, semver]) => this.addDependency(name, semver));
Object.entries(dependencies.devDependencies).forEach(([name, semver]) => this.addDevDependency(name, semver));
}
/**
* Prepend a build command to the build npm script of package.json
* @param {String} command the command to run
*/
addBuildStep(command) {
this.modifyJson((json) => {
const currentBuild = dot_prop_1.default.get(json, 'scripts.build');
if (currentBuild) {
command = `${command} && ${currentBuild}`;
}
dot_prop_1.default.set(json, 'scripts.build', command);
});
}
/**
* Add a new npm script to package.json
* @param {String} name name of script
* @param {String} command command to run
*/
addScript(name, command) {
name = this._escapeProp(name);
this.modifyJson((json) => {
dot_prop_1.default.set(json, `scripts.${name}`, command);
});
}
/**
* Add a property inside the portlet section.
* @param {string} name name of property
* @param {any} value value of property
*/
addPortletProperty(name, value) {
name = this._escapeProp(name);
this.modifyJson((json) => {
dot_prop_1.default.set(json, `portlet.${name}`, value);
});
}
}
exports.default = default_1;