"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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFja2FnZS1jb25maWcuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvbWF0ZXJpYWwvc2NoZW1hdGljcy9uZy1hZGQvcGFja2FnZS1jb25maWcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBUUg7OztHQUdHO0FBQ0gsU0FBUyxnQkFBZ0IsQ0FBQyxHQUEyQjtJQUNuRCxPQUFPLE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDO1NBQ3BCLElBQUksRUFBRTtTQUNOLE1BQU0sQ0F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