| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-17 12:24:33 -08:00
										 |  |  | import {CompilerHostContext} from '@angular/compiler-cli/src/compiler_host'; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | import * as ts from 'typescript'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export type Entry = string | Directory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | export interface Directory { [name: string]: Entry; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-17 12:24:33 -08:00
										 |  |  | export class MockAotContext implements CompilerHostContext { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   constructor(public currentDirectory: string, private files: Entry) {} | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  |   fileExists(fileName: string): boolean { return typeof this.getEntry(fileName) === 'string'; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  |   directoryExists(path: string): boolean { return typeof this.getEntry(path) === 'object'; } | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   readFile(fileName: string): string|undefined { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const data = this.getEntry(fileName); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     if (typeof data === 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |       return data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 11:13:20 -08:00
										 |  |  |   readResource(fileName: string): Promise<string> { | 
					
						
							|  |  |  |     const result = this.readFile(fileName); | 
					
						
							|  |  |  |     if (result == null) { | 
					
						
							|  |  |  |       return Promise.reject(new Error(`Resource not found: ${fileName}`)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return Promise.resolve(result); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  |   writeFile(fileName: string, data: string): void { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const parts = fileName.split('/'); | 
					
						
							|  |  |  |     const name = parts.pop(); | 
					
						
							|  |  |  |     const entry = this.getEntry(parts); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     if (entry && typeof entry !== 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |       entry[name] = data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  |   assumeFileExists(fileName: string): void { this.writeFile(fileName, ''); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getEntry(fileName: string|string[]): Entry|undefined { | 
					
						
							|  |  |  |     let parts = typeof fileName === 'string' ? fileName.split('/') : fileName; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     if (parts[0]) { | 
					
						
							|  |  |  |       parts = this.currentDirectory.split('/').concat(parts); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     parts.shift(); | 
					
						
							|  |  |  |     parts = normalize(parts); | 
					
						
							|  |  |  |     let current = this.files; | 
					
						
							|  |  |  |     while (parts.length) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const part = parts.shift(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       if (typeof current === 'string') { | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const next = (<Directory>current)[part]; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |       if (next === undefined) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       current = next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return current; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getDirectories(path: string): string[] { | 
					
						
							|  |  |  |     const dir = this.getEntry(path); | 
					
						
							|  |  |  |     if (typeof dir !== 'object') { | 
					
						
							|  |  |  |       return []; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return Object.keys(dir).filter(key => typeof dir[key] === 'object'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function normalize(parts: string[]): string[] { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const result: string[] = []; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |   while (parts.length) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const part = parts.shift(); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     switch (part) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       case '.': | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       case '..': | 
					
						
							|  |  |  |         result.pop(); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         result.push(part); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockCompilerHost implements ts.CompilerHost { | 
					
						
							| 
									
										
										
										
											2016-11-17 12:24:33 -08:00
										 |  |  |   constructor(private context: MockAotContext) {} | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  |   fileExists(fileName: string): boolean { return this.context.fileExists(fileName); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  |   readFile(fileName: string): string { return this.context.readFile(fileName); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 11:15:23 -07:00
										 |  |  |   directoryExists(directoryName: string): boolean { | 
					
						
							|  |  |  |     return this.context.directoryExists(directoryName); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getSourceFile( | 
					
						
							|  |  |  |       fileName: string, languageVersion: ts.ScriptTarget, | 
					
						
							|  |  |  |       onError?: (message: string) => void): ts.SourceFile { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const sourceText = this.context.readFile(fileName); | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  |     if (sourceText) { | 
					
						
							|  |  |  |       return ts.createSourceFile(fileName, sourceText, languageVersion); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return undefined; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getDefaultLibFileName(options: ts.CompilerOptions): string { | 
					
						
							|  |  |  |     return ts.getDefaultLibFileName(options); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 14:55:58 -08:00
										 |  |  |   writeFile: ts.WriteFileCallback = (fileName, text) => { this.context.writeFile(fileName, text); }; | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 14:55:58 -08:00
										 |  |  |   getCurrentDirectory(): string { return this.context.currentDirectory; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getCanonicalFileName(fileName: string): string { return fileName; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   useCaseSensitiveFileNames(): boolean { return false; } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   getNewLine(): string { return '\n'; } | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getDirectories(path: string): string[] { return this.context.getDirectories(path); } | 
					
						
							| 
									
										
										
										
											2016-06-09 14:51:53 -07:00
										 |  |  | } |