action.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.ActionList = exports.UnknownActionException = void 0;
  11. exports.isContentAction = isContentAction;
  12. const core_1 = require("@angular-devkit/core");
  13. class UnknownActionException extends core_1.BaseException {
  14. constructor(action) {
  15. super(`Unknown action: "${action.kind}".`);
  16. }
  17. }
  18. exports.UnknownActionException = UnknownActionException;
  19. let _id = 1;
  20. class ActionList {
  21. _actions = [];
  22. _action(action) {
  23. this._actions.push({
  24. ...action,
  25. id: _id++,
  26. parent: this._actions[this._actions.length - 1]?.id ?? 0,
  27. });
  28. }
  29. create(path, content) {
  30. this._action({ kind: 'c', path, content });
  31. }
  32. overwrite(path, content) {
  33. this._action({ kind: 'o', path, content });
  34. }
  35. rename(path, to) {
  36. this._action({ kind: 'r', path, to });
  37. }
  38. delete(path) {
  39. this._action({ kind: 'd', path });
  40. }
  41. optimize() {
  42. const toCreate = new Map();
  43. const toRename = new Map();
  44. const toOverwrite = new Map();
  45. const toDelete = new Set();
  46. for (const action of this._actions) {
  47. switch (action.kind) {
  48. case 'c':
  49. toCreate.set(action.path, action.content);
  50. break;
  51. case 'o':
  52. if (toCreate.has(action.path)) {
  53. toCreate.set(action.path, action.content);
  54. }
  55. else {
  56. toOverwrite.set(action.path, action.content);
  57. }
  58. break;
  59. case 'd':
  60. toDelete.add(action.path);
  61. break;
  62. case 'r': {
  63. const maybeCreate = toCreate.get(action.path);
  64. const maybeOverwrite = toOverwrite.get(action.path);
  65. if (maybeCreate) {
  66. toCreate.delete(action.path);
  67. toCreate.set(action.to, maybeCreate);
  68. }
  69. if (maybeOverwrite) {
  70. toOverwrite.delete(action.path);
  71. toOverwrite.set(action.to, maybeOverwrite);
  72. }
  73. let maybeRename = undefined;
  74. for (const [from, to] of toRename.entries()) {
  75. if (to == action.path) {
  76. maybeRename = from;
  77. break;
  78. }
  79. }
  80. if (maybeRename) {
  81. toRename.set(maybeRename, action.to);
  82. }
  83. if (!maybeCreate && !maybeOverwrite && !maybeRename) {
  84. toRename.set(action.path, action.to);
  85. }
  86. break;
  87. }
  88. }
  89. }
  90. this._actions = [];
  91. toDelete.forEach((x) => {
  92. this.delete(x);
  93. });
  94. toRename.forEach((to, from) => {
  95. this.rename(from, to);
  96. });
  97. toCreate.forEach((content, path) => {
  98. this.create(path, content);
  99. });
  100. toOverwrite.forEach((content, path) => {
  101. this.overwrite(path, content);
  102. });
  103. }
  104. push(action) {
  105. this._actions.push(action);
  106. }
  107. get(i) {
  108. return this._actions[i];
  109. }
  110. has(action) {
  111. for (let i = 0; i < this._actions.length; i++) {
  112. const a = this._actions[i];
  113. if (a.id == action.id) {
  114. return true;
  115. }
  116. if (a.id > action.id) {
  117. return false;
  118. }
  119. }
  120. return false;
  121. }
  122. find(predicate) {
  123. return this._actions.find(predicate) || null;
  124. }
  125. forEach(fn, thisArg) {
  126. this._actions.forEach(fn, thisArg);
  127. }
  128. get length() {
  129. return this._actions.length;
  130. }
  131. [Symbol.iterator]() {
  132. return this._actions[Symbol.iterator]();
  133. }
  134. }
  135. exports.ActionList = ActionList;
  136. function isContentAction(action) {
  137. return action.kind == 'c' || action.kind == 'o';
  138. }