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.getPackageJsonDependency = exports.removePackageJsonDependency = exports.addPackageJsonDependency = exports.NodeDependencyType = void 0;
|
---|
11 | const json_file_1 = require("./json-file");
|
---|
12 | const PKG_JSON_PATH = '/package.json';
|
---|
13 | var NodeDependencyType;
|
---|
14 | (function (NodeDependencyType) {
|
---|
15 | NodeDependencyType["Default"] = "dependencies";
|
---|
16 | NodeDependencyType["Dev"] = "devDependencies";
|
---|
17 | NodeDependencyType["Peer"] = "peerDependencies";
|
---|
18 | NodeDependencyType["Optional"] = "optionalDependencies";
|
---|
19 | })(NodeDependencyType = exports.NodeDependencyType || (exports.NodeDependencyType = {}));
|
---|
20 | const ALL_DEPENDENCY_TYPE = [
|
---|
21 | NodeDependencyType.Default,
|
---|
22 | NodeDependencyType.Dev,
|
---|
23 | NodeDependencyType.Optional,
|
---|
24 | NodeDependencyType.Peer,
|
---|
25 | ];
|
---|
26 | function addPackageJsonDependency(tree, dependency, pkgJsonPath = PKG_JSON_PATH) {
|
---|
27 | const json = new json_file_1.JSONFile(tree, pkgJsonPath);
|
---|
28 | const { overwrite, type, name, version } = dependency;
|
---|
29 | const path = [type, name];
|
---|
30 | if (overwrite || !json.get(path)) {
|
---|
31 | json.modify(path, version);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | exports.addPackageJsonDependency = addPackageJsonDependency;
|
---|
35 | function removePackageJsonDependency(tree, name, pkgJsonPath = PKG_JSON_PATH) {
|
---|
36 | const json = new json_file_1.JSONFile(tree, pkgJsonPath);
|
---|
37 | for (const depType of ALL_DEPENDENCY_TYPE) {
|
---|
38 | json.remove([depType, name]);
|
---|
39 | }
|
---|
40 | }
|
---|
41 | exports.removePackageJsonDependency = removePackageJsonDependency;
|
---|
42 | function getPackageJsonDependency(tree, name, pkgJsonPath = PKG_JSON_PATH) {
|
---|
43 | const json = new json_file_1.JSONFile(tree, pkgJsonPath);
|
---|
44 | for (const depType of ALL_DEPENDENCY_TYPE) {
|
---|
45 | const version = json.get([depType, name]);
|
---|
46 | if (typeof version === 'string') {
|
---|
47 | return {
|
---|
48 | type: depType,
|
---|
49 | name: name,
|
---|
50 | version,
|
---|
51 | };
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return null;
|
---|
55 | }
|
---|
56 | exports.getPackageJsonDependency = getPackageJsonDependency;
|
---|