ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
46 lines • 1.9 kB
JavaScript
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageVersionFromPackageJson = exports.addPackageToPackageJson = void 0;
/**
* Sorts the keys of the given object.
*
* @returns A new object instance with sorted keys
*/
// eslint-disable-next-line @typescript-eslint/ban-types
function sortObjectByKeys(obj) {
return Object.keys(obj)
.sort()
.reduce((result, key) => (result[key] = obj[key]) && result, {});
}
/** Adds a package to the package.json in the given host tree. */
function addPackageToPackageJson(host, pkg, version) {
if (host.exists('package.json')) {
const sourceText = host.read('package.json').toString('utf-8');
const json = JSON.parse(sourceText);
if (!json.dependencies) {
json.dependencies = {};
}
if (!json.dependencies[pkg]) {
json.dependencies[pkg] = version;
json.dependencies = sortObjectByKeys(json.dependencies);
}
host.overwrite('package.json', JSON.stringify(json, null, 2));
}
return host;
}
exports.addPackageToPackageJson = addPackageToPackageJson;
/** Gets the version of the specified package by looking at the package.json in the given tree. */
function getPackageVersionFromPackageJson(tree, name) {
var _a, _b;
if (!tree.exists('package.json')) {
return null;
}
const packageJson = JSON.parse(tree.read('package.json').toString('utf8'));
return (_b = (_a = packageJson.dependencies) === null || _a === void 0 ? void 0 : _a[name]) !== null && _b !== void 0 ? _b : null;
}
exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson;
//# sourceMappingURL=package-config.js.map
;