| 
									
										
										
										
											2020-07-27 13:02:28 -07:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2019 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ts = require('typescript'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const Source = require('./doclint/Source'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function checkDeps() { | 
					
						
							|  |  |  |   const root = path.normalize(path.join(__dirname, '..')); | 
					
						
							|  |  |  |   const src = path.normalize(path.join(__dirname, '..', 'src')); | 
					
						
							|  |  |  |   const sources = await Source.readdir(src); | 
					
						
							|  |  |  |   const program = ts.createProgram({ | 
					
						
							|  |  |  |     options: { | 
					
						
							|  |  |  |       allowJs: true, | 
					
						
							|  |  |  |       target: ts.ScriptTarget.ESNext, | 
					
						
							|  |  |  |       strict: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     rootNames: sources.map(source => source.filePath()), | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   const sourceFiles = program.getSourceFiles(); | 
					
						
							|  |  |  |   const errors = []; | 
					
						
							|  |  |  |   sourceFiles.filter(x => !x.fileName.includes('node_modules')).map(x => visit(x, x.fileName)); | 
					
						
							|  |  |  |   for (const error of errors) | 
					
						
							|  |  |  |     console.log(error); | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  |   if (errors.length) { | 
					
						
							|  |  |  |     console.log(`--------------------------------------------------------`); | 
					
						
							|  |  |  |     console.log(`Changing the project structure or adding new components?`); | 
					
						
							|  |  |  |     console.log(`Update DEPS in //${path.relative(root, __filename)}.`); | 
					
						
							|  |  |  |     console.log(`--------------------------------------------------------`); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-27 13:02:28 -07:00
										 |  |  |   process.exit(errors.length ? 1 : 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function visit(node, fileName) { | 
					
						
							|  |  |  |     if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) { | 
					
						
							|  |  |  |       const importName = node.moduleSpecifier.text; | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  |       const importPath = path.resolve(path.dirname(fileName), importName) + '.ts'; | 
					
						
							| 
									
										
										
										
											2020-07-27 13:02:28 -07:00
										 |  |  |       if (!allowImport(fileName, importPath)) | 
					
						
							|  |  |  |         errors.push(`Disallowed import from ${path.relative(root, fileName)} to ${path.relative(root, importPath)}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ts.forEachChild(node, x => visit(x, fileName)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function allowImport(from, to) { | 
					
						
							| 
									
										
										
										
											2020-08-22 07:07:13 -07:00
										 |  |  |     from = from.substring(from.indexOf('src' + path.sep)).replace(/\\/g, '/'); | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  |     to = to.substring(to.indexOf('src' + path.sep)).replace(/\\/g, '/'); | 
					
						
							|  |  |  |     const fromDirectory = from.substring(0, from.lastIndexOf('/') + 1); | 
					
						
							| 
									
										
										
										
											2020-08-22 15:46:42 -07:00
										 |  |  |     const toDirectory = to.substring(0, to.lastIndexOf('/') + 1); | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  |     if (fromDirectory === toDirectory) | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (!DEPS[from]) { | 
					
						
							|  |  |  |       if (from.endsWith('/')) | 
					
						
							|  |  |  |         from = from.substring(0, from.length - 1); | 
					
						
							|  |  |  |       if (from.lastIndexOf('/') === -1) | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  |         throw new Error(`Cannot find DEPS for ${fromDirectory}`); | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  |       from = from.substring(0, from.lastIndexOf('/') + 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  |     for (const dep of DEPS[from]) { | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  |       if (to === dep || toDirectory === dep) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       if (dep.endsWith('**')) { | 
					
						
							|  |  |  |         const parent = dep.substring(0, dep.length - 2); | 
					
						
							|  |  |  |         if (to.startsWith(parent)) | 
					
						
							| 
									
										
										
										
											2020-08-22 07:07:13 -07:00
										 |  |  |           return true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2020-07-27 13:02:28 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  | const DEPS = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEPS['src/protocol/'] = ['src/utils/']; | 
					
						
							|  |  |  | DEPS['src/install/'] = ['src/utils/']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 06:51:51 -07:00
										 |  |  | // Client depends on chromium protocol for types.
 | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  | DEPS['src/client/'] = ['src/utils/', 'src/protocol/', 'src/server/chromium/protocol.ts']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  | DEPS['src/dispatchers/'] = ['src/utils/', 'src/protocol/', 'src/server/**']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Generic dependencies for server-side code.
 | 
					
						
							| 
									
										
										
										
											2020-08-24 06:51:51 -07:00
										 |  |  | DEPS['src/server/'] = [ | 
					
						
							|  |  |  |   'src/utils/', | 
					
						
							|  |  |  |   'src/generated/', | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  |   // Can depend on files directly in the server directory.
 | 
					
						
							|  |  |  |   'src/server/', | 
					
						
							|  |  |  |   // Can depend on any files in these subdirectories.
 | 
					
						
							|  |  |  |   'src/server/common/**', | 
					
						
							|  |  |  |   'src/server/injected/**', | 
					
						
							| 
									
										
										
										
											2020-08-24 06:51:51 -07:00
										 |  |  |   'src/server/debug/**', | 
					
						
							|  |  |  | ]; | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  | // No dependencies for code shared between node and page.
 | 
					
						
							|  |  |  | DEPS['src/server/common/'] = []; | 
					
						
							|  |  |  | // Strict dependencies for injected code.
 | 
					
						
							| 
									
										
										
										
											2020-08-24 15:30:45 -07:00
										 |  |  | DEPS['src/server/injected/'] = ['src/server/common/']; | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 15:30:45 -07:00
										 |  |  | // Electron uses chromium internally.
 | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  | DEPS['src/server/electron/'] = [...DEPS['src/server/'], 'src/server/chromium/']; | 
					
						
							| 
									
										
										
										
											2020-08-23 21:24:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEPS['src/server/playwright.ts'] = [...DEPS['src/server/'], 'src/server/chromium/', 'src/server/webkit/', 'src/server/firefox/']; | 
					
						
							| 
									
										
										
										
											2020-08-24 14:48:03 -07:00
										 |  |  | DEPS['src/server.ts'] = DEPS['src/inprocess.ts'] = DEPS['src/browserServerImpl.ts'] = ['src/**']; | 
					
						
							| 
									
										
										
										
											2020-08-22 07:07:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 13:02:28 -07:00
										 |  |  | checkDeps(); |