diff --git a/packages/compiler-cli/src/ngtsc/file_system/src/invalid_file_system.ts b/packages/compiler-cli/src/ngtsc/file_system/src/invalid_file_system.ts index b9a9c4af21..30da87045c 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/src/invalid_file_system.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/src/invalid_file_system.ts @@ -22,7 +22,10 @@ export class InvalidFileSystem implements FileSystem { readFile(path: AbsoluteFsPath): string { throw makeError(); } - writeFile(path: AbsoluteFsPath, data: string, exclusive?: boolean): void { + readFileBuffer(path: AbsoluteFsPath): Buffer { + throw makeError(); + } + writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive?: boolean): void { throw makeError(); } removeFile(path: AbsoluteFsPath): void { diff --git a/packages/compiler-cli/src/ngtsc/file_system/src/node_js_file_system.ts b/packages/compiler-cli/src/ngtsc/file_system/src/node_js_file_system.ts index 9e50a8e494..934a4df903 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/src/node_js_file_system.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/src/node_js_file_system.ts @@ -23,7 +23,10 @@ export class NodeJSFileSystem implements FileSystem { readFile(path: AbsoluteFsPath): string { return fs.readFileSync(path, 'utf8'); } - writeFile(path: AbsoluteFsPath, data: string, exclusive: boolean = false): void { + readFileBuffer(path: AbsoluteFsPath): Buffer { + return fs.readFileSync(path); + } + writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive: boolean = false): void { fs.writeFileSync(path, data, exclusive ? {flag: 'wx'} : undefined); } removeFile(path: AbsoluteFsPath): void { diff --git a/packages/compiler-cli/src/ngtsc/file_system/src/types.ts b/packages/compiler-cli/src/ngtsc/file_system/src/types.ts index 365de3b851..aac892c089 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/src/types.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/src/types.ts @@ -37,7 +37,8 @@ export type PathSegment = BrandedPath<'PathSegment'>; export interface FileSystem { exists(path: AbsoluteFsPath): boolean; readFile(path: AbsoluteFsPath): string; - writeFile(path: AbsoluteFsPath, data: string, exclusive?: boolean): void; + readFileBuffer(path: AbsoluteFsPath): Buffer; + writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive?: boolean): void; removeFile(path: AbsoluteFsPath): void; symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void; readdir(path: AbsoluteFsPath): PathSegment[]; diff --git a/packages/compiler-cli/src/ngtsc/file_system/test/node_js_file_system_spec.ts b/packages/compiler-cli/src/ngtsc/file_system/test/node_js_file_system_spec.ts index 2e15f6b646..ff5d64e8ce 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/test/node_js_file_system_spec.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/test/node_js_file_system_spec.ts @@ -44,6 +44,16 @@ describe('NodeJSFileSystem', () => { }); }); + describe('readFileBuffer()', () => { + it('should delegate to fs.readFileSync()', () => { + const buffer = new Buffer('Some contents'); + const spy = spyOn(realFs, 'readFileSync').and.returnValue(buffer); + const result = fs.readFileBuffer(abcPath); + expect(result).toBe(buffer); + expect(spy).toHaveBeenCalledWith(abcPath); + }); + }); + describe('writeFile()', () => { it('should delegate to fs.writeFileSync()', () => { const spy = spyOn(realFs, 'writeFileSync'); diff --git a/packages/compiler-cli/src/ngtsc/file_system/testing/src/mock_file_system.ts b/packages/compiler-cli/src/ngtsc/file_system/testing/src/mock_file_system.ts index 8382d456f5..147aa8d563 100644 --- a/packages/compiler-cli/src/ngtsc/file_system/testing/src/mock_file_system.ts +++ b/packages/compiler-cli/src/ngtsc/file_system/testing/src/mock_file_system.ts @@ -32,13 +32,22 @@ export abstract class MockFileSystem implements FileSystem { readFile(path: AbsoluteFsPath): string { const {entity} = this.findFromPath(path); if (isFile(entity)) { - return entity; + return entity.toString(); } else { throw new MockFileSystemError('ENOENT', path, `File "${path}" does not exist.`); } } - writeFile(path: AbsoluteFsPath, data: string, exclusive: boolean = false): void { + readFileBuffer(path: AbsoluteFsPath): Buffer { + const {entity} = this.findFromPath(path); + if (isFile(entity)) { + return Buffer.isBuffer(entity) ? entity : new Buffer(entity); + } else { + throw new MockFileSystemError('ENOENT', path, `File "${path}" does not exist.`); + } + } + + writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive: boolean = false): void { const [folderPath, basename] = this.splitIntoFolderAndFile(path); const {entity} = this.findFromPath(folderPath); if (entity === null || !isFolder(entity)) { @@ -286,7 +295,7 @@ export type Entity = Folder|File|SymLink; export interface Folder { [pathSegments: string]: Entity; } -export type File = string; +export type File = string|Buffer; export class SymLink { constructor(public path: AbsoluteFsPath) {} } @@ -311,7 +320,7 @@ class MockFileSystemError extends Error { } export function isFile(item: Entity|null): item is File { - return typeof item === 'string'; + return Buffer.isBuffer(item) || typeof item === 'string'; } export function isSymLink(item: Entity|null): item is SymLink {