file-system.d.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.dev/license
  7. */
  8. import { UpdateRecorder } from './update-recorder';
  9. /**
  10. * A workspace path semantically is equivalent to the `Path` type provided by the
  11. * Angular devkit. Paths denoted with such a type are guaranteed to be representing
  12. * paths of a given virtual file system. This means that the root of a path can be
  13. * different, and does not necessarily need to match the root in the real file system.
  14. *
  15. * For example: Consider we have a project in `/home/<..>/my-project`. Then a path
  16. * like `/package.json` could actually refer to the `package.json` file in `my-project`.
  17. * Note that in the real file system this would not match though.
  18. *
  19. * One might wonder why another type has been declared for such paths, when there
  20. * already is the `Path` type provided by the devkit. We do this for a couple of reasons:
  21. *
  22. * 1. The update-tool cannot have a dependency on the Angular devkit as that one
  23. * is not synced into g3. We want to be able to run migrations in g3 if needed.
  24. */
  25. export type WorkspacePath = string & {
  26. __PRIVATE_DEVKIT_PATH: void;
  27. };
  28. /** Interface that describes a directory. */
  29. export interface DirectoryEntry {
  30. /** List of directories inside the directory. */
  31. directories: string[];
  32. /** List of files inside the directory. */
  33. files: string[];
  34. }
  35. /**
  36. * Abstraction of the file system that migrations can use to record and apply
  37. * changes. This is necessary to support virtual file systems as used in the CLI devkit.
  38. */
  39. export declare abstract class FileSystem {
  40. /** Checks whether the given file exists. */
  41. abstract fileExists(path: WorkspacePath): boolean;
  42. /** Checks whether the given directory exists. */
  43. abstract directoryExists(path: WorkspacePath): boolean;
  44. /** Gets the contents of the given file. */
  45. abstract read(filePath: WorkspacePath): string | null;
  46. /** Reads the given directory to retrieve children. */
  47. abstract readDirectory(dirPath: WorkspacePath): DirectoryEntry;
  48. /**
  49. * Creates an update recorder for the given file. Edits can be recorded and
  50. * committed in batches. Changes are not applied automatically because otherwise
  51. * migrations would need to re-read files, or account for shifted file contents.
  52. */
  53. abstract edit(filePath: WorkspacePath): UpdateRecorder;
  54. /** Applies all changes which have been recorded in update recorders. */
  55. abstract commitEdits(): void;
  56. /** Creates a new file with the given content. */
  57. abstract create(filePath: WorkspacePath, content: string): void;
  58. /** Overwrites an existing file with the given content. */
  59. abstract overwrite(filePath: WorkspacePath, content: string): void;
  60. /** Deletes the given file. */
  61. abstract delete(filePath: WorkspacePath): void;
  62. /**
  63. * Resolves given paths to a resolved path in the file system. For example, the devkit
  64. * tree considers the actual workspace directory as file system root.
  65. *
  66. * Follows the same semantics as the native path `resolve` method. i.e. segments
  67. * are processed in reverse. The last segment is considered the target and the
  68. * function will iterate from the target through other segments until it finds an
  69. * absolute path segment.
  70. */
  71. abstract resolve(...segments: string[]): WorkspacePath;
  72. }