"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/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 */ function sortObjectByKeys(obj) { return Object.keys(obj).sort().reduce((result, key) => { result[key] = obj[key]; return 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) { if (!tree.exists('package.json')) { return null; } const packageJson = JSON.parse(tree.read('package.json').toString('utf8')); if (packageJson.dependencies && packageJson.dependencies[name]) { return packageJson.dependencies[name]; } return null; } exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFja2FnZS1jb25maWcuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvbWF0ZXJpYWwvc2NoZW1hdGljcy9uZy1hZGQvcGFja2FnZS1jb25maWcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBUUg7OztHQUdHO0FBQ0gsU0FBUyxnQkFBZ0IsQ0FBQyxHQUEyQjtJQUNuRCxPQUFPLE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUMsSUFBSSxFQUFFLENBQUMsTUFBTSxDQUFDLENB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