dependency.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.dev/license
  8. */
  9. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || (function () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. exports.ExistingBehavior = exports.InstallBehavior = exports.DependencyType = void 0;
  44. exports.addDependency = addDependency;
  45. const tasks_1 = require("@angular-devkit/schematics/tasks");
  46. const path = __importStar(require("node:path"));
  47. const installTasks = new WeakMap();
  48. /**
  49. * An enum used to specify the type of a dependency found within a package manifest
  50. * file (`package.json`).
  51. */
  52. var DependencyType;
  53. (function (DependencyType) {
  54. DependencyType["Default"] = "dependencies";
  55. DependencyType["Dev"] = "devDependencies";
  56. DependencyType["Peer"] = "peerDependencies";
  57. })(DependencyType || (exports.DependencyType = DependencyType = {}));
  58. /**
  59. * An enum used to specify the dependency installation behavior for the {@link addDependency}
  60. * schematics rule. The installation behavior affects if and when {@link NodePackageInstallTask}
  61. * will be scheduled when using the rule.
  62. */
  63. var InstallBehavior;
  64. (function (InstallBehavior) {
  65. /**
  66. * No installation will occur as a result of the rule when specified.
  67. *
  68. * NOTE: This does not prevent other rules from scheduling a {@link NodePackageInstallTask}
  69. * which may install the dependency.
  70. */
  71. InstallBehavior[InstallBehavior["None"] = 0] = "None";
  72. /**
  73. * Automatically determine the need to schedule a {@link NodePackageInstallTask} based on
  74. * previous usage of the {@link addDependency} within the schematic.
  75. */
  76. InstallBehavior[InstallBehavior["Auto"] = 1] = "Auto";
  77. /**
  78. * Always schedule a {@link NodePackageInstallTask} when the rule is executed.
  79. */
  80. InstallBehavior[InstallBehavior["Always"] = 2] = "Always";
  81. })(InstallBehavior || (exports.InstallBehavior = InstallBehavior = {}));
  82. /**
  83. * An enum used to specify the existing dependency behavior for the {@link addDependency}
  84. * schematics rule. The existing behavior affects whether the named dependency will be added
  85. * to the `package.json` when the dependency is already present with a differing specifier.
  86. */
  87. var ExistingBehavior;
  88. (function (ExistingBehavior) {
  89. /**
  90. * The dependency will not be added or otherwise changed if it already exists.
  91. */
  92. ExistingBehavior[ExistingBehavior["Skip"] = 0] = "Skip";
  93. /**
  94. * The dependency's existing specifier will be replaced with the specifier provided in the
  95. * {@link addDependency} call. A warning will also be shown during schematic execution to
  96. * notify the user of the replacement.
  97. */
  98. ExistingBehavior[ExistingBehavior["Replace"] = 1] = "Replace";
  99. })(ExistingBehavior || (exports.ExistingBehavior = ExistingBehavior = {}));
  100. /**
  101. * Adds a package as a dependency to a `package.json`. By default the `package.json` located
  102. * at the schematic's root will be used. The `manifestPath` option can be used to explicitly specify
  103. * a `package.json` in different location. The type of the dependency can also be specified instead
  104. * of the default of the `dependencies` section by using the `type` option for either `devDependencies`
  105. * or `peerDependencies`.
  106. *
  107. * When using this rule, {@link NodePackageInstallTask} does not need to be included directly by
  108. * a schematic. A package manager install task will be automatically scheduled as needed.
  109. *
  110. * @param name The name of the package to add.
  111. * @param specifier The package specifier for the package to add. Typically a SemVer range.
  112. * @param options An optional object that can contain the `type` of the dependency
  113. * and/or a path (`packageJsonPath`) of a manifest file (`package.json`) to modify.
  114. * @returns A Schematics {@link Rule}
  115. */
  116. function addDependency(name, specifier, options = {}) {
  117. const { type = DependencyType.Default, packageJsonPath = '/package.json', install = InstallBehavior.Auto, existing = ExistingBehavior.Replace, } = options;
  118. return (tree, context) => {
  119. const manifest = tree.readJson(packageJsonPath);
  120. const dependencySection = manifest[type];
  121. if (!dependencySection) {
  122. // Section is not present. The dependency can be added to a new object literal for the section.
  123. manifest[type] = { [name]: specifier };
  124. }
  125. else {
  126. const existingSpecifier = dependencySection[name];
  127. if (existingSpecifier === specifier) {
  128. // Already present with same specifier
  129. return;
  130. }
  131. if (existingSpecifier) {
  132. // Already present but different specifier
  133. if (existing === ExistingBehavior.Skip) {
  134. return;
  135. }
  136. // ExistingBehavior.Replace is the only other behavior currently
  137. context.logger.warn(`Package dependency "${name}" already exists with a different specifier. ` +
  138. `"${existingSpecifier}" will be replaced with "${specifier}".`);
  139. }
  140. // Add new dependency in alphabetical order
  141. const entries = Object.entries(dependencySection);
  142. entries.push([name, specifier]);
  143. entries.sort((a, b) => a[0].localeCompare(b[0]));
  144. manifest[type] = Object.fromEntries(entries);
  145. }
  146. tree.overwrite(packageJsonPath, JSON.stringify(manifest, null, 2));
  147. const installPaths = installTasks.get(context) ?? new Set();
  148. if (install === InstallBehavior.Always ||
  149. (install === InstallBehavior.Auto && !installPaths.has(packageJsonPath))) {
  150. context.addTask(new tasks_1.NodePackageInstallTask({ workingDirectory: path.dirname(packageJsonPath) }));
  151. installPaths.add(packageJsonPath);
  152. installTasks.set(context, installPaths);
  153. }
  154. };
  155. }