devkit-file-system.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.DevkitFileSystem = void 0;
  11. const core_1 = require("@angular-devkit/core");
  12. const file_system_1 = require("../update-tool/file-system");
  13. const path = require("path");
  14. /**
  15. * File system that leverages the virtual tree from the CLI devkit. This file
  16. * system is commonly used by `ng update` migrations that run as part of the
  17. * Angular CLI.
  18. */
  19. class DevkitFileSystem extends file_system_1.FileSystem {
  20. constructor(_tree) {
  21. super();
  22. this._tree = _tree;
  23. this._updateRecorderCache = new Map();
  24. }
  25. resolve(...segments) {
  26. // Note: We use `posix.resolve` as the devkit paths are using posix separators.
  27. return (0, core_1.normalize)(path.posix.resolve('/', ...segments.map(core_1.normalize)));
  28. }
  29. edit(filePath) {
  30. if (this._updateRecorderCache.has(filePath)) {
  31. return this._updateRecorderCache.get(filePath);
  32. }
  33. const recorder = this._tree.beginUpdate(filePath);
  34. this._updateRecorderCache.set(filePath, recorder);
  35. return recorder;
  36. }
  37. commitEdits() {
  38. this._updateRecorderCache.forEach(r => this._tree.commitUpdate(r));
  39. this._updateRecorderCache.clear();
  40. }
  41. fileExists(filePath) {
  42. return this._tree.exists(filePath);
  43. }
  44. directoryExists(dirPath) {
  45. // The devkit tree does not expose an API for checking whether a given
  46. // directory exists. It throws a specific error though if a directory
  47. // is being read as a file. We use that to check if a directory exists.
  48. try {
  49. this._tree.get(dirPath);
  50. }
  51. catch (e) {
  52. // Note: We do not use an `instanceof` check here. It could happen that
  53. // the devkit version used by the CLI is different than the one we end up
  54. // loading. This can happen depending on how Yarn/NPM hoists the NPM
  55. // packages / whether there are multiple versions installed. Typescript
  56. // throws a compilation error if the type isn't specified and we can't
  57. // check the type, so we have to cast the error output to any.
  58. if (e.constructor.name === 'PathIsDirectoryException') {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. overwrite(filePath, content) {
  65. this._tree.overwrite(filePath, content);
  66. }
  67. create(filePath, content) {
  68. this._tree.create(filePath, content);
  69. }
  70. delete(filePath) {
  71. this._tree.delete(filePath);
  72. }
  73. read(filePath) {
  74. const buffer = this._tree.read(filePath);
  75. return buffer !== null ? buffer.toString() : null;
  76. }
  77. readDirectory(dirPath) {
  78. const { subdirs: directories, subfiles: files } = this._tree.getDir(dirPath);
  79. return { directories, files };
  80. }
  81. }
  82. exports.DevkitFileSystem = DevkitFileSystem;
  83. //# sourceMappingURL=devkit-file-system.js.map