| 
									
										
										
										
											2017-10-13 09:03:28 -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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {runOneBuild} from '@angular/bazel'; | 
					
						
							|  |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as os from 'os'; | 
					
						
							|  |  |  | import * as path from 'path'; | 
					
						
							|  |  |  | import * as ts from 'typescript'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {createTsConfig} from './tsconfig_template'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface TestSupport { | 
					
						
							|  |  |  |   basePath: string; | 
					
						
							|  |  |  |   runfilesPath: string; | 
					
						
							|  |  |  |   angularCorePath: string; | 
					
						
							|  |  |  |   writeConfig({ | 
					
						
							|  |  |  |       srcTargetPath, depPaths, pathMapping, | 
					
						
							|  |  |  |   }: { | 
					
						
							|  |  |  |     srcTargetPath: string, | 
					
						
							|  |  |  |     depPaths?: string[], | 
					
						
							|  |  |  |     pathMapping?: Array<{moduleName: string; path: string;}>, | 
					
						
							| 
									
										
										
										
											2018-01-29 15:01:38 -08:00
										 |  |  |   }): {compilerOptions: ts.CompilerOptions}; | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |   read(fileName: string): string; | 
					
						
							|  |  |  |   write(fileName: string, content: string): void; | 
					
						
							|  |  |  |   writeFiles(...mockDirs: {[fileName: string]: string}[]): void; | 
					
						
							|  |  |  |   shouldExist(fileName: string): void; | 
					
						
							|  |  |  |   shouldNotExist(fileName: string): void; | 
					
						
							| 
									
										
										
										
											2017-10-24 14:54:08 +03:00
										 |  |  |   runOneBuild(): boolean; | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function setup( | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         bazelBin = 'bazel-bin', tsconfig = 'tsconfig.json', | 
					
						
							|  |  |  |     }: { | 
					
						
							|  |  |  |       bazelBin?: string, | 
					
						
							|  |  |  |       tsconfig?: string, | 
					
						
							|  |  |  |     } = {}): TestSupport { | 
					
						
							| 
									
										
										
										
											2017-12-06 06:56:49 -08:00
										 |  |  |   const runfilesPath = process.env['TEST_SRCDIR']; | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const basePath = makeTempDir(runfilesPath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const bazelBinPath = path.resolve(basePath, bazelBin); | 
					
						
							|  |  |  |   fs.mkdirSync(bazelBinPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 15:03:29 -08:00
										 |  |  |   const angularCorePath = path.resolve(runfilesPath, 'angular', 'packages', 'core'); | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |   const ngFiles = listFilesRecursive(angularCorePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const tsConfigJsonPath = path.resolve(basePath, tsconfig); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     basePath, | 
					
						
							|  |  |  |     runfilesPath, | 
					
						
							|  |  |  |     angularCorePath, | 
					
						
							|  |  |  |     write, | 
					
						
							|  |  |  |     read, | 
					
						
							|  |  |  |     writeFiles, | 
					
						
							|  |  |  |     writeConfig, | 
					
						
							|  |  |  |     shouldExist, | 
					
						
							|  |  |  |     shouldNotExist, | 
					
						
							|  |  |  |     runOneBuild: runOneBuildImpl | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // -----------------
 | 
					
						
							|  |  |  |   // helpers
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 15:01:38 -08:00
										 |  |  |   function mkdirp(dirname: string) { | 
					
						
							|  |  |  |     const parent = path.dirname(dirname); | 
					
						
							|  |  |  |     if (!fs.existsSync(parent)) { | 
					
						
							|  |  |  |       mkdirp(parent); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     fs.mkdirSync(dirname); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |   function write(fileName: string, content: string) { | 
					
						
							|  |  |  |     const dir = path.dirname(fileName); | 
					
						
							|  |  |  |     if (dir != '.') { | 
					
						
							|  |  |  |       const newDir = path.resolve(basePath, dir); | 
					
						
							| 
									
										
										
										
											2018-01-29 15:01:38 -08:00
										 |  |  |       if (!fs.existsSync(newDir)) mkdirp(newDir); | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     fs.writeFileSync(path.resolve(basePath, fileName), content, {encoding: 'utf-8'}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function read(fileName: string) { | 
					
						
							|  |  |  |     return fs.readFileSync(path.resolve(basePath, fileName), {encoding: 'utf-8'}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function writeFiles(...mockDirs: {[fileName: string]: string}[]) { | 
					
						
							|  |  |  |     mockDirs.forEach( | 
					
						
							|  |  |  |         (dir) => { Object.keys(dir).forEach((fileName) => { write(fileName, dir[fileName]); }); }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function writeConfig({ | 
					
						
							|  |  |  |       srcTargetPath, depPaths = [], pathMapping = [], | 
					
						
							|  |  |  |   }: { | 
					
						
							|  |  |  |     srcTargetPath: string, | 
					
						
							|  |  |  |     depPaths?: string[], | 
					
						
							|  |  |  |     pathMapping?: Array<{moduleName: string; path: string;}>, | 
					
						
							|  |  |  |   }) { | 
					
						
							|  |  |  |     srcTargetPath = path.resolve(basePath, srcTargetPath); | 
					
						
							|  |  |  |     const compilationTargetSrc = listFilesRecursive(srcTargetPath); | 
					
						
							|  |  |  |     const target = '//' + path.relative(basePath, srcTargetPath); | 
					
						
							|  |  |  |     const files = [...compilationTargetSrc]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     depPaths = depPaths.concat([angularCorePath]); | 
					
						
							|  |  |  |     pathMapping = pathMapping.concat([{moduleName: '@angular/core', path: angularCorePath}]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const depPath of depPaths) { | 
					
						
							|  |  |  |       files.push(...listFilesRecursive(depPath).filter(f => f.endsWith('.d.ts'))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const pathMappingObj = {}; | 
					
						
							|  |  |  |     for (const mapping of pathMapping) { | 
					
						
							|  |  |  |       pathMappingObj[mapping.moduleName] = [mapping.path]; | 
					
						
							|  |  |  |       pathMappingObj[path.join(mapping.moduleName, '*')] = [path.join(mapping.path, '*')]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const emptyTsConfig = ts.readConfigFile( | 
					
						
							|  |  |  |         path.resolve( | 
					
						
							| 
									
										
										
										
											2017-12-19 15:03:29 -08:00
										 |  |  |             runfilesPath, 'angular', 'packages', 'bazel', 'test', 'ngc-wrapped', 'empty', | 
					
						
							|  |  |  |             'empty_tsconfig.json'), | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |         read); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const tsconfig = createTsConfig({ | 
					
						
							|  |  |  |       defaultTsConfig: emptyTsConfig.config, | 
					
						
							|  |  |  |       rootDir: basePath, | 
					
						
							|  |  |  |       target: target, | 
					
						
							|  |  |  |       outDir: bazelBinPath, compilationTargetSrc, | 
					
						
							|  |  |  |       files: files, | 
					
						
							|  |  |  |       pathMapping: pathMappingObj, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     write(path.resolve(basePath, tsConfigJsonPath), JSON.stringify(tsconfig, null, 2)); | 
					
						
							| 
									
										
										
										
											2018-01-29 15:01:38 -08:00
										 |  |  |     return tsconfig; | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function shouldExist(fileName: string) { | 
					
						
							|  |  |  |     if (!fs.existsSync(path.resolve(basePath, fileName))) { | 
					
						
							|  |  |  |       throw new Error(`Expected ${fileName} to be emitted (basePath: ${basePath})`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function shouldNotExist(fileName: string) { | 
					
						
							|  |  |  |     if (fs.existsSync(path.resolve(basePath, fileName))) { | 
					
						
							|  |  |  |       throw new Error(`Did not expect ${fileName} to be emitted (basePath: ${basePath})`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function runOneBuildImpl(): boolean { return runOneBuild(['@' + tsConfigJsonPath]); } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 11:44:48 +01:00
										 |  |  | function makeTempDir(baseDir: string): string { | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |   const id = (Math.random() * 1000000).toFixed(0); | 
					
						
							|  |  |  |   const dir = path.join(baseDir, `tmp.${id}`); | 
					
						
							|  |  |  |   fs.mkdirSync(dir); | 
					
						
							|  |  |  |   return dir; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function listFilesRecursive(dir: string, fileList: string[] = []) { | 
					
						
							| 
									
										
										
										
											2017-12-06 06:56:49 -08:00
										 |  |  |   fs.readdirSync(dir).forEach(file => { | 
					
						
							| 
									
										
										
										
											2017-10-13 09:03:28 -07:00
										 |  |  |     if (fs.statSync(path.join(dir, file)).isDirectory()) { | 
					
						
							|  |  |  |       listFilesRecursive(path.join(dir, file), fileList); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       fileList.push(path.join(dir, file)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return fileList; | 
					
						
							|  |  |  | } |