| 
									
										
										
										
											2018-07-16 08:49:56 +01: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | import {existsSync, readFileSync, readdirSync, statSync} from 'fs'; | 
					
						
							|  |  |  | import * as mockFs from 'mock-fs'; | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  | import {join} from 'path'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  | const Module = require('module'); | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {mainNgcc} from '../../src/ngcc/src/main'; | 
					
						
							| 
									
										
										
										
											2019-01-25 19:48:27 +01:00
										 |  |  | import {getAngularPackagesFromRunfiles} from '../runfile_helpers'; | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | describe('ngcc main()', () => { | 
					
						
							|  |  |  |   beforeEach(createMockFileSystem); | 
					
						
							|  |  |  |   afterEach(restoreRealFileSystem); | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  |   it('should run ngcc without errors for fesm2015', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'fesm2015'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-07-16 08:49:56 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  |   it('should run ngcc without errors for fesm5', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'fesm5'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  |   it('should run ngcc without errors for esm2015', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'esm2015'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-07-24 16:53:23 +03:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should run ngcc without errors for esm5', () => { | 
					
						
							| 
									
										
										
										
											2018-07-30 20:06:51 +03:00
										 |  |  |     const format = 'esm5'; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |     expect(mainNgcc(['-f', format, '-s', '/node_modules'])).toBe(0); | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | function createMockFileSystem() { | 
					
						
							| 
									
										
										
										
											2019-01-25 19:48:27 +01:00
										 |  |  |   mockFs({'/node_modules/@angular': loadAngularPackages()}); | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  |   spyOn(Module, '_resolveFilename').and.callFake(mockResolve); | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-25 13:06:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  | function restoreRealFileSystem() { | 
					
						
							|  |  |  |   mockFs.restore(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-25 19:48:27 +01:00
										 |  |  | /** Load the built Angular packages into an in-memory structure. */ | 
					
						
							|  |  |  | function loadAngularPackages(): Directory { | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   const packagesDirectory: Directory = {}; | 
					
						
							| 
									
										
										
										
											2019-01-25 19:48:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getAngularPackagesFromRunfiles().forEach( | 
					
						
							|  |  |  |       ({name, pkgPath}) => { packagesDirectory[name] = loadDirectory(pkgPath); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 11:32:01 +01:00
										 |  |  |   return packagesDirectory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Load real files from the filesystem into an "in-memory" structure, | 
					
						
							|  |  |  |  * which can be used with `mock-fs`. | 
					
						
							|  |  |  |  * @param directoryPath the path to the directory we want to load. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function loadDirectory(directoryPath: string): Directory { | 
					
						
							|  |  |  |   const directory: Directory = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   readdirSync(directoryPath).forEach(item => { | 
					
						
							|  |  |  |     const itemPath = join(directoryPath, item); | 
					
						
							|  |  |  |     if (statSync(itemPath).isDirectory()) { | 
					
						
							|  |  |  |       directory[item] = loadDirectory(itemPath); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       directory[item] = readFileSync(itemPath, 'utf-8'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return directory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface Directory { | 
					
						
							|  |  |  |   [pathSegment: string]: string|Directory; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 15:59:10 +01:00
										 |  |  | function mockResolve(p: string): string|null { | 
					
						
							|  |  |  |   if (existsSync(p)) { | 
					
						
							|  |  |  |     const stat = statSync(p); | 
					
						
							|  |  |  |     if (stat.isFile()) { | 
					
						
							|  |  |  |       return p; | 
					
						
							|  |  |  |     } else if (stat.isDirectory()) { | 
					
						
							|  |  |  |       const pIndex = mockResolve(p + '/index'); | 
					
						
							|  |  |  |       if (pIndex && existsSync(pIndex)) { | 
					
						
							|  |  |  |         return pIndex; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (const ext of ['.js', '.d.ts']) { | 
					
						
							|  |  |  |     if (existsSync(p + ext)) { | 
					
						
							|  |  |  |       return p + ext; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return null; | 
					
						
							| 
									
										
										
										
											2019-01-25 19:44:49 +01:00
										 |  |  | } |