|
13 | 13 | */
|
14 | 14 |
|
15 | 15 | import * as File from 'vinyl';
|
| 16 | +import * as vfs from 'vinyl-fs'; |
| 17 | +import * as nodePath from 'path'; |
| 18 | +import * as fs from 'fs'; |
| 19 | +import { promisify } from 'util'; |
| 20 | +import { Transform } from 'stream'; |
| 21 | + |
| 22 | +const stat = promisify(fs.stat); |
| 23 | + |
16 | 24 |
|
17 | 25 | /**
|
18 | 26 | * Returns the string contents of a Vinyl File object, waiting for
|
@@ -41,3 +49,85 @@ export async function getFileContents(file: File): Promise<string> {
|
41 | 49 | `It has neither a buffer nor a stream.`);
|
42 | 50 | };
|
43 | 51 |
|
| 52 | + |
| 53 | +export class SyntheticFileMap { |
| 54 | + protected fileMap: Map<string, File> = new Map(); |
| 55 | + protected watcher: fs.FSWatcher | null = null; |
| 56 | + |
| 57 | + constructor(readonly basePath: string, protected transform: Transform) { |
| 58 | + this.watchFilesystem(); |
| 59 | + } |
| 60 | + |
| 61 | + watchFilesystem() { |
| 62 | + if (this.watcher != null) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + this.watcher = fs.watch(this.basePath, { |
| 67 | + recursive: true, |
| 68 | + persistent: false |
| 69 | + }, (eventType: string, filename: string) => |
| 70 | + this.onFsEvent(eventType, filename)); |
| 71 | + } |
| 72 | + |
| 73 | + stopWatchingFilesystem() { |
| 74 | + if (this.watcher == null) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + this.watcher.close(); |
| 79 | + } |
| 80 | + |
| 81 | + async hasFile(path: string): Promise<boolean> { |
| 82 | + const filePath = this.mappedPath(path); |
| 83 | + |
| 84 | + if (this.fileMap.has(filePath)) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + const stats = await stat(filePath); |
| 90 | + |
| 91 | + if (!stats.isDirectory()) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + } catch (e) {} |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + async readFile(path: string): Promise<File> { |
| 100 | + const filePath = this.mappedPath(path); |
| 101 | + |
| 102 | + if (this.fileMap.has(filePath)) { |
| 103 | + return this.fileMap.get(filePath); |
| 104 | + } |
| 105 | + |
| 106 | + return await new Promise<File>((resolve, reject) => { |
| 107 | + vfs.src([filePath]) |
| 108 | + .pipe(this.transform) |
| 109 | + .on('data', (file: File) => { |
| 110 | + // NOTE(cdata): A transform may emit more than one file here, as is |
| 111 | + // the case for HTML Modules => JS Modules |
| 112 | + this.fileMap.set(file.path, file); |
| 113 | + }) |
| 114 | + .on('end', () => { |
| 115 | + if (this.fileMap.has(filePath)) { |
| 116 | + resolve(this.fileMap.get(filePath)); |
| 117 | + } else { |
| 118 | + reject(new Error('Not found!')); |
| 119 | + } |
| 120 | + }); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private mappedPath(path: string): string { |
| 125 | + return nodePath.join(this.basePath, path); |
| 126 | + } |
| 127 | + |
| 128 | + private onFsEvent(_eventType: string, filePath: string): void { |
| 129 | + if (this.fileMap.has(filePath)) { |
| 130 | + this.fileMap.delete(filePath); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments