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