1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.getPackageVersionFromPackageJson = exports.addPackageToPackageJson = void 0;
|
---|
11 | /**
|
---|
12 | * Sorts the keys of the given object.
|
---|
13 | * @returns A new object instance with sorted keys
|
---|
14 | */
|
---|
15 | function sortObjectByKeys(obj) {
|
---|
16 | return Object.keys(obj).sort().reduce((result, key) => {
|
---|
17 | result[key] = obj[key];
|
---|
18 | return result;
|
---|
19 | }, {});
|
---|
20 | }
|
---|
21 | /** Adds a package to the package.json in the given host tree. */
|
---|
22 | function addPackageToPackageJson(host, pkg, version) {
|
---|
23 | if (host.exists('package.json')) {
|
---|
24 | const sourceText = host.read('package.json').toString('utf-8');
|
---|
25 | const json = JSON.parse(sourceText);
|
---|
26 | if (!json.dependencies) {
|
---|
27 | json.dependencies = {};
|
---|
28 | }
|
---|
29 | if (!json.dependencies[pkg]) {
|
---|
30 | json.dependencies[pkg] = version;
|
---|
31 | json.dependencies = sortObjectByKeys(json.dependencies);
|
---|
32 | }
|
---|
33 | host.overwrite('package.json', JSON.stringify(json, null, 2));
|
---|
34 | }
|
---|
35 | return host;
|
---|
36 | }
|
---|
37 | exports.addPackageToPackageJson = addPackageToPackageJson;
|
---|
38 | /** Gets the version of the specified package by looking at the package.json in the given tree. */
|
---|
39 | function getPackageVersionFromPackageJson(tree, name) {
|
---|
40 | if (!tree.exists('package.json')) {
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 | const packageJson = JSON.parse(tree.read('package.json').toString('utf8'));
|
---|
44 | if (packageJson.dependencies && packageJson.dependencies[name]) {
|
---|
45 | return packageJson.dependencies[name];
|
---|
46 | }
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 | exports.getPackage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