virtual-host.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.FileSystemHost = void 0;
  11. exports.createFileSystemCompilerHost = createFileSystemCompilerHost;
  12. exports.createFormatDiagnosticHost = createFormatDiagnosticHost;
  13. const ts = require("typescript");
  14. /**
  15. * Implementation of a TypeScript parse config host that relies fully on
  16. * a given virtual file system.
  17. */
  18. class FileSystemHost {
  19. constructor(_fileSystem) {
  20. this._fileSystem = _fileSystem;
  21. this.useCaseSensitiveFileNames = ts.sys.useCaseSensitiveFileNames;
  22. }
  23. fileExists(path) {
  24. return this._fileSystem.fileExists(this._fileSystem.resolve(path));
  25. }
  26. readFile(path) {
  27. const content = this._fileSystem.read(this._fileSystem.resolve(path));
  28. if (content === null) {
  29. return undefined;
  30. }
  31. // Strip BOM as otherwise TSC methods (e.g. "getWidth") will return an offset which
  32. // which breaks the CLI UpdateRecorder. https://github.com/angular/angular/pull/30719
  33. return content.replace(/^\uFEFF/, '');
  34. }
  35. readDirectory(rootDir, extensions, excludes, includes, depth) {
  36. if (ts.matchFiles === undefined) {
  37. throw Error('Unable to read directory in virtual file system host. This means that ' +
  38. 'TypeScript changed its file matching internals.\n\nPlease consider downgrading your ' +
  39. 'TypeScript version, and report an issue in the Angular Components repository.');
  40. }
  41. return ts.matchFiles(rootDir, extensions, extensions, includes, this.useCaseSensitiveFileNames, '/', depth, p => this._getFileSystemEntries(p), p => this._fileSystem.resolve(p), p => this._fileSystem.directoryExists(this._fileSystem.resolve(p)));
  42. }
  43. _getFileSystemEntries(path) {
  44. return this._fileSystem.readDirectory(this._fileSystem.resolve(path));
  45. }
  46. }
  47. exports.FileSystemHost = FileSystemHost;
  48. /**
  49. * Creates a TypeScript compiler host that fully relies fully on the given
  50. * virtual file system. i.e. no interactions with the working directory.
  51. */
  52. function createFileSystemCompilerHost(options, fileSystem) {
  53. const host = ts.createCompilerHost(options, true);
  54. const virtualHost = new FileSystemHost(fileSystem);
  55. host.readFile = virtualHost.readFile.bind(virtualHost);
  56. host.readDirectory = virtualHost.readDirectory.bind(virtualHost);
  57. host.fileExists = virtualHost.fileExists.bind(virtualHost);
  58. host.directoryExists = dirPath => fileSystem.directoryExists(fileSystem.resolve(dirPath));
  59. host.getCurrentDirectory = () => '/';
  60. host.getCanonicalFileName = p => fileSystem.resolve(p);
  61. return host;
  62. }
  63. /** Creates a format diagnostic host that works with the given file system. */
  64. function createFormatDiagnosticHost(fileSystem) {
  65. return {
  66. getCanonicalFileName: p => fileSystem.resolve(p),
  67. getCurrentDirectory: () => '/',
  68. getNewLine: () => '\n',
  69. };
  70. }
  71. //# sourceMappingURL=virtual-host.js.map